求助!为什么第5个点会WA啊?
查看原帖
求助!为什么第5个点会WA啊?
371278
herselfqwq楼主2020/12/29 17:42

RT,感谢各位大佬

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
long long n,m,sx,sy,mx,my,fx,fy,ans,head,tail,book[1005][505],Book[1005][505],step;
char mp[1005][1005],c;
int dx[4]={-1,0,1,0};
int dy[4]={0,1,0,-1};
struct walk {
	long long x;
	long long y;
	long long bushu;
}queue[10000005],Queue[10000005];
int main () {
    ios::sync_with_stdio(0);
    cin>>n>>m;
    for (int i=1;i<=n;i++) {
    	for (int j=1;j<=m;j++) {
    		cin>>mp[i][j];
    		if (mp[i][j]=='S') {
    			sx=i;
    			sy=j;
			}
			if (mp[i][j]=='C') {
				mx=i;
				my=j;
			}
			if (mp[i][j]=='G') {
				fx=i;
				fy=j;
			}
		}
	}
	head=1,tail=1;
	queue[tail].x = sx;
	queue[tail].y = sy;
	queue[tail].bushu = 0;
	tail++;
	book[sx][sy]=1;
	while (head<tail) {
		for (int i=0;i<4;i++) {
			int xx=queue[head].x+dx[i];
			int yy=queue[head].y+dy[i];
			if (xx>=1&&xx<=n&&yy>=1&&yy<=m&&mp[xx][yy]=='C'&&xx==mx&&yy==my) {
				step=queue[head].bushu+1;
				head=1,tail=1;
				Queue[head].x=mx;
				Queue[head].y=my;
				Queue[head].bushu = step;
				Book[mx][my]=1;
				tail++;
//				cout<<step<<endl;
				break;
			}
			if (xx>=1&&xx<=n&&yy>=1&&yy<=m&&mp[xx][yy]=='.'&&book[xx][yy]==0) {
				book[xx][yy]=1;
				queue[tail].x=xx;
				queue[tail].y=yy;
				queue[tail].bushu = queue[head].bushu + 1;
				tail++;
			}
		}
		head++;
	}
	head=1;
//	cout<<head<<" "<<tail<<endl;
	Queue[head].bushu = step;
//	cout<<Queue[head].x<<Queue[head].y<<Queue[head].bushu<<endl;
	while (head<tail) {
		for (int i=0;i<4;i++) {
			int xx=Queue[head].x+dx[i];
			int yy=Queue[head].y+dy[i];
			if (xx>=1&&xx<=n&&yy>=1&&yy<=m&&xx==fx&&yy==fy) {
				ans=Queue[head].bushu+1;
				cout<<ans<<endl;
				return 0;
			}
			if (xx>=1&&xx<=n&&yy>=1&&yy<=m&&mp[xx][yy]=='.'&&Book[xx][yy]==0) {
//				cout<<xx<<" "<<yy<<endl;
				Book[xx][yy]=1;
				Queue[tail].x=xx;
				Queue[tail].y=yy;
				Queue[tail].bushu = Queue[head].bushu + 1;
				tail++;
			}
		}
		head++;
	}
	cout<<"-1"<<endl;
	return 0;
}
2020/12/29 17:42
加载中...