40分广搜求助
查看原帖
40分广搜求助
547725
Zilljy258楼主2023/10/20 19:02

我的代码

#include<iostream>
#include<cstdio>
#include<cstring>

using namespace std;

int n,m;
bool vis[60][60][4];
int dx[12]= {-1,0,1,0,-2,0,2,0,-3,0,3,0};
int dy[12]= {0,1,0,-1,0,2,0,-2,0,3,0,-3};
int fw[12]= {0,1,2,3,0,1,2,3,0,1,2,3};
bool ff[12];
int mx,my;
int h,t;

struct queue {
	int xx;
	int yy;
	int fc;
	int st;
} q[400000];

int aabs(int x){
	return x>0 ? x : -x;
}

int bfs() {
	while(t-h) {
		if(q[h].xx==mx&&q[h].yy==my) return q[h].st;
		memset(ff,false,sizeof(ff));
		for(int i=0; i<12; ++i) {
			if(ff[i]==true){
				continue;
			}
			if(aabs(fw[i]-q[h].fc)==2) continue;
			if(q[h].fc==fw[i]) {
				if(q[h].xx+dx[i]<n&&q[h].yy+dy[i]<m&&q[h].xx+dx[i]>0&&q[h].yy+dy[i]>0&&!vis[q[h].xx+dx[i]][q[h].yy+dy[i]][q[h].fc]) {
					q[t].fc=q[h].fc;
					q[t].xx=q[h].xx+dx[i];
					q[t].yy=q[h].yy+dy[i];
					q[t].st=q[h].st+1;
					vis[q[t].xx][q[t].yy][q[t].fc]=true;
					t++;
				}
				else ff[i%4+8]=true;
			} else if(!vis[q[h].xx][q[h].yy][fw[i]]) {
				q[t].fc=fw[i];
				q[t].xx=q[h].xx;
				q[t].yy=q[h].yy;
				q[t].st=q[h].st+1;
				vis[q[t].xx][q[t].yy][q[t].fc]=true;
				t++;
			}
			else{
				ff[i%4+8]=true;
			}
		}
		h++;
	}
	return -1;
}

int main() {
	memset(vis,false,sizeof(vis));
	int x;
	scanf("%d%d",&n,&m);
	for(int i=1; i<=n; ++i) {
		for(int j=1; j<=m; ++j) {
			scanf("%d",&x);
			if(x) {
				for(int k=0; k<4; ++k)
					vis[i][j][k]=vis[i-1][j][k]=vis[i][j-1][k]=vis[i-1][j-1][k]=true;
			}
		}
	}

	int xxx,yyy;
	char ffw;
	scanf("%d%d%d%d",&xxx,&yyy,&mx,&my);
	
	cin>>ffw;

	h=t=0;
	q[t].xx=xxx;
	q[t].yy=yyy;
	q[t].st=0;
	if(ffw=='N') q[t].fc=0;
	if(ffw=='E') q[t].fc=1;
	if(ffw=='S') q[t].fc=2;
	if(ffw=='W') q[t].fc=3;
	vis[q[t].xx][q[t].yy][q[t].fc]=true;
	t++;

	printf("%d",bfs());

	return 0;
}

样例输入 #1

9 10
0 0 0 0 0 0 1 0 0 0
0 0 0 0 0 0 0 0 1 0
0 0 0 1 0 0 0 0 0 0
0 0 1 0 0 0 0 0 0 0
0 0 0 0 0 0 1 0 0 0
0 0 0 0 0 1 0 0 0 0
0 0 0 1 1 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
1 0 0 0 0 0 0 0 1 0
7 2 2 7 S

样例输出 #1

12

我输出 13 QAQ

谢谢!

2023/10/20 19:02
加载中...