60pts求助 WA on#3#4
查看原帖
60pts求助 WA on#3#4
1287433
__ycy1124__楼主2024/11/18 19:14
#include<bits/stdc++.h>
using namespace std;
char a[31][31];
bool bj[31][31];
int ans[31][31][4];
struct Node{
	int x,y,fx,w;
};
queue<Node>q;
int p1[4]={0,-1,0,1},p2[4]={1,0,-1,0};
void bfs(int x,int y,int fx,int w){
//	cout<<x<<' '<<y<<' '<<fx<<' '<<w<<'\n';
	if(w>ans[x][y][fx]){
		return;
	}
	ans[x][y][fx]=w;
	bool qwq=0;
	if(bj[x+p1[fx]][y+p2[fx]]&&w<ans[x+p1[fx]][y+p2[fx]][fx]){
		ans[x+p1[fx]][y+p2[fx]][fx]=w;
		q.push({x+p1[fx],y+p2[fx],fx,w});
		qwq=1;
	}
	if(bj[x+p1[(fx+1)%4]][y+p2[(fx+1)%4]]&&w+1<ans[x+p1[(fx+1)%4]][y+p2[(fx+1)%4]][(fx+1)%4]){
		ans[x+p1[(fx+1)%4]][y+p2[(fx+1)%4]][(fx+1)%4]=w+1;
		q.push({x+p1[(fx+1)%4],y+p2[(fx+1)%4],(fx+1)%4,w+1});
		qwq=1;
	}
	if(bj[x+p1[(fx-1+4)%4]][y+p2[(fx-1+4)%4]]&&w+5<ans[x+p1[(fx-1+4)%4]][y+p2[(fx-1+4)%4]][(fx-1+4)%4]){
		ans[x+p1[(fx-1+4)%4]][y+p2[(fx-1+4)%4]][(fx-1+4)%4]=w+5;
		q.push({x+p1[(fx-1+4)%4],y+p2[(fx-1+4)%4],(fx-1+4)%4,w+5});
		qwq=1;
	}
	if(!qwq){
		if(w+10<ans[x+p1[(fx-2+4)%4]][y+p2[(fx-2+4)%4]][(fx-2+4)%4]){
			ans[x+p1[(fx-2+4)%4]][y+p2[(fx-2+4)%4]][(fx-2+4)%4]=w+10;
			q.push({x+p1[(fx-2+4)%4],y+p2[(fx-2+4)%4],(fx-2+4)%4,w+10});
		}
	}
}
int main(){
	ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
	int h,w;
	cin>>h>>w;
	int sx,sy,tx,ty,fx;
	for(int i=1;i<=h;i++){
		for(int j=1;j<=w;j++){
			cin>>a[i][j];
			if(a[i][j]=='E'){
				fx=0,sx=i,sy=j;
			}
			else if(a[i][j]=='W'){
				fx=2,sx=i,sy=j;
			}
			else if(a[i][j]=='N'){
				fx=3,sx=i,sy=j;
			}
			else if(a[i][j]=='S'){
				fx=1,sx=i,sy=j;
			}
			if(a[i][j]=='F'){
				tx=i,ty=j;
			}
			if(a[i][j]!='.'){
				bj[i][j]=1;
			}
			for(int k=0;k<=3;k++){
				ans[i][j][k]=1e17;
			}
		}
	}
	q.push({sx,sy,fx,0});
	while(!q.empty()){
		bfs(q.front().x,q.front().y,q.front().fx,q.front().w);
		q.pop();
	}
	int Ans=1e17;
//	for(int i=1;i<=h;i++){
//		for(int j=1;j<=w;j++){
//			cout<<bj[i][j];
//		}
//		cout<<'\n';
//	}
//	for(int i=1;i<=h;i++){
//		int qwq=1e9;
//		for(int j=1;j<=w;j++){
//			for(int k=0;k<=3;k++){
//				qwq=min(qwq,ans[i][j][k]);
//			}
//			cout<<qwq<<' ';
//		}
//		cout<<'\n';
//	}
	for(int i=0;i<=3;i++){
		Ans=min(Ans,ans[tx][ty][i]);
	}
	cout<<Ans;
	return 0;
}
2024/11/18 19:14
加载中...