有T有W,求查错qwq
查看原帖
有T有W,求查错qwq
432491
ThinkLongLong楼主2023/8/11 12:50
#include<bits/stdc++.h>
using namespace std;
const int N=35,inf=0x3f3f3f3f;
int n,m,q;
int g[N][N];
int dx[]={0,-1,0,1};
int dy[]={-1,0,1,0};

int visp[N][N];
struct ndp{int x,y,stp;};
int bfsp(int tx,int ty,int sx,int sy,int ex,int ey){
	queue<ndp> q;
	memset(visp,0,sizeof visp);
	q.push(ndp{sx,sy,0});visp[sx][sy]=1;
	visp[tx][ty]=1;
	while(q.size()){
		ndp f=q.front();q.pop();
		int x=f.x,y=f.y,stp=f.stp;
//		cout<<x<<' '<<y<<' '<<stp<<endl;
		if(x==ex&&y==ey)return stp;
		for(int i=0;i<4;i++){
			int xx=x+dx[i],yy=y+dy[i];
			if(!g[xx][yy]||visp[xx][yy])continue;
			visp[xx][yy]=1;
			q.push(ndp{xx,yy,stp+1});
		}
	}
	return -1;
}

int vis[N][N];
struct nd{int x,y,px,py,stp;//x,y 位置  px,py 到达该位置时p位置 
	bool operator< (const nd &y) const {return stp>y.stp;}
};
int bfs(int sx,int sy,int tx,int ty,int ex,int ey){
	priority_queue<nd> q;
	memset(vis,0,sizeof vis);
	q.push(nd{sx,sy,tx,ty,0});vis[sx][sy]=1;
	while(q.size()){
		nd f=q.top();q.pop();
		int x=f.x,y=f.y,px=f.px,py=f.py,stp=f.stp;
//		cout<<x<<' '<<y<<' '<<px<<' '<<py<<' '<<stp<<endl;
		if(x==ex&&y==ey){return stp;}
		for(int i=0;i<4;i++){
			int xx=x+dx[i],yy=y+dy[i];
			if(!g[xx][yy]||vis[xx][yy])continue;
			int ansp=bfsp(x,y,px,py,xx,yy);
			if(ansp==-1)continue;
			q.push(nd{xx,yy,x,y,stp+ansp+1});
			vis[xx][yy]=1;
		}
	}
	return -1;
}
int main(){
	cin>>n>>m>>q;
	for(int i=1;i<=n;i++)
		for(int j=1;j<=m;j++)
			scanf("%d",&g[i][j]);
	int tx,ty,sx,sy,ex,ey;
	while(q--){
		scanf("%d%d%d%d%d%d",&tx,&ty,&sx,&sy,&ex,&ey);
		printf("%d\n",bfs(sx,sy,tx,ty,ex,ey));
	} 
	return 0;
}

这是提交结果
尤其想知道为啥会wa
调了一天了还是不对

10 20 1
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0
1 0 1 1 1 0 1 1 1 0 1 1 1 0 1 1 1 0 1 1
1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0
1 1 1 0 1 0 1 0 1 0 1 1 1 0 1 0 1 0 1 0
1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0
1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0
1 1 1 0 1 0 1 0 1 1 1 0 1 0 1 0 1 0 1 0
1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0
1 1 1 0 1 1 1 0 1 1 1 0 1 1 1 0 1 1 1 1
1 4 10 9 10 1

上面这组输出就不对,应该是2651,我输出2653
总是比答案稍微大一点,不知道咋回事
求大佬查错orz

2023/8/11 12:50
加载中...