华容道

题目描述

【问题描述】

小 B 最近迷上了华容道,可是他总是要花很长的时间才能完成一次。于是,他想到用编程来完成华容道:给定一种局面, 华容道是否根本就无法完成,如果能完成, 最少需要多少时间。

小 B 玩的华容道与经典的华容道游戏略有不同,游戏规则是这样的:

  1. 在一个 n*m 棋盘上有 n*m 个格子,其中有且只有一个格子是空白的,其余 n*m-1个格子上每个格子上有一个棋子,每个棋子的大小都是 1*1 的;

  2. 有些棋子是固定的,有些棋子则是可以移动的;

  3. 任何与空白的格子相邻(有公共的边)的格子上的棋子都可以移动到空白格子上。

游戏的目的是把某个指定位置可以活动的棋子移动到目标位置。

给定一个棋盘,游戏可以玩 q 次,当然,每次棋盘上固定的格子是不会变的, 但是棋盘上空白的格子的初始位置、 指定的可移动的棋子的初始位置和目标位置却可能不同。第 i 次

玩的时候, 空白的格子在第 EXi 行第 EYi 列,指定的可移动棋子的初始位置为第 SXi 行第 SYi列,目标位置为第 TXi 行第 TYi 列。

假设小 B 每秒钟能进行一次移动棋子的操作,而其他操作的时间都可以忽略不计。请你告诉小 B 每一次游戏所需要的最少时间,或者告诉他不可能完成游戏。

输入输出格式

输入格式:

 

输入文件为 puzzle.in。

第一行有 3 个整数,每两个整数之间用一个空格隔开,依次表示 n、m 和 q;

接下来的 n 行描述一个 n*m 的棋盘,每行有 m 个整数,每两个整数之间用一个空格隔开,每个整数描述棋盘上一个格子的状态,0 表示该格子上的棋子是固定的,1 表示该格子上的棋子可以移动或者该格子是空白的。接下来的 q 行,每行包含 6 个整数依次是 EXi、EYi、SXi、SYi、TXi、TYi,每两个整数之间用一个空格隔开,表示每次游戏空白格子的位置,指定棋子的初始位置和目标位置。

 

输出格式:

 

输出文件名为 puzzle.out。

输出有 q 行,每行包含 1 个整数,表示每次游戏所需要的最少时间,如果某次游戏无法完成目标则输出−1。

 

输入输出样例

输入样例#1:
3 4 2
0 1 1 1
0 1 1 0
0 1 0 0
3 2 1 2 2 2
1 2 2 2 3 2
输出样例#1:
2
-1

说明

【输入输出样例说明】

棋盘上划叉的格子是固定的,红色格子是目标位置,圆圈表示棋子,其中绿色圆圈表示目标棋子。

  1. 第一次游戏,空白格子的初始位置是 (3, 2)(图中空白所示),游戏的目标是将初始位置在(1, 2)上的棋子(图中绿色圆圈所代表的棋子)移动到目标位置(2, 2)(图中红色的格子)上。

移动过程如下:

  1. 第二次游戏,空白格子的初始位置是(1, 2)(图中空白所示),游戏的目标是将初始位置在(2, 2)上的棋子(图中绿色圆圈所示)移动到目标位置 (3, 2)上。

要将指定块移入目标位置,必须先将空白块移入目标位置,空白块要移动到目标位置,必然是从位置(2, 2)上与当前图中目标位置上的棋子交换位置,之后能与空白块交换位置的只有当前图中目标位置上的那个棋子,因此目标棋子永远无法走到它的目标位置, 游戏无

法完成。

【数据范围】

对于 30%的数据,1 ≤ n, m ≤ 10,q = 1;

对于 60%的数据,1 ≤ n, m ≤ 30,q ≤ 10;

对于 100%的数据,1 ≤ n, m ≤ 30,q ≤ 500。

#include <cstdio>
#include <cstring>
#define MAXN 4000
#define gg(a,b,c)  a*120+b*4+c
#define INF 1e9
int n,m,qq;
int e[20010][3],p[4010],counter=0,ans;
int q[1010][2],dis[35][35],sq[4010],sdis[4010],head,tail;
short vis[4010],map[35][35]/*the chess map*/,dir[4][2] = {-1,0,1,0,0,-1,0,1};//0:x,1:y
template<class T>inline void cin(T&x){
    static char c;static int y;
    for(c=getchar(),x=0,y=1;c<48||57<c;c=getchar())if(c=='-')y=-1;
    for(;48<=c&&c<=57;c=getchar())x=((x+(x<<2))<<1)+(c^'0');
    x*=y;}//an interesing quick_reader
