救救孩子吧
被遗忘的帖子qwq
#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];
//p代表的pre(指定点的先行官是空白点?我起变量名是这样的qwq
struct ndp{int x,y,stp;};
int bfsp(int tx,int ty,int sx,int sy,int ex,int ey){//空白点
//(tx,ty):指定点位置 (sx,sy):空白点起点 (ex,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到达该位置时空白点位置
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;
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);
//ansp=空白点移动到【指定点下一步要移动的位置】所需的步数
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
总是比答案稍微大一点,不知道咋回事
求大佬查错cccccorz