每日一题4.16.1

每日一题4.16.1

红与黑

在这里插入图片描述
在这里插入图片描述
代码实现:

#include<iostream>
#include<vector>
#include<queue>
#include<string>
using namespace std;
int dest[4][2] = { 1, 0, 0, 1, 0, -1, -1, 0 };
struct node{
	int x;
	int y;
};
int main()
{
	int m, n;
	while (cin >> m >> n)
	{
		vector<vector<char>> room(m, vector<char>(n));
		int a, b;
		for (int i = 0; i < m; i++)
		{
			for (int j = 0; j < n; j++)
			{
				cin >> room[i][j];
				if (room[i][j] == '@')
				{
					a = i;
					b = j;
				}

			}
		}
		queue<node> p;
		int result = 1;
		node cur;
		cur.x = a;
		cur.y = b;
		p.push(cur);
		while (!p.empty())
		{
			node tmp = p.front();
			p.pop();
			for (int i = 0; i < 4; i++)
			{
				node next;
				next.x = tmp.x + dest[i][0];
				next.y = tmp.y + dest[i][1];
				if (next.x < m && next.x >= 0 && next.y < n && next.y >= 0 && room[next.x][next.y] == '.')
				{
					result++;
					p.push(next);
					room[next.x][next.y] = '@';
				}
			}
			
		}
		cout << result << endl;
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/lxb18821659801/article/details/89515698