ZOJ2165

2 篇文章 0 订阅

还是广度优先搜索,和1091差不多。

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <queue>
using namespace std;

int main()
{
    int p[21][21];
    char c;
    int m, n;
    while (scanf("%d%d", &n, &m)!=EOF)
    {
		if (n==0&&m==0)
		break;
		int i, j;
		queue<int> q;
		for (i=1; i<=m; i++)
		{
			getchar();
			for (j=1; j<=n; j++)
		    {
				scanf("%c", &c);
				if (c=='.')
				p[i][j]=0;
				else if (c=='@')    /*出发点*/
				{
					q.push(i);
					q.push(j);
					p[i][j]=1;
				}
				else if (c=='#')    /*标记为1即不能再走*/
				p[i][j]=1;
			}
		}
		j=0;                 /*计数走过的black tile*/
		while (!q.empty())    /*当队列为空时所有可走的black tile都走过了*/
		{
			int x, y;
			x = q.front();
			q.pop();
			y = q.front();
			q.pop();
			j++;
			if (x-1>=1&&p[x-1][y]==0)
			{
				p[x-1][y]=1;
				q.push(x-1);
				q.push(y);
			}
			if(y+1<=n&&p[x][y+1]==0)
			{
				p[x][y+1]=1;
				q.push(x);
				q.push(y+1);
			}
			if (x+1<=m&&p[x+1][y]==0)
			{
				p[x+1][y]=1;
				q.push(x+1);
				q.push(y);
			}
			if(y-1>=1&&p[x][y-1]==0)
			{
				p[x][y-1]=1;
				q.push(x);
				q.push(y-1);
			}
		}
		printf("%d\n", j);
	}
    return 0;
}


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值