求助bfs解法
查看原帖
求助bfs解法
1529685
JSJ_WuYeHao楼主2024/12/14 15:55
#include <iostream>
#include <cstring>
#include <algorithm>
#include <queue>

using namespace std;

typedef pair<int, int> PII;

const int N = 110;

int n, m;
char g[N][N];
int d[N][N];

bool bfs()
{
	queue<PII> a;
	memset(d, -1, sizeof(d));
	d[0][0] = 0;
	a.push({ 0,0 });
	int dx[4] = { 0,0,1,-1 }, dy[4]={ 1,-1,0,0 };
	while (a.size())
	{
		auto t = a.front();
		a.pop();
		for (int i = 0; i < 4; i++)
		{
			int x = t.first + dx[i], y = t.second + dy[i];
			if (x >= 0 && y >= 0 && x < n  && y < m  && g[x][y] == '.' && d[x][y] == -1)
			{
				d[x][y] = d[t.first][t.second] + 1;
				a.push({ x,y });
			}
		}
	}
	if (d[n - 1][m - 1] == -1)return false;
	return true;
}

int main()
{
	cin >> n >> m;
	for (int i = 0; i < n; i++)
		for (int j = 0; j < m; j++)
			cin >> g[i][j];

	if (bfs())cout << "Yes";
	else cout << "No";
	return 0;
}

为什么我的全错了

2024/12/14 15:55
加载中...