Weed

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  equal squares. Andrew's memory is phenomenal. He remembers which squares were occupied by the weed. For the purpose of simplicity, Andrew thinks that each square is either fully occupied by the weed or completely free from it. Andrew likes botany and he knows that if some square is free from the weed but at least two of its adjacent squares are occupied by the weed (two squares are adjacent if they have common side), that square will be also occupied by the weed soon. Andrew is pretty sure that during last years weed occupied every square possible. Please help Andrew to estimate how many squares is occupied by the weed.

Input
The first line of the input contains integers N and M (1 ≤ NM ≤ 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;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值