int min(int a,int b){return a<b?a:b;}//just don't want to use iostream base
void add_edge(int sn,int fn,int val){e[++counter][0]=fn,e[counter][1]=val,e[counter][2]=p[sn],p[sn]=counter;}//e[c][0]is the before,e[c][2]is the next
void bfs(int si,int sj,int bi,int bj,int id){int i,j,k,ii,jj;
    memset(dis,0,sizeof(dis));head=1; tail=2;//set the used num
    q[1][0]=si,q[1][1]=sj,dis[si][sj]=1;//push the first one to the queue ,and just decide it's distance as 1
    while(head!=tail){i=q[head][0],j=q[head++][1];//mark last x and y,and then change the queue's head
        for(k=0;k<4;k++){ii=i+dir[k][0],jj=j+dir[k][1];//move around
            if(!map[ii][jj]||(ii==bi&&jj==bj)||dis[ii][jj])continue;//if the chessman can't move |or have moved to|or dis has been marked ,just go on
            dis[ii][jj]=dis[i][j]+1,q[tail][0]=ii,q[tail++][1]=jj;//mark the dis,because move from the last x,y ;so the dis is just one more, |and then push it to the queue
        }//if the queue is empty,break
    }if(id==4)return;//step 4,just move from the blank|and this work has done ,just return (blank area can't move!!!)
    for(k=0;k<4;k++){i=bi+dir[k][0],j=bj+dir[k][1];//move around
        if((i==si&&j==sj)||!dis[i][j]) continue;//if have moved to|or has not been marked ,just go on
        add_edge(gg(bi,bj,id),gg(bi,bj,k),dis[i][j]-1);//add_edge
    }add_edge(gg(bi,bj,id),gg(si,sj,id^1),1);//the last operation
}
void spfa(int si,int sj){int i,sn,fn,val;memset(sdis,60,sizeof(sdis));head=1;tail=1;//set the used num
    for(i=0;i<4;i++){//move around
        if(!dis[si+dir[i][0]][sj+dir[i][1]]) continue;//if the area move to has not been marked
        sn=gg(si,sj,i);
        sq[tail++]=sn,sdis[sn]=dis[si+dir[i][0]][sj+dir[i][1]]-1,vis[sn]=1;//add the sn into the queue,mark the dis(sdis),and set the been array as true
    }while(head!=tail){sn=sq[head++];//if the queue isn't empty,keep going,use a num to mark the sq element
        for(i=p[sn];i;i=e[i][2]){fn=e[i][0],val=e[i][1];//before and val has marked
            if(sdis[fn]<=sdis[sn]+val)continue;//if can't make better ,just go on
            sdis[fn]=sdis[sn]+val;//or change the new dis,as better 
            if(vis[fn])continue;//if has visit ,go on
            vis[fn]=1;sq[tail++]=fn;// or ,make it been visited,and push it to the queue sq
            if(tail>MAXN) tail = 1;//if the tail may beyond the array admitted,reset the tail
        }vis[sn]=0;if(head>MAXN)head=1;//as above,and let it not been visited
        }
}
int main(){int i,j,bi,bj,si,sj,fi,fj;cin(n),cin(m),cin(qq);
    for(i=1;i<=n;i++) for(j=1;j<=m;j++) cin(map[i][j]);//above ciner
    for(i=1;i<=n;i++) for(j=1;j<=m;j++){
        if(!map[i][j]) continue;//if this chessman can't move,just go on for the next one
        if(map[i-1][j]) bfs(i-1,j,i,j,0);//move above to here step0
        if(map[i+1][j]) bfs(i+1,j,i,j,1);//move down  to here step1
        if(map[i][j-1]) bfs(i,j-1,i,j,2);//move left  to here step2
        if(map[i][j+1]) bfs(i,j+1,i,j,3);//move right to here step3
        //set the first map,add some necessary edge
    }while(qq--){cin(bi),cin(bj),cin(si),cin(sj),cin(fi),cin(fj);//b:blank,s:start,f:finish
        if(si==fi &&sj==fj) { printf("0\n"); continue;}//the wanted is just the given,need no time|the special solution|
        bfs(bi,bj,si,sj,4); /*move blank area to the start step4*/spfa(si,sj);ans=INF;//
        for(i=0;i<4;i++) ans=min(ans,sdis[gg(fi,fj,i)]);
        if(ans<INF) printf("%d\n",ans)/*possible solution*/;else printf("-1\n");/*impossible solution*/}return 0;
}//rubish cpp,note from franzl lang.

代码实现如上,程序是最好的语言、、

posted @ 2017-07-08 13:46  yodel  阅读(328)  评论(0编辑  收藏  举报