Weed
Time Limit : 2000/1000ms (Java/Other) Memory Limit : 131072/65536K (Java/Other)
Total Submission(s) : 8 Accepted Submission(s) : 6
Problem Description
Andrew has visited his garden for the last time many years ago. Today's property taxes are so high, so Andrew decided to sell his garden. The land was not cultivated for a long time and now it is probably a lot of weed on it. Andrew wants to remove everything from the ground before selling. Now he wants to estimate the amount of work.
The garden has the rectangular form and is divided into

Input
The first line of the input contains integers N and M (1 ≤ N, M ≤ 1000). Next N lines contain M characters each. Character X denotes that the corresponding square is occupied by the weed. A period character
(
.) denotes an empty square.
Output
Print one integer denoting the number of squares occupied by the weed after so many years.Example(s)
sample input |
sample output |
3 3 X.. .X. .X. |
6 |
sample input |
sample output |
3 4 X..X .X.. .X.. |
12 |
#include<stdio.h>
#include<string.h>
#include<queue>
#include<algorithm>
using namespace std;
struct node
{
int x = 0, y = 0; // 坐标
}str, str1, str2;
queue<node>Q;
int a[1005][1005]; // 标记草
int b[8][2] = { { 1, 1 }, { 1, -1 }, { -1, -1 }, { -1, 1 }, { 0, 2 }, { 2, 0 }, { 0, -2 }, { -2, 0 }, };
int n, m, g, h;
void BFS()
{
while (!Q.empty())
{
str = Q.front(); Q.pop();
g += 1;
for (int i = 0; i < 8; i++) // 八个方向
{
str1 = str;
str1.x += b[i][0];
str1.y += b[i][1];
if ((a[str.x][str.y + 1] || (str.y + 1 >= m)) && (a[str.x + 1][str.y] || (str.x + 1 >= n)) && (a[str.x][str.y - 1] || (str.y - 1<0)) && (a[str.x - 1][str.y] || (str.x - 1<0))) break;
if (str1.x >= 0 && str1.x<n&&str1.y >= 0 && str1.y<m)
{
if (i == 0)
{
if (a[str1.x][str1.y])
{
if (!a[str.x][str.y + 1]) { str2.x = str.x; str2.y = str.y + 1; Q.push(str2); a[str.x][str.y + 1] = 1; h += 1; if (h >= n*m) return; }
if (!a[str.x + 1][str.y]) { str2.x = str.x + 1; str2.y = str.y; Q.push(str2); a[str.x + 1][str.y] = 1; h += 1; if (h >= n*m) return; }
}
}
else if (i == 1)
{
if (a[str1.x][str1.y])
{
if (!a[str.x + 1][str.y]) { str2.x = str.x + 1; str2.y = str.y; Q.push(str2); a[str.x + 1][str.y] = 1; h += 1; if (h >= n*m) return; }
if (!a[str.x][str.y - 1]) { str2.x = str.x; str2.y = str.y - 1; Q.push(str2); a[str.x][str.y - 1] = 1; h += 1; if (h >= n*m) return; }
}
}
else if (i == 2)
{
if (a[str1.x][str1.y])
{
if (!a[str.x][str.y - 1]) { str2.x = str.x; str2.y = str.y - 1; Q.push(str2); a[str.x][str.y - 1] = 1; h += 1; if (h >= n*m) return; }
if (!a[str.x - 1][str.y]) { str2.x = str.x - 1; str2.y = str.y; Q.push(str2); a[str.x - 1][str.y] = 1; h += 1; if (h >= n*m) return; }
}
}
else if (i == 3)
{
if (a[str1.x][str1.y])
{
if (!a[str.x - 1][str.y]) { str2.x = str.x - 1; str2.y = str.y; Q.push(str2); a[str.x - 1][str.y] = 1; h += 1; if (h >= n*m) return; }
if (!a[str.x][str.y + 1]) { str2.x = str.x; str2.y = str.y + 1; Q.push(str2); a[str.x][str.y + 1] = 1; h += 1; if (h >= n*m) return; }
}
}
else if (i == 4)
{
if (a[str1.x][str1.y])
{
if (!a[str.x][str.y + 1]) { str2.x = str.x; str2.y = str.y + 1; Q.push(str2); a[str.x][str.y + 1] = 1; h += 1; if (h >= n*m) return; }
}
}
else if (i == 5)
{
if (a[str1.x][str1.y])
{
if (!a[str.x + 1][str.y]) { str2.x = str.x + 1; str2.y = str.y; Q.push(str2); a[str.x + 1][str.y] = 1; h += 1; if (h >= n*m) return; }
}
}
else if (i == 6)
{
if (a[str1.x][str1.y])
{
if (!a[str.x][str.y - 1]) { str2.x = str.x; str2.y = str.y - 1; Q.push(str2); a[str.x][str.y - 1] = 1; h += 1; if (h >= n*m) return; }
}
}
else if (i == 7)
{
if (a[str1.x][str1.y])
{
if (!a[str.x - 1][str.y]) { str2.x = str.x - 1; str2.y = str.y; Q.push(str2); a[str.x - 1][str.y] = 1; h += 1; if (h >= n*m) return; }
}
}
}
}
}
return;
}
int main()
{
while (scanf("%d%d", &n, &m) != EOF)
{
char c; h = 0;
memset(a, 0, sizeof(a));
for (int i = 0; i < n; i++)
{
getchar();
for (int j = 0; j < m; j++)
{
scanf("%c", &c);
if (c == 'X')
{
a[i][j] = 1; h += 1;
str.x = i; str.y = j;
Q.push(str);
}
}
}
g = 0;
BFS();
if (h > g) g = h;
printf("%d\n", g);
}
return 0;
}