B3625求助
  • 板块题目总版
  • 楼主_guoyan
  • 当前回复2
  • 已保存回复2
  • 发布时间2023/10/4 21:47
  • 上次更新2023/11/2 15:39:43
查看原帖
B3625求助
635963
_guoyan楼主2023/10/4 21:47

Rt,BFS做的,除第一个点以外全部TLE

#include<bits/stdc++.h>
using namespace std;
bool vis[105][105];
char mp[105][105];
int n,m;
queue<pair<int,int> > q;
void bfs()
{
	while(!q.empty())
	{
		pair t=q.front();
		q.pop();
		vis[t.first][t.second]=1;
		if(t.first==n&&t.second==m)break;
		mp[t.first][t.second]='#';
		if(mp[t.first+1][t.second]=='.')q.push({t.first+1,t.second});
		if(mp[t.first][t.second+1]=='.')q.push({t.first,t.second+1});
		if(mp[t.first][t.second-1]=='.')q.push({t.first,t.second-1});
		if(mp[t.first-1][t.second]=='.')q.push({t.first-1,t.second});
	}
	return;
}
int main()
{
	cin >> n >> m;
	for(int i = 1; i <= n; i++)
	{
		for(int j = 1; j <= m; j++)
		{
			cin >> mp[i][j];
		}
	}
	q.push({1,1});
	bfs();
	if(vis[n][m])cout << "Yes";
	else cout << "No";
	return 0;
}
2023/10/4 21:47
加载中...