2018.12.14 浪在ACM 集训队第九次测试赛

浪在ACM 集训队第九次测试赛

 

B  Battleship  

E  Masha and two friends

 

 

B 传送门

题意:

    战船上有占地n*n的房间cells[][],只由当cells[i][j]=='.'时才能够容纳水手。
    已知水手需要k个连续的房间'.',可以是水平方向连续k个'.'或竖直方向的连续k个'.'。
    问哪个坐标(x,y)含有最多的连续的k个房间,如果不存在,输出任意坐标。
View Code

题解:

  我的做法:暴力

  求出每个坐标最多含有多少种容纳水手的方式,找到最大的。

  具体细节看代码:

 1 #include<iostream>
 2 #include<cstdio>
 3 using namespace std;
 4 const int maxn=100+10;
 5 
 6 int n,k;
 7 char cells[maxn][maxn];
 8 
 9 int Find(int a,int b,int c)
10 {
11     int res=0;
12     while(a <= c)
13     {
14         if(a+k-1 >= c && a+k-1 <= b)
15             res++;
16         a++;
17     }
18     return res;
19 }
20 int update(int curX,int curY)//查找(curX,curY)含有的解
21 {
22     int upX=curX,downX=curX;
23     while(upX-1 >= 1 && cells[upX-1][curY] == '.')
24         upX--;
25     while(downX+1 <= n && cells[downX+1][curY] == '.')
26         downX++;
27 
28     int leftY=curY,rightY=curY;
29     while(leftY-1 >= 1 && cells[curX][leftY-1] == '.')
30         leftY--;
31     while(rightY+1 <= n && cells[curX][rightY+1] == '.')
32         rightY++;
33 
34     int res=Find(upX,downX,curX);
35     res += Find(leftY,rightY,curY);
36     return res;
37 }
38 void Solve()
39 {
40     int res=0;
41     int x=1,y=1;
42     for(int i=1;i <= n;++i)
43         for(int j=1;j <= n;++j)
44         {
45             if(cells[i][j] != '.')
46                 continue;
47             int curRes=update(i,j);
48             if(res < curRes)
49             {
50                 res=curRes;
51                 x=i,y=j;
52             }
53         }
54     printf("%d %d\n",x,y);
55 }
56 int main()
57 {
58     scanf("%d%d",&n,&k);
59     for(int i=1;i <= n;++i)
60         scanf("%s",cells[i]+1);
61     Solve();
62 
63     return 0;
64 }
View Code

题目来源:CodeForces - 965B

 

E 传送门

  题解

posted @ 2018-12-19 21:49  HHHyacinth  阅读(143)  评论(0编辑  收藏  举报