阅读背景:

TOJ:Prairie dogs_世界在身后,观我背影辽阔

来源:互联网 

2192.   Prairie dogs I

#include <iostream>
#include <cstdio>
#include <cstring>
#define MAXN 10
using namespace std;

char s[MAXN][MAXN];
bool v[MAXN][MAXN];
int m, n, room, cirs, area;

void search(int x, int y)
{
	if (x >= 0 && y >= 0 && x < m && y < n && s[x][y] == 'X')
	{
		s[x][y] = '*'; area++;
		search(x, y + 1); search(x + 1, y + 1);
		search(x + 1, y); search(x + 1, y - 1);
		search(x - 1, y); search(x - 1, y - 1);
		search(x, y - 1); search(x - 1, y + 1);
	}
}

void cal(int x, int y)
{
	
	if (x >= 0 && x < m && y >= 0 && y < n && s[x][y] == '*')
	{
		if (x == 0 || x == m - 1) cirs++;
		if (y == 0 || y == n - 1) cirs++;
		s[x][y] = '#'; v[x][y] = 1;
		if (x > 0 && s[x - 1][y] != '*' && !v[x - 1][y]) cirs++;
		if (x < m - 1 && s[x + 1][y] != '*' && !v[x + 1][y]) cirs++;
		if (y > 0 && s[x][y - 1] != '*' && !v[x][y - 1]) cirs++;
		if (y < n - 1 && s[x][y + 1] != '*' && !v[x][y + 1]) cirs++;
		cal(x - 1, y); cal(x + 1, y); cal(x - 1, y - 1); cal(x - 1, y + 1);
		cal(x, y - 1); cal(x, y + 1); cal(x + 1, y - 1); cal(x + 1, y - 1);
	}
}

int main()
{
	while (cin >> m >> n, m + n)
	{
		double res = 0;
		memset(v, 0, sizeof(v));
		for (int i = 0; i < m; i++)
			cin >> s[i];
		for (int i = 0; i < m; i++)
			for (int j = 0; j < n; j++)
			{
				cirs = 0; area = 0;
				if (s[i][j] == 'X')
				{
					search(i, j), cal(i, j);
					res += 1.0 * cirs / area;
				}
			}
		if (res > 1.0 * m * n / 4)
			printf("NO\n");
		else
			printf("YES\n");
	}
}#include <iostream>



你的当前访问异常,请进行认证后继续阅读剩余内容。

分享到: