社区讨论

BFS 输出一致WA10?

P2960[USACO09OCT] Invasion of the Milkweed G参与者 1已保存回复 0

讨论操作

快速查看讨论及其快照的属性,并进行相关操作。

当前回复
0 条
当前快照
1 份
快照标识符
@lo14b4vl
此快照首次捕获于
2023/10/22 14:58
2 年前
此快照最后确认于
2023/11/02 14:30
2 年前
查看原帖
CPP
#include <bits/stdc++.h>
using namespace std;
const int N=200;
typedef pair<pair<int,int>,int> PIII;
#define x first
#define y second
int n,m,sx,sy;
char g[N][N];
bool st[N][N];
int dx[]={-1,0,1,0},dy[]={0,1,0,-1};
int bfs(){
	int res=0;
	queue<PIII> q;
	q.push({{sx,sy},0});
	while(q.size()){
		PIII t=q.front();
		q.pop();
		int x=t.x.x,y=t.x.y,time=t.y;
		res=max(res,time);
		for(int i=0;i<4;i++)
			for(int j=0;j<4;j++){
				int nx=x+dx[i],ny=y+dy[j];
				if(nx<1||nx>n||ny<1||ny>m) continue;
				if(g[nx][ny]=='*') continue;
				if(st[nx][ny]) continue;
				st[nx][ny]=true;
				q.push({{nx,ny},time+1});
			}
	}
	return res;
}
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);cout.tie(0);
	cin>>m>>n>>sy>>sx;
	for(int i=1;i<=n;i++)
		for(int j=1;j<=m;j++)
			cin>>g[i][j];
	st[sx][sy]=true;
	cout<<bfs()<<endl;
	return 0; 
}
我下载数据测试是73(图片不会放┭┮﹏┭┮)

回复

0 条回复,欢迎继续交流。

正在加载回复...