二十分求助
查看原帖
二十分求助
821607
zhueryueqin0852楼主2024/8/5 14:48
#include <bits/stdc++.h>
using namespace std;
int n,m,sx,sy,tx,ty,cnt;
char f;
int dir[3]={1,2,3},mp[10005][10005],mx[4]={0,1,2,3};
struct node{
	int x;
	int y;
	int front;
};
void bfs(int x,int y,int t){
	queue<node>q;
	node tmp={x,y};
	q.push(tmp);
	while(!q.empty()){
		tmp=q.front();
		q.pop();
		for(int j=0;j<4;j++){
			t=mx[j];
			int ux=tmp.x,uy=tmp.y,ut=tmp.front;
			if(ux==tx&&uy==ty)return ;
			cnt++;
			for(int i=0;i<3;i++){
				int nx=ux+dir[i];
				int ny=uy+dir[i];
				int nt=mx[j];
				if(nx>0&&nx<=n&&ny>0&&ny<=m&&mp[nx][ny]==0){
					mp[nx][ny]=1;
					tmp.front=nt,tmp.x=nx,tmp.y=ny;
					q.push(tmp);
				}
			}
		}
	}
	return ;
}
int main(){
	cin>>n>>m;
	int l;
	for(int i=1;i<=n;i++){
		for(int j=1;j<=m;j++){
			cin>>mp[i][j];
		}
	}
	cin>>sx>>sy>>tx>>ty>>f;
	mp[sx][sy]=1;
	if(f=='E')l=1;
	if(f=='S')l=2;
	if(f=='W')l=3;
	if(f=='N')l=0;
	bfs(sx,sy,l);
	cout<<cnt;
	return 0;
}
2024/8/5 14:48
加载中...