[Codeforces Round #354 DIV2D (CF676D)] Theseus and labyrinth

来源:互联网 发布:zip mac怎么解压软件 编辑:程序博客网 时间:2024/05/29 13:06

题意

nm网格,每个房间四个门存在与否有16种状态。每一秒可以从一个格子走到相邻格子(当且仅当两格子有互通的门),或者将所有的格子顺时针旋转90。询问从起始点走到终点最少用时。

题解

拆点,每个格子拆成四种状态的点,然后bfs,就是写起来比较麻烦。

代码

/// by ztx#include <bits/stdc++.h>//   1 1 1 1//   U D L R//   8 4 2 1#define  U    8#define  D    4#define  L    2#define  R    1#define  maxn  1010LLstruct P { int x, y, t; } now;int vis[4][maxn][maxn];std::deque<P>q;char map[4][maxn][maxn];const int dx[4] = {-1, 1 , 0 , 0 };const int dy[4] = {0 , 0 , -1, 1 };char ch;inline void GetStatus(int x,int y) {    ch = map[0][x][y];    if (ch == '*') map[0][x][y] = map[1][x][y] = map[2][x][y] = map[3][x][y] = 0;    else if (ch == '+') map[0][x][y] = map[1][x][y] = map[2][x][y] = map[3][x][y] = 15;    else if (ch == '-') map[0][x][y] = map[2][x][y] = 3, map[1][x][y] = map[3][x][y] = 12;    else if (ch == '|') map[0][x][y] = map[2][x][y] = 12, map[1][x][y] = map[3][x][y] = 3;    else if (ch == '^') map[0][x][y] = U, map[1][x][y] = R, map[2][x][y] = D, map[3][x][y] = L;    else if (ch == '>') map[0][x][y] = R, map[1][x][y] = D, map[2][x][y] = L, map[3][x][y] = U;    else if (ch == '<') map[0][x][y] = L, map[1][x][y] = U, map[2][x][y] = R, map[3][x][y] = D;    else if (ch == 'v') map[0][x][y] = D, map[1][x][y] = L, map[2][x][y] = U, map[3][x][y] = R;    else if (ch == 'L') map[0][x][y] = 15^L, map[1][x][y] = 15^U, map[2][x][y] = 15^R, map[3][x][y] = 15^D;    else if (ch == 'R') map[0][x][y] = 15^R, map[1][x][y] = 15^D, map[2][x][y] = 15^L, map[3][x][y] = 15^U;    else if (ch == 'U') map[0][x][y] = 15^U, map[1][x][y] = 15^R, map[2][x][y] = 15^D, map[3][x][y] = 15^L;    else if (ch == 'D') map[0][x][y] = 15^D, map[1][x][y] = 15^L, map[2][x][y] = 15^U, map[3][x][y] = 15^R;}int main() {    int n, m, i, j, t, tt, x, y, x0, y0;    scanf("%d%d", &n, &m);    for (i = 1; i <= n; i ++ )        scanf("%s", map[0][i]+1);    scanf("%d%d%d%d", &x, &y, &x0, &y0);    for (i = 1; i <= n; i ++ )        for (j = 1; j <= m; j ++ )            GetStatus(i,j);    vis[0][x][y] = 1;    q.clear(), q.push_back((P){x,y,0});    while (!q.empty()) {        now = q.front(), q.pop_front(), t = now.t;        if (now.x == x0 && now.y == y0) { printf("%d\n", vis[now.t][now.x][now.y]-1); goto END; }        for (i = 0; i < 4; i ++ ) {            x = now.x+dx[i], y = now.y+dy[i];            if (vis[t][x][y]) continue;            if (i==0 && (((map[t][now.x][now.y]&U)==0) || ((map[t][x][y]&D)==0))) continue;            if (i==1 && (((map[t][now.x][now.y]&D)==0) || ((map[t][x][y]&U)==0))) continue;            if (i==2 && (((map[t][now.x][now.y]&L)==0) || ((map[t][x][y]&R)==0))) continue;            if (i==3 && (((map[t][now.x][now.y]&R)==0) || ((map[t][x][y]&L)==0))) continue;            vis[t][x][y] = vis[t][now.x][now.y]+1, q.push_back((P){x,y,t});        }        t ++ , t %= 4;        if (!vis[t][now.x][now.y]) vis[t][now.x][now.y] = vis[now.t][now.x][now.y]+1, q.push_back((P){now.x,now.y,t});    }    puts("-1");    END:;    getchar(),getchar();    return 0;}
0 0