华容道 洛谷1979 bfs+spfa

来源:互联网 发布:汇鑫通网络支付 编辑:程序博客网 时间:2024/05/17 22:19

Description


小 B 最近迷上了华容道,可是他总是要花很长的时间才能完成一次。于是,他想到用编程来完成华容道:给定一种局面, 华容道是否根本就无法完成,如果能完成, 最少需要多少时间。
小 B 玩的华容道与经典的华容道游戏略有不同,游戏规则是这样的:
在一个 n*m 棋盘上有 n*m 个格子,其中有且只有一个格子是空白的,其余 n*m-1个格子上每个格子上有一个棋子,每个棋子的大小都是 1*1 的;
有些棋子是固定的,有些棋子则是可以移动的;
任何与空白的格子相邻(有公共的边)的格子上的棋子都可以移动到空白格子上。
游戏的目的是把某个指定位置可以活动的棋子移动到目标位置。
给定一个棋盘,游戏可以玩 q 次,当然,每次棋盘上固定的格子是不会变的, 但是棋盘上空白的格子的初始位置、 指定的可移动的棋子的初始位置和目标位置却可能不同。第 i 次
玩的时候, 空白的格子在第 EXi 行第 EYi 列,指定的可移动棋子的初始位置为第 SXi 行第 SYi列,目标位置为第 TXi 行第 TYi 列。
假设小 B 每秒钟能进行一次移动棋子的操作,而其他操作的时间都可以忽略不计。请你告诉小 B 每一次游戏所需要的最少时间,或者告诉他不可能完成游戏。

Input


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

Output


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

Data constraint


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

Solution


60分做法显然,用f[x1][y1][x2][y2]表示空白格子在(x1,y1),指定棋子在(x2,y2)时最小步数,bfs,总的复杂度为O(q(nm)2)

如何优化呢?考虑每次移动只能发生在空白格子与相邻棋子之间,那么只有空白格子在指定棋子的四个方向上时产生的状态才是有用状态。用f[x][y][k]表示指定棋子在(x,y),空白格子在k方向上与指定棋子相邻时的最小步数
不难发现,空白格子可以与指定棋子交换位置,也可以移动到指定棋子的其他相邻方向上。根据两种走法在起始状态与结束状态之间连边,构成的图分别跑最短路就可以了。

需要注意的是,在bfs空白格子移动到其他方向上时要封住指定棋子以确保指定棋子不会改变原本的位置。
好懒啊就不放图了

Code


#include <stdio.h>#include <string.h>#include <queue>#define rep(i, st, ed) for (int i = st; i <= ed; i += 1)#define erg(i, st) for (int i = ls[st]; i; i = e[i].next)#define fill(arr, num) memset(arr, num, sizeof(arr))#define min(x, y) (x)<(y)?(x):(y)#define INF 100000#define N 33#define E N * N * N | 1struct edge{int x, y, w, next;}e[E];bool inQueue[N * N * 4], vis[N * N * 4], been[N][N];int idx[N][N][4], mp[N][N], dis[N * N * 4], ls[N * N * 4];int dir[4][2] = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}}, rev[4] = {1, 0, 3, 2};int edgeCnt = 0;inline int read(){    int num = 0; char ch = getchar();    while (ch < '0' || ch > '9'){        ch = getchar();    }    while (ch >= '0' && ch <= '9'){        num = (num << 1) + (num << 3) + ch - '0';        ch = getchar();    }    return num;}inline void addEdge(int x, int y, int w){    e[++ edgeCnt] = (edge){x, y, w, ls[x]}; ls[x] = edgeCnt;    // printf("%d -> %d, %d\n", x, y, w);}inline int bfs(int stX, int stY, int edX, int edY){    std:: queue<int> que;    fill(been, 0);    que.push(stX); que.push(stY);    que.push(0);    been[stX][stY] = 1;    while (!que.empty()){        int nowX = que.front(); que.pop();        int nowY = que.front(); que.pop();        int step = que.front(); que.pop();        if (nowX == edX && nowY == edY){            return step;        }        // printf("%d, %d\n", x, y);        rep(direction, 0, 3){            int tarX = nowX + dir[direction][0];            int tarY = nowY + dir[direction][1];            if (!been[tarX][tarY] && mp[tarX][tarY]){                que.push(tarX); que.push(tarY);                que.push(step + 1);                been[tarX][tarY] = 1;            }        }    }    return INF;}inline int spfa(int x1, int y1, int x2, int y2, int x3, int y3){    std:: queue<int> que;    if (x2 == x3 && y2 == y3){        return 0;    }    if (!mp[x1][y1] || !mp[x2][y2] || !mp[x3][y3]){        return INF;    }    rep(i, 0, idx[0][0][0]){dis[i] = INF;}    rep(direction, 0, 3){        int now = idx[x2][y2][direction];        if (now){            que.push(now);            mp[x2][y2] = 0;            dis[now] = bfs(x1, y1, x2 + dir[direction][0], y2 + dir[direction][1]);            mp[x2][y2] = 1;            inQueue[now] = 1;        }    }    fill(inQueue, 0);    while (!que.empty()){        int now = que.front(); que.pop();        // printf("%d\n", now);        erg(i, now){            // printf("x%d, y%d, w%d, nx%d\n", e[i].x, e[i].y, e[i].w, e[i].next);            if (dis[now] + e[i].w < dis[e[i].y]){                dis[e[i].y] = dis[now] + e[i].w;                if (!inQueue[e[i].y]){                    que.push(e[i].y);                    inQueue[e[i].y] = 1;                }            }        }        inQueue[now] = 0;    }    int ret = INF;    rep(direction, 0, 3){        ret = min(ret, dis[idx[x3][y3][direction]]);    }    return ret;}int main(void){    freopen("data.in", "r", stdin);    freopen("myp.out", "w", stdout);    fill(mp, 0);    fill(ls, 0);    int n = read(), m = read(), p = read();    rep(i, 1, n){        rep(j, 1, m){            mp[i][j] = read();        }    }    rep(i, 1, n){        rep(j, 1, m){            rep(k, 0, 3){                if (mp[i][j] && mp[i + dir[k][0]][j + dir[k][1]]){                    idx[i][j][k] = ++ idx[0][0][0];                }            }        }    }    rep(i, 1, n){        rep(j, 1, m){            rep(k, 0, 3){                int now = idx[i][j][k], tar = idx[i + dir[k][0]][j + dir[k][1]][rev[k]];                if (now && tar){                    addEdge(now, tar, 1);                }                rep(l, 0, 3){                    tar = idx[i][j][l];                    if (l != k && now && tar){                        mp[i][j] = 0;                        addEdge(now, tar, bfs(i + dir[k][0], j + dir[k][1], i + dir[l][0], j + dir[l][1]));/*                      printf("%d, %d\n", i + dir[k][0], j + dir[k][1]);                        printf("%d, %d\n", i + dir[l][0], j + dir[l][1]);                        printf("%d\n", bfs(i + dir[k][0], j + dir[k][1], i + dir[l][0], j + dir[l][1]));*/                      mp[i][j] = 1;                    }                }            }        }    }    while (p --){        int x1 = read(), y1 = read(), x2 = read(), y2 = read(), x3 = read(), y3 = read();        int ans = spfa(x1, y1, x2, y2, x3, y3);        if (ans == INF){            puts("-1");            continue;        }        printf("%d\n", ans);    }    return 0;}
原创粉丝点击