样例不过求条
查看原帖
样例不过求条
991587
zhoujunchen楼主2025/7/25 09:15
#include<bits/stdc++.h>
using namespace std;
#define int long long
const int tx[]={-1,0,1,0};
const int ty[]={0,1,0,-1};
int n,m,a[91][91],sx,sy,ex,ey,fxx;
char ch;
map<int,int> vis;
int hashh(int a,int b,int c){
	return a*1e9+b*1e4+c;
}
bool bd(int x,int y){
	if(a[x][y]||a[x+1][y]||a[x][y+1]||a[x+1][y+1])return 0;
	return 1;
}
struct node{
	int x,y,cost,fx;
};
void bfs(){
	queue<node> q;
	q.push({sx,sy,0,fxx});
	while(q.size()){
		node tmp=q.front();
		int x=tmp.x,y=tmp.y,cost=tmp.cost,fx=tmp.fx;
		int d=hashh(x,y,fx);
		q.pop();
		
		if(x==ex&&y==ey)return cout<<cost,void();
		
		if(vis[d])continue;
		vis[d]=1;
		
		cout<<x<<" "<<y<<" "<<fx<<" "<<cost<<"\n";
		
		
		fx=(fx-1+4)%4;
		q.push({x,y,cost+1,fx});
		fx=tmp.fx;
		
		fx=(fx+1+4)%4;
		q.push({x,y,cost+1,fx});
		fx=tmp.fx;
		
		
		
		for(int i=1;i<=3;i++){
			int xx=x+tx[fx]*i,yy=y+ty[fx]*i;
			if(xx>=1&&xx<n&&yy>=1&&yy<m&&bd(xx,yy))q.push({xx,yy,cost+1,fx});
		}
	}
}
signed main(){
	ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
	cin>>n>>m;
	for(int i=1;i<=n;i++)for(int j=1;j<=m;j++)cin>>a[i][j];
	cin>>sx>>sy>>ex>>ey>>ch;
	if(ch=='N')fxx=0;
	if(ch=='E')fxx=1;
	if(ch=='S')fxx=2;
	if(ch=='W')fxx=3;
	//cout<<sx<<" "<<sy<<" "<<ex<<" "<<ey<<" "<<fxx<<"\n"; 
	bfs();
	return 0;
}
2025/7/25 09:15
加载中...