12 分求助,WA on #3
查看原帖
12 分求助,WA on #3
903392
T7_Daniel楼主2023/11/16 16:54
#include<bits/stdc++.h>
using namespace std;
char mp[25][25];
int n,m; 
struct node{
	int x,y;
	int step;
};
bool vis[25][25];
void solve(){
	cin>>n>>m;
	for(int i=1;i<=n;i++){
		for(int j=1;j<=m;j++){
			cin>>mp[i][j];
		}
	} 
	queue<node> q;
	q.push({1,1,1});
	while(!q.empty()){
		node t=q.front();q.pop();
		int x=t.x,y=t.y,step=t.step;
		if(x==n && y==m){
			cout<<t.step<<endl;
			return;
		}
		if(x<1 || x>n || y<1 || y>m) continue;
		if(vis[x][y]==1) continue;
		vis[x][y]=1;
		if(mp[x][y]=='+'){
			q.push({x-1,y,step+1});
			q.push({x+1,y,step+1});
			q.push({x,y-1,step+1});
			q.push({x,y+1,step+1});
		}
		else if(mp[x][y]=='-'){
			q.push({x,y-1,step+1});
			q.push({x,y+1,step+1});
		}
		else if(mp[x][y]=='|'){
			q.push({x-1,y,step+1});
			q.push({x+1,y,step+1});
		}
	}
	cout<<-1<<endl;
}
int main(){
	int t;cin>>t;
	while(t--){
		solve();
		memset(vis,0,sizeof vis);
	}
	return 0;
}
2023/11/16 16:54
加载中...