Description
Solution
发现最优方案中一定是这样的:
1.空白格到达起始格的一侧
2.起始格移到空白格位置
3.空白格到达起始格的另一侧并转到步骤2
所以预处理出对于每个格子空白格从一侧绕道另一侧的最少步数,对于每次询问直接BFS即可
Code
//Author: Hany01
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<queue>
#include<vector>
#include<set>
#define For(i , j , k) for (int i = (j) , _##end_ = (k) ; i <= _##end_ ; ++ i)
#define Fordown(i , j , k) for (int i = (j) , _##end_ = (k) ; i >= _##end_ ; -- i)
#define Set(a , b) memset(a , b , sizeof(a))
#define pb push_back
#define mp(a , b) make_pair(a , b)
#define INF (0x3f3f3f3f)
#define INF1 (2139062143)
#define Mod (1000000007)
using namespace std;
typedef long long LL;
typedef pair<int , int> PII;
template <typename T> inline bool chkmax(T &a , T b) { return a < b ? (a = b , 1) : 0; }
template <typename T> inline bool chkmin(T &a , T b) { return b < a ? (a = b , 1) : 0; }
int _ , __;
char c_;
inline int read()
{
for (_ = 0 , __ = 1 , c_ = getchar() ; !isdigit(c_) ; c_ = getchar()) if (c_ == '-') __ = -1;
for ( ; isdigit(c_) ; c_ = getchar()) _ = (_ << 1) + (_ << 3) + (c_ ^ 48);
return _ * __;
}
inline void file()
{
#ifndef ONLINE_JUDGE
freopen("puzzle.in" , "r" , stdin);
freopen("puzzle.out" , "w" , stdout);
#endif
}
const int maxn = 33;
struct Pos
{
int x , y , dir;
};
int dt[4][2] = {-1 , 0 , 1 , 0 , 0 , -1 , 0 , 1};
int n , m , Q , dis[maxn][maxn][4] , dist[maxn][maxn] , cst[maxn][maxn][4][4] , sx , sy , tx , ty , bx , by , G[maxn][maxn] , isinq[maxn][maxn] , isinq1[maxn][maxn][4];
queue<PII> q;
inline void pre_BFS(int x , int y)
{
int sx , sy , tx , ty , cx , cy , nx , ny;
For(i , 0 , 2)
For(j , i + 1 , 3)
{
sx = x + dt[i][0];
sy = y + dt[i][1];
tx = x + dt[j][0];
ty = y + dt[j][1];
q.push(mp(sx , sy));
Set(dist , 127);
dist[sx][sy] = 0;
while (!q.empty())
{
cx = q.front().first;
cy = q.front().second;
q.pop();
isinq[cx][cy] = 0;
For(k , 0 , 3)
{
nx = cx + dt[k][0];
ny = cy + dt[k][1];
if (nx == x && ny == y || !G[nx][ny] || nx < 1 || nx > n || ny < 1 || ny > m)
continue;
if (chkmin(dist[nx][ny] , dist[cx][cy] + 1))
if (!isinq[nx][ny])
{
q.push(mp(nx , ny));
isinq[nx][ny] = 1;
if (nx == tx && ny == ty)
break;
}
if (nx == tx && ny == ty)
break;
}
}
cst[x][y][i][j] = cst[x][y][j][i] = dist[tx][ty];
}
}
inline void Init()
{
n = read();
m = read();
Q = read();
For(i , 1 , n)
For(j , 1 ,m)
G[i][j] = read();
For(i , 1 , n)
For(j , 1 , m)
if (G[i][j])
pre_BFS(i , j);
}
void BFS()
{
int cx , cy , nx , ny;
Set(dist , 127);
dist[bx][by] = 0;
q.push(mp(bx , by));
while (!q.empty())
{
cx = q.front().first;
cy = q.front().second;
q.pop();
isinq[cx][cy] = 0;
For(i , 0 , 3)
{
nx = cx + dt[i][0];
ny = cy + dt[i][1];
if (nx == sx && ny == sy || !G[nx][ny] || nx < 1 || ny < 1 || nx > n || ny > m)
continue;
if (chkmin(dist[nx][ny] , dist[cx][cy] + 1))
if (!isinq[nx][ny])
{
q.push(mp(nx , ny));
isinq[nx][ny] = 1;
}
}
}
}
inline void Solve()
{
int cx , cy , cd;
queue<Pos> q;
while (Q --)
{
bx = read();
by = read();
sx = read();
sy = read();
tx = read();
ty = read();
if (sx == tx && sy == ty)
{
puts("0");
continue;
}
BFS();
For(i , 1 , n)
For(j , 1 , m)
For(k , 0 , 3)
dis[i][j][k] = INF;
For(i , 0 , 3)
{
if (dist[sx + dt[i][0]][sy + dt[i][1]] == INF1 || !G[sx + dt[i][0]][sy + dt[i][1]])
continue;
q.push((Pos){sx + dt[i][0] , sy + dt[i][1] , i ^ 1});
dis[sx + dt[i][0]][sy + dt[i][1]][i ^ 1] = dist[sx + dt[i][0]][sy + dt[i][1]] + 1;
isinq1[sx + dt[i][0]][sy + dt[i][1]][i ^ 1] = 1;
}
while (!q.empty())
{
cx = q.front().x;
cy = q.front().y;
cd = q.front().dir;
q.pop();
isinq1[cx][cy][cd] = 0;
For(i , 0 , 3)
{
if (i == cd || cst[cx][cy][cd][i] == INF1 || !G[cx + dt[i][0]][cy + dt[i][1]] || cx + dt[i][0] < 1 || cx + dt[i][0] > n || cy + dt[i][1] < 1 || cy + dt[i][1] > m)
continue;
if (chkmin(dis[cx + dt[i][0]][cy + dt[i][1]][i ^ 1] , dis[cx][cy][cd] + cst[cx][cy][cd][i] + 1))
{
if (!isinq1[cx + dt[i][0]][cy + dt[i][1]][i ^ 1])
{
isinq1[cx + dt[i][0]][cy + dt[i][1]][i ^ 1] = 1;
q.push((Pos){cx + dt[i][0] , cy + dt[i][1] , i ^ 1});
}
}
}
}
int Ans = min(min(dis[tx][ty][0] , dis[tx][ty][1]) , min(dis[tx][ty][2] , dis[tx][ty][3]));
if (Ans == INF)
puts("-1");
else
printf("%d\n" , Ans);
}
}
int main()
{
file();
Init();
Solve();
return 0;
}
//朝闻游子唱骊歌,昨夜微霜初度河。
//鸿雁不堪愁里听,云山况是客中过。
//关城曙色催寒近,御苑砧声向晚多。
//莫是长安行乐处,空令岁月易蹉跎。
//--李颀《送魏万之京》