求指导!!!

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int n,m,p;
int map[35][35];
int dis[35][35][4][4];
int xx[100000],yy[100000],zz[100000];
int movex[4]={-1,1,0,0};
int movey[4]={0,0,-1,1};
int vis[35][35];
struct node{
    int x,y,kx,ky,step;
}q[8100000];
int ex,ey,sx,sy,tx,ty;
int book[35][35][35][35];
int ans;

void bfs(int x,int y,int from,int to){
    memset(vis,0,sizeof(vis));
    memset(xx,0,sizeof(xx));
    memset(yy,0,sizeof(yy));
    memset(zz,0,sizeof(zz));
    int stax=x+movex[from];
    int stay=y+movey[from];
    int newx=x+movex[to];
    int newy=y+movey[to];
    
    if(stax<1||stax>n||stay<1||stay>m) return;
    if(newx<1||newx>n||newy<1||newy>m) return;
    
    int head=0,tail=1;
    xx[0]=stax; yy[0]=stay; zz[0]=0;
    vis[stax][stay]=1;
    while(head<tail){
        
        
        for(int i=0;i<4;i++){
            int nx=xx[head]+movex[i];
            int ny=yy[head]+movey[i];
            if(nx<1||nx>n||ny<1||ny>m) continue;
            if(!map[nx][ny]) continue;
            if(nx==x&&ny==y) continue;
            
            if(!vis[nx][ny]){
                vis[nx][ny]=1;
                xx[tail]=nx;
                yy[tail]=ny;
                zz[tail]=zz[head]+1;
                if(nx==newx&&ny==newy){
                    dis[x][y][from][to]=zz[tail];
                    return;
                }
                tail++;
            }
        }
        head++;
    }
}

void BFS(){
    
    int head=0,tail=1;
    q[0].x=sx; q[0].y=sy; q[0].kx=ex; q[0].ky=ey; q[0].step=0;//kx,ky为空白格的位置,x,y为目标方格 
    book[sx][sy][ex][ey]=1;
    
    while(head<tail){
        for(int i=0;i<4;i++){
            
            
            int kx=q[head].kx,ky=q[head].ky;
            
            int x=q[head].x,y=q[head].y;
            
            int newx=kx+movex[i],newy=ky+movey[i];
            
            if(newx<1||newx>n||newy<1||newy>m) continue;
            if(!map[newx][newy]) continue;
            if(book[x][y][newx][newy]) continue;
            
            
            
            if(newx==x&&newy==y){                         // 当在目标方格周围时 
                q[tail].x=kx;q[tail].y=ky;q[tail].kx=x;q[tail].ky=y;
                q[tail].step=q[head].step+1;//先交换 
                book[kx][ky][x][y]=1;
                if(kx==tx&&ky==ty){
                    ans=min(ans,q[tail].step);
                    continue;
                }           //再比较答案 
                tail++;                             
                int nx=kx,ny=ky,nz=q[head].step+1;   //当前目标格 
                for(int k=0;k<4;k++){
                                             //迅速移动 
                    int nxx=nx+movex[k],nyy=ny+movey[k];
                    if(nxx<1||nxx>n||nyy<1||nyy>m) continue;
                    if(!map[nxx][nyy]) continue;
                    if(book[nx][ny][nxx][nyy]) continue;
                    q[tail].x=nx,q[tail].y=ny;q[tail].kx=nxx;
                    q[tail].ky=nyy;q[tail].step+=dis[nx][ny][i][k]+nz;
                    book[nx][ny][nxx][nyy]=1;
                    tail++;
                    
                }
            }
            
            else {
                q[tail].x=x;q[tail].y=y;q[tail].kx=newx;q[tail].ky=newy;
                q[tail].step=q[head].step+1;
                book[x][y][newx][newy]=1;
                tail++;
            }
        }
        head++;
    }
}

int main(){
    freopen("H.in","r",stdin);
    //freopen("H.out","w",stdout);
    
    int i,j,k,l;
    scanf("%d %d %d",&n,&m,&p);
    for(i=1;i<=n;i++){
        for(j=1;j<=m;j++){
            scanf("%d",&map[i][j]);
        }
    }//读入 
    
    for(i=1;i<=n;i++){
        for(j=1;j<=m;j++){
            if(map[i][j]){
                for(k=0;k<4;k++){
                    for(l=0;l<4;l++){
                        bfs(i,j,k,l);
                        /*printf("**%d %d %d %d %d**\n",i,j,k,l,dis[i][j][k][l]);*/
                    }
                }
            }
        }
    }//预处理 
    
    for(i=1;i<=p;i++){
        scanf("%d %d %d %d %d %d",&ex,&ey,&sx,&sy,&tx,&ty);
        memset(book,0,sizeof(book));
        ans=99999999;
        BFS();
        if(ans==99999999) printf("-1\n");
        else printf("%d\n",ans);
    }
    
    return 0;
}

0 条评论

目前还没有评论...

信息

ID
1846
难度
8
分类
(无)
标签
递交数
2682
已通过
380
通过率
14%
被复制
9
上传者