battleship
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.
InputThe 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 field. Each line contains ncharacters, each of which is either '#' (denotes a definitely empty cell) or '.' (denotes a cell that can belong to the ship).
OutputOutput 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.
examples
Input
4 3 #..# #.#. .... .###
Output
3 2
Input
10 4 #....##... .#...#.... ..#..#..#. ...#.#.... .#..##.#.. .....#...# ...#.##... .#...#.#.. .....#..#. ...#.#...#
Output
6 1
Input
19 6 ##..............### #......#####.....## .....#########..... ....###########.... ...#############... ..###############.. .#################. .#################. .#################. .#################. #####....##....#### ####............### ####............### #####...####...#### .#####..####..##### ...###........###.. ....###########.... .........##........ #.................#
Output
1 8Note
The picture below shows the three possible locations of the ship that contain the cell (3,2)
in the first sample.
题意:找出船体相交最多的点,每条船要么水平要么垂直,长度为k
思路:直接暴力模拟,可以先预处理存下每个 . 的上下左右有多少连续的 . 存下来
最后遍历,每当到 . 的时候就看上下左右的长度总共可以包含多少船不断更新
code:
#include <iOStream>
#include <cstdio>
#include <cstring>
using namespace std;
const int N = 120;
int G[N][N];
int up[N][N],dn[N][N],lt[N][N],rt[N][N];
int n,k;
int ansx = 1,ansy = 1;
int cnt,maxn = 0;
int main(){
char s[N];
scanf("%d%d",&n,&k);
for(int i = 1; i <= n; i++){
scanf("%s",s);
for(int j = 0; j < n; j++){
if(s[j] == '.')
G[i][j+1] = 1;
}
}
for(int i = 1; i <= n; i++){
for(int j = 1; j <= n; j++){
if(G[i][j]){
lt[i][j] = lt[i][j-1] + 1;
up[i][j] = up[i-1][j] + 1;
}
}
}
for(int i = n; i >= 1; i--){
for(int j = n; j >= 1; j--){
if(G[i][j]){
rt[i][j] = rt[i][j+1] + 1;
dn[i][j] = dn[i+1][j] + 1;
}
}
}
for(int i = 1; i <= n; i++){
for(int j = 1; j <= n; j++){
if(G[i][j]){
cnt = 0;
if(min(lt[i][j],k) + min(rt[i][j],k) > k)
cnt += (min(lt[i][j],k) + min(rt[i][j],k) - k);
if(min(up[i][j],k) + min(dn[i][j],k) > k)
cnt += (min(up[i][j],k) + min(dn[i][j],k) - k);
if(cnt > maxn){
maxn = cnt;
ansx = i;
ansy = j;
}
}
}
}
printf("%d %d\n",ansx,ansy);
return 0;
}