CF 965 B. Battleship
Arkady is playing Battleship. The rules of this game aren't really important.There is a field of n×n
cells. There should be exactly one k-decker on the field, i. e. a ship that is kcells long oriented either horizontally or vertically. However,
Arkady doesn't know where it is located. For each cell Arkady knows if it is definitely empty or can contain a part of the ship.
Consider all possible locations of the ship. Find such a cell that belongs to the maximum possible number of different locations of the ship.
The first line contains two integers n and k (1≤k≤n≤100) — the size of the field and the size of the ship.
The next n lines contain the filed.Each line cotains ncharacters, each of which is either '#' (denotes a definitely empty cell) or '.' (denotes a cell that can belong to the ship).
Output two integers — the row and the column of a cell that belongs to the maximum possible number of different locations of the ship.
If there are multiple answers, output any of them. In particular, if no ship can be placed on the field, you can output any cell.
4 3
#..#
#.#.
....
.###
3 2
10 4
#....##...
.#...#....
..#..#..#.
...#.#....
.#..##.#..
.....#...#
...#.##...
.#...#.#..
.....#..#.
...#.#...#
6 1
19 6
##..............###
#......#####.....##
.....#########.....
....###########....
...#############...
..###############..
.#################.
.#################.
.#################.
.#################.
#####....##....####
####............###
####............###
#####...####...####
.#####..####..#####
...###........###..
....###########....
.........##........
#.................#
1 8
The picture below shows the three possible locations of the ship that contain the cell (3,2)
in the first sample.
读了好几遍终于看懂了题目意思,"#"代表空单元,"."代表船部件,并且水平或者竖直的连起来的k个可能构成一个完整的部件,问有没有一个单元可以和其他单元组合构成尽可能多的部件,暴力一下。
#include <iostream> #include <algorithm> #include <cstring> #include <cstdio> #include <vector> #include <queue> #include <stack> #include <cstdlib> #include <iomanip> #include <cmath> #include <cassert> #include <ctime> #include <map> #include <set> using namespace std; #pragma comment(linker, "/stck:1024000000,1024000000") #define lowbit(x) (x&(-x)) #define max(x,y) (x>=y?x:y) #define min(x,y) (x<=y?x:y) #define MAX 100000000000000000 #define MOD 1000000007 #define pi acos(-1.0) #define ei exp(1) #define PI 3.1415926535897932384626433832 #define ios() ios::sync_with_stdio(true) #define INF 0x3f3f3f3f #define mem(a) ((a,0,sizeof(a))) typedef long long ll; char a[106][106]; int vis[106][106]; int n,k; int main() { scanf("%d%d",&n,&k); for(int i=0;i<n;i++) scanf("%s",&a[i]); int col=0,row=0,cnt=0,tot=0; memset(vis,0,sizeof(vis)); for(int i=0;i<n;i++) { for(int j=0;j<n;j++) { if(a[i][j]=='.') { cnt=0; for(int z=j;z<min(n,j+k);z++) if(a[i][z]=='.') cnt++; if(cnt==k){ for(int z=j;z<min(n,j+k);z++) vis[i][z]++; } cnt=0; for(int z=i;z<min(n,i+k);z++) if(a[z][j]=='.') cnt++; if(cnt==k){ for(int z=i;z<min(n,i+k);z++) vis[z][j]++; } } if(vis[i][j]>tot) { tot=vis[i][j]; row=i; col=j; } } } printf("%d %d\n",row+1,col+1); return 0; }