题目描述
小 B 最近迷上了华容道,可是他总是要花很长的时间才能完成一次。于是,他想到用
编程来完成华容道:给定一种局面,华容道是否根本就无法完成,如果能完成,最少需要多 少时间。
小 B 玩的华容道与经典的华容道游戏略有不同,游戏规则是这样的:
1. 在一个 n*m 棋盘上有 n*m 个格子,其中有且只有一个格子是空白的,其余 n*m-1
个格子上每个格子上有一个棋子,每个棋子的大小都是 1*1 的;
2. 有些棋子是固定的,有些棋子则是可以移动的;
3. 任何与空白的格子相邻(有公共的边)的格子上的棋子都可以移动到空白格子上。 游戏的目的是把某个指定位置可以活动的棋子移动到目标位置。
给定一个棋盘,游戏可以玩 q 次,当然,每次棋盘上固定的格子是不会变的,但是棋盘 上空白的格子的初始位置、指定的可移动的棋子的初始位置和目标位置却可能不同。第 i 次 玩的时候,空白的格子在第 EXi 行第 EYi 列,指定的可移动棋子的初始位置为第 SXi 行第 SYi 列,目标位置为第 TXi 行第 TYi 列。
假设小 B 每秒钟能进行一次移动棋子的操作,而其他操作的时间都可以忽略不计。请 你告诉小 B 每一次游戏所需要的最少时间,或者告诉他不可能完成游戏。
输入
第一行有 3 个整数,每两个整数之间用一个空格隔开,依次表示 n、m 和 q; 接下来的 n 行描述一个 n*m 的棋盘,每行有 m 个整数,每两个整数之间用一个空格隔
开,每个整数描述棋盘上一个格子的状态,0 表示该格子上的棋子是固定的,1 表示该格子 上的棋子可以移动或者该格子是空白的。
接下来的 q 行,每行包含 6 个整数依次是 EXi、EYi、SXi、SYi、TXi、TYi,每两个整 数之间用一个空格隔开,表示每次游戏空白格子的位置,指定棋子的初始位置和目标位置。
输出
输出有 q 行,每行包含 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
样例输出
2
-1
提示
【输入输出样例说明】 棋盘上划叉的格子是固定的,红色格子是目标位置,圆圈表示棋子,其中绿色圆圈表示
目标棋子。
- 第一次游戏,空白格子的初始位置是 (3, 2)(图中空白所示),游戏的目标是将初始
位置在(1, 2)上的棋子(图中绿色圆圈所代表的棋子)移动到目标位置(2, 2)(图中红色的格 子)上。
移动过程如
【数据范围】
对于 30%的数据,1 ≤ n,m ≤ 10,q = 1; 对于 60%的数据,1 ≤ n,m ≤ 30,q ≤ 10; 对于 100%的数据,1 ≤ n, m ≤ 30,q ≤ 500。
题解
首先,如果要移动目标棋子,那么我们首先必须要将空格移到该棋子的上下左右四个方向上相邻位置之一,然后才可以移动该棋子。然后,我们分析该棋子移动时候的性质: 棋子每次可以移动,仅当空格位于其相邻位置的时候,每次移动完棋子,空格总会在棋子相邻的位置,那么我们发现,对于棋子在某一位置,然后空格又在其四个方向上某一相邻位置时,棋子要想某一方向移动一个时的花费的步数是一定的,那么,就可以先进行一次预处理,预处理出对于目标棋子在上述条件下每次移动所需的步数。 然后,预处理完成之后,我们会发现每次查询都会变成一个求最短路的问题,用Dijstra或SPFA的话,可以在时限范围内解决。
代码
#include<cstdio>
#include<iostream>
#include<cstring>
#include<iostream>
#include<queue>
#include<cmath>
#include<algorithm>
#define N 500005
#define inf 100000000
#define ll long long
using namespace std;
const int xx[4]= {1,-1,0,0};
const int yy[4]= {0,0,1,-1};
inline int read() {
int x=0;
char ch=getchar();
while (ch<'0'||ch>'9') ch=getchar();
while (ch>='0'&&ch<='9') x=x*10+ch-'0',ch=getchar();
return x;
}
struct node {
int x,y;
};
struct data {
int x,y,dis;
};
struct spf {
int x,y,k;
};
int a[35][35],dis[35][35][4],Move[35][35][4][4];
bool flag[35][35][4],mark[35][35];
int n,m,q;
bool check(int x,int y) {
if (x<1||x>n||y<1||y>m) return 0;
if (a[x][y]==0) return 0;
if (mark[x][y]) return 0;
return 1;
}
int bfs(node s,node t) {
if (s.x==t.x&&s.y==t.y) return 0;
memset(mark,0,sizeof(mark));
queue<data>q;
q.push((data) {
s.x,s.y,0
});
mark[s.x][s.y]=1;
while (!q.empty()) {
int x=q.front().x,y=q.front().y,dis=q.front().dis;
q.pop();
for (int i=0; i<4; i++) {
int nx=x+xx[i],ny=y+yy[i];
if (check(nx,ny)) {
if (nx==t.x&&ny==t.y) return dis+1;
mark[nx][ny]=1;
q.push((data) {
nx,ny,dis+1
});
}
}
}
return inf;
}
node go(int x,int y,int t) {
return (node) {
x+xx[t],y+yy[t]
};
}
void build() {
for (int i=1; i<=n; i++)
for (int j=1; j<=m; j++) {
if (a[i][j]==0) continue;
a[i][j]=0;
for (int k=0; k<4; k++)
for (int h=0; h<4; h++) {
if (h<k) {
Move[i][j][k][h]=Move[i][j][h][k];
continue;
}
node s=go(i,j,k);
node t=go(i,j,h);
if (a[s.x][s.y]==0||a[t.x][t.y]==0) continue;
Move[i][j][k][h]=bfs(s,t)+1;
if (Move[i][j][k][h]==inf+1) continue;
//cout<<i<<" "<<j<<" "<<k<<" "<<h<<" "<<Move[i][j][k][h]<<endl;
}
a[i][j]=1;
}
}
int other(int i) {
if (i==0) return 1;
if (i==1) return 0;
if (i==2) return 3;
if (i==3) return 2;
}
void spfa(int ex,int ey,int sx,int sy,int tx,int ty) {
memset(dis,63,sizeof(dis));
queue<spf>q;
for (int i=0; i<4; i++) {
node s,t;
s.x=ex;
s.y=ey;
t=go(sx,sy,i);
a[sx][sy]=0;
dis[sx][sy][i]=bfs(s,t);
q.push((spf) {
sx,sy,i
});
}
a[sx][sy]=1;
memset(flag,0,sizeof(flag));
for (int i=0; i<4; i++) flag[sx][sy][i]=1;
while (!q.empty()) {
spf now=q.front();
q.pop();
for (int i=0; i<4; i++) {
int x=now.x+xx[i],y=now.y+yy[i];
if (x<1||x>n||y<1||y>m) continue;
if (a[x][y]==0) continue;
if (dis[x][y][other(i)]>dis[now.x][now.y][now.k]+Move[now.x][now.y][now.k][i]) {
dis[x][y][other(i)]=dis[now.x][now.y][now.k]+Move[now.x][now.y][now.k][i];
if (!flag[x][y][other(i)]) {
flag[x][y][other(i)]=1;
q.push((spf) {x,y,other(i});
}
}
}
flag[now.x][now.y][now.k]=0;
}
int ans=inf;
for (int i=0; i<4; i++) ans=min(dis[tx][ty][i],ans);
if (ans>=inf) puts("-1");
else printf("%d\n",ans);
}
int main() {
n=read();
m=read();
q=read();
for (int i=1; i<=n; i++)
for (int j=1; j<=m; j++)
a[i][j]=read();
build();
while (q--) {
node s,t;
int ex=read(),ey=read(),sx=read(),sy=read(),tx=read(),ty=read();
if (sx==tx&&sy==ty) {
puts("0");
continue;
}
spfa(ex,ey,sx,sy,tx,ty);
}
return 0;
}