其他分享
首页 > 其他分享> > POJ - 1979

POJ - 1979

作者:互联网

题目:

Red and Black - POJ 1979 - Virtual Judge

分析:

基础BFS。

统计BFS搜索到的点的个数。

使用知识:BFS,模拟队列。

代码:

#include <iostream>
#include <cstring>
#include <algorithm>
#include <cstdio>

using namespace std;

const int N = 100;

struct Point
{
	int x;
	int y;
}q[10000];

int n, m;
char road[N][N];
bool vis[N][N];
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};

int bfs(int a, int b)
{
	int hh = 0, tt = 0, cnt = 0;
	q[0] = {a, b};
	vis[a][b] = true;
	
	while (hh <= tt)
	{
		Point t = q[hh ++];
		
		for (int i = 0; i < 4; i ++ )
		{
			int xx = t.x + dx[i];
			int yy = t.y + dy[i];
			if (xx >= 0 && xx < n && yy >= 0 && yy < m && !vis[xx][yy] &&
			road[xx][yy] == '.')
			{
				vis[xx][yy] = true;
				q[++ tt] = {xx, yy};
				cnt ++ ;
			}
		}
	}
	
	return cnt;
}

int main()
{
	while (scanf("%d%d", &m, &n))
	{
		if (n == 0 && m == 0) break;
		
		memset(vis, 0, sizeof(vis));
		memset(road, 0, sizeof(road));
		
		for (int i = 0; i < n; i ++ )
			scanf("%s", road[i]);
			
		for (int i = 0; i < n; i ++ )
		{
			for (int j = 0; j < m; j ++ )
			{
				if (road[i][j] == '@')
				cout << bfs(i, j) + 1 << endl;
			}
		}
	}
    return 0;
}

标签:1979,int,yy,vis,xx,POJ,&&,road
来源: https://blog.csdn.net/weixin_60484917/article/details/122489385