C - Cross
Time Limit: 2 sec / Memory Limit: 1024 MB
Score : 300300 points
Problem Statement
We have a grid with �H horizontal rows and �W vertical columns. We denote by (�,�)(i,j) the cell at the �i-th row from the top and �j-th column from the left of the grid.
Each cell in the grid has a symbol #
or .
written on it. Let �[�][�]C[i][j] be the character written on (�,�)(i,j). For integers �i and �j such that at least one of 1≤�≤�1≤i≤H and 1≤�≤�1≤j≤W is violated, we define �[�][�]C[i][j] to be .
.
(4�+1)(4n+1) squares, consisting of (�,�)(a,b) and (�+�,�+�),(�+�,�−�),(�−�,�+�),(�−�,�−�)(a+d,b+d),(a+d,b−d),(a−d,b+d),(a−d,b−d) (1≤�≤�,1≤�)(1≤d≤n,1≤n), are said to be a cross of size �n centered at (�,�)(a,b) if and only if all of the following conditions are satisfied:
- �[�][�]C[a][b] is
#
. - �[�+�][�+�],�[�+�][�−�],�[�−�][�+�]C[a+d][b+d],C[a+d][b−d],C[a−d][b+d], and �[�−�][�−�]C[a−d][b−d] are all
#
, for all integers �d such that 1≤�≤�1≤d≤n, - At least one of �[�+�+1][�+�+1],�[�+�+1][�−�−1],�[�−�−1][�+�+1]C[a+n+1][b+n+1],C[a+n+1][b−n−1],C[a−n−1][b+n+1], and �[�−�−1][�−�−1]C[a−n−1][b−n−1] is
.
.
For example, the grid in the following figure has a cross of size 11 centered at (2,2)(2,2) and another of size 22 centered at (3,7)(3,7).
The grid has some crosses. No #
is written on the cells except for those comprising a cross.
Additionally, no two squares that comprise two different crosses share a corner. The two grids in the following figure are the examples of grids where two squares that comprise different crosses share a corner; such grids are not given as an input. For example, the left grid is invalid because (3,3)(3,3) and (4,4)(4,4) share a corner.
Let �=min(�,�)N=min(H,W), and ��Sn be the number of crosses of size �n. Find �1,�2,…,��S1,S2,…,SN.
Constraints
- 3≤�,�≤1003≤H,W≤100
- �[�][�]C[i][j] is
#
or.
. - No two different squares that comprise two different crosses share a corner.
- �H and �W are integers.
Input
The input is given from Standard Input in the following format:
�H �W �[1][1]�[1][2]…�[1][�]C[1][1]C[1][2]…C[1][W] �[2][1]�[2][2]…�[2][�]C[2][1]C[2][2]…C[2][W] ⋮⋮ �[�][1]�[�][2]…�[�][�]C[H][1]C[H][2]…C[H][W]
Output
Print �1,�2,…S1,S2,…, and ��SN, separated by spaces.
Sample Input 1 Copy
5 9 #.#.#...# .#...#.#. #.#...#.. .....#.#. ....#...#
Sample Output 1 Copy
1 1 0 0 0
As described in the Problem Statement, there are a cross of size 11 centered at (2,2)(2,2) and another of size 22 centered at (3,7)(3,7).
Sample Input 2 Copy
3 3 ... ... ...
Sample Output 2 Copy
0 0 0
There may be no cross.
Sample Input 3 Copy
3 16 #.#.....#.#..#.# .#.......#....#. #.#.....#.#..#.#
Sample Output 3 Copy
3 0 0
Sample Input 4 Copy
15 20 #.#..#.............# .#....#....#.#....#. #.#....#....#....#.. ........#..#.#..#... #.....#..#.....#.... .#...#....#...#..#.# ..#.#......#.#....#. ...#........#....#.# ..#.#......#.#...... .#...#....#...#..... #.....#..#.....#.... ........#.......#... #.#....#....#.#..#.. .#....#......#....#. #.#..#......#.#....#
Sample Output 4 Copy
5 0 1 0 0 0 1 0 0 0 0 0 0 0 0
AC:(c++)
#include <iostream>
#include <cstring>
#include <algorithm>
#include <map>
using namespace std;
const int N = 110, INF = 100000;
char g[N][N];
int n, m;
int dx[4] = {-1, -1, 1, 1}, dy[4] = {-1, 1, -1, 1};
int dfs(int x, int y)
{
int res = INF;
for (int i = 0; i < 4; i ++ )
{
int a = x, b = y;
int cnt = -1;
while (a >= 1 && b >= 1 && a <= n && b <= m && g[a][b] == '#')
{
cnt ++;
a += dx[i]; b += dy[i];
}
res = min(res, cnt);
}
return res;
}
int main()
{
cin >> n >> m;
for (int i = 1; i <= n; i ++ )
cin >> g[i] + 1;
map<int, int> ans;
for (int i = 1; i <= n; i ++ )
for (int j = 1; j <= m; j ++ )
if (g[i][j] == '#')ans[dfs(i, j)] += 1;
for (int i = 1; i <= min(n, m); i ++ ) cout << ans[i] << ' ';
cout << endl;
return 0;
}