救我QAQ
查看原帖
救我QAQ
705058
BGM114514楼主2024/8/22 16:00

样例没过,题解的思路和我写的不一样,呜呜呜……

提交记录

#include<bits/stdc++.h>
using namespace std;
int mx[]={0,1,0,-1};
int my[]={1,0,-1,0};
struct node{
	int x,y,time,to;
	bool turn;
};
bool mp[55][55],vis[55][55][4];
int n,m,sx,sy,ex,ey,f,ans=0x3f3f3f3f;
bool check(int x,int y,int to){
	return (x>=1&&x<n&&y>=1&&y<m&&vis[x][y][to]==0&&!mp[x][y]);
}
void bfs(int fa,int tt){
	queue<node> q;
	q.push({sx,sy,tt,fa,0});
	vis[sx][sy][f]=1;
	while(!q.empty()){
		node head=q.front();
		q.pop();
		int x=head.x,y=head.y,t=head.time;
		if(t>=ans)continue;
		if(x==ex&&y==ey){
			ans=min(ans,t);
			continue;
		}
		int l=(head.to+3)%4,r=(head.to+1)%4;
		if(!head.turn){
			vis[x][y][l]=1;
			q.push({x,y,t+1,l,1});
			vis[x][y][r]=1;
			q.push({x,y,t+1,r,1});
		}
		for(int i=1;i<=3;i++){
			int h=x+mx[head.to]*i,l=y+my[head.to]*i;
			if(check(h,l,head.to)){
				vis[h][l][head.to]=1;
				q.push({h,l,t+1,head.to,0});
			}
		}
	}
}
int main(){
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	cin>>n>>m;
	for(int i=1;i<=n;i++){
		for(int j=1;j<=m;j++){
			bool p;
			cin>>p;
			if(p){
				mp[i][j]=mp[i-1][j]=mp[i][j-1]=mp[i-1][j-1]=1;
			}
		}
	}
	cin>>sx>>sy>>ex>>ey;
	char ch;
	cin>>ch;
	if(ch=='E')f=0;
	if(ch=='S')f=1;
	if(ch=='W')f=2;
	if(ch=='N')f=3;
	bfs(f,0);
	bfs((f+3)%4,1);
	bfs((f+1)%4,1);
	bfs((f+2)%4,2);
	if(ans==0x3f3f3f3f)cout<<-1<<endl;
	else cout<<ans<<endl;
	return 0;
} 
2024/8/22 16:00
加载中...