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");
	}
}

2201.     Prairie dogs II

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

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

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

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

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

2805.   Prairie dogs III

#include <iostream>
#include <stdio.h>
#include <cmath>
using namespace std;

void print(int n)
	{
		int w = (int)pow(3.0,n - 1);
		if (n == 0)
			{
				cout << "@";
				return;
			}
		else
			{
				print(n - 1);
				for (int i = 0; i < w; i ++)
					cout << " ";
				print(n - 1);
			}
	}

 
int main()
{
	int n;
	while (scanf("%d",&n)!=EOF)
		{
			print(n);
			cout << endl;
		}

	return 0;
}


2818.   Prairie dogs IV

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

int  a[MAXN][MAXN], flag = 1;

void make()
{
	memset(a, 0, sizeof(a));
	int cnt = 1, i, j;
	i = j = 1000; flag = 1;
	a[i][j++] = cnt++;
	a[i][j] = cnt++;
	while (true)
	{
		if (i < 0 || i > 2000 || j < 0 || j > 2000) break;
		if (flag == 1)
		{
			if (a[i + 1][j])
				a[i][++j] = cnt++;
			else if (!a[i + 1][j])
				a[++i][j] = cnt++, flag = 2;
			else if (i == 0 && j == 2000)
				break;
		}
		else if (flag == 2)
		{
			if (a[i][j - 1])
				a[++i][j] = cnt++;
			else if (!a[i][j - 1])
				a[i][--j] = cnt++, flag = 3;
		}
		else if (flag == 3)
		{
			if (a[i - 1][j])
				a[i][--j] = cnt++;
			else if (!a[i - 1][j])
				a[--i][j] = cnt++, flag = 4;
		}
		else if (flag == 4)
		{
			if (a[i][j + 1])
				a[--i][j] = cnt++;
			else if (!a[i][j + 1])
				a[i][++j] = cnt++, flag = 1;
		}
	}
}

int main()
{
	int x, y;
	make();
	while (cin >> x >> y)
	{
		x += 1000, y += 1000;
		cout << a[y][x] << endl;
	}
}

3298.     Prairie dogs V

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <queue>
#define INF (-1u) >> 1
#define MAXN 1010
using namespace std;

char map[MAXN][MAXN];
int v[MAXN][MAXN];
int dir[4][2] = {1, 0, -1, 0, 0, 1, 0, -1};
int x1, y1, x2, y2, m, n, ans;

struct node
{
	int x, y, tt;
	bool operator < (const node & a) const
	{
		return tt > a.tt;
	}
} test, now;

void BFS()
{
	x1--, x2--; y1--; y2--;
	priority_queue <node> Q;
	//int tx, ty, tt, vx, vy, vv;
	int vx, vy, vv;
	ans = INF;
	memset(v, 0, sizeof(v));
	test.x = x1; test.y = y1; test.tt = 0;
	Q.push(test);
	v[x1][y1] = 1;
	while(!Q.empty())
	{
		//tx = Q.front(); Q.pop();
		//ty = Q.front(); Q.pop();
		//tt = Q.front(); Q.pop();
		now = Q.top(); Q.pop();
		if (now.x == x2 && now.y == y2)  ans = min(ans, now.tt);
		for(int i = 0; i < 4; i++)
		{
			vx = now.x + dir[i][0];
			vy = now.y + dir[i][1];
			if(vx < 0 || vx >= m || vy < 0 || vy >= n || v[vx][vy]) continue;
			v[vx][vy] = 1;
			if (map[vx][vy] == '.')
				vv = now.tt + 1;
			else vv = now.tt;
			test.x = vx; test.y = vy; test.tt = vv;
			Q.push(test);
		}
	}
}

int main()
{
	while (cin >> m >> n, m + n)
	{
		memset(map, 0, sizeof(map));
		for (int i = 0; i < m; i++)
			for (int j = 0; j < n; j++)
				cin >> map[i][j];
		cin >> x1 >> y1;
		cin >> x2 >> y2;
		BFS();
		cout << ans << endl;
	}
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值