• CF div2 D BFS


    http://codeforces.com/contest/676/problem/D

    题目大意:

    勇者去迷宫杀恶龙。迷宫是有n*m的方格子组成的。迷宫上有各种记号,这些记号表达着能走的方向。当且仅当两个房间的记号是相互联通的,才能走过去。

    我们有如下选择,最直白的一种就是直接走到另外一个格子上去(当然格子得相互连通),第二种操作就是顺时针旋转所有的格子(格子的位置保持不变)。

    问,勇者要多久才能走到恶龙所在的地方。

    思路:

    表示这道题真心不会。。。下面的这个代码也是参考了别人的吧。。。换成我的话for的循环里面肯定不会写成f*3+i之类的(虽然这个到现在还没有弄明白)

    我们用结构体保存当前的位置,旋转的次数和行走的距离。然后我们每次对一个当前的格子进行两种操作

    ①瞬时间旋转操作(每次都旋转1,反正最后肯定会遍历到4的)

    ②就是枚举上下左右,看看是否能走过去。

    然后就OK了

      1 //看看会不会爆int!
      2 #include <bits/stdc++.h>
      3 using namespace std;
      4 #define ll long long
      5 #define pb push_back
      6 #define mk make_pair
      7 #define fi first
      8 #define se second
      9 #define all(a) a.begin(), a.end()
     10 
     11 const int maxn = 1005;
     12 bool vis[maxn][maxn][10];
     13 int n, m;
     14 struct point{
     15     int x, y;
     16     int f, d;
     17     point(int xx = 0, int yy = 0, int ff = 0, int dd = 0){
     18         x = xx, y = yy, f = ff, d = dd;
     19     }
     20 }p[maxn][maxn];
     21 
     22 char atlas[maxn][maxn];
     23 int xt, yt, xm, ym;
     24 /*
     25 int dx[] = {0, 0, -1, 1};//上下左右
     26 int dy[] = {-1, 1, 0, 0};
     27 */
     28 int dx[] = {-1, 0, 1, 0};//左下右上
     29 int dy[] = {0, 1, 0, -1};
     30 
     31 bool check(char ch, int dir){
     32     if (dir == 0) return ch == '+' || ch == '|' || ch == '^' || ch == 'L' || ch == 'R' || ch == 'D';
     33     else if (dir == 1) return ch == '+' || ch == '-' || ch == '>' || ch == 'L' || ch == 'U' || ch == 'D';
     34     else if (dir == 2) return ch == '+' || ch == '|' || ch == 'v' || ch == 'L' || ch == 'R' || ch == 'U';
     35     else  return ch == '+' || ch == '-' || ch == '<' || ch == 'R' || ch == 'U' || ch == 'D';
     36     return 0;
     37 }
     38 
     39 int bfs(){
     40     queue <point> que;
     41     vis[xt][yt][0] = 1;
     42     que.push(point(xt, yt, 0, 0));
     43     while (!que.empty()){
     44         point q = que.front();
     45         que.pop();
     46         int x = q.x, y = q.y;
     47         int f = q.f, d = q.d;
     48         if (x == xm && y == ym) return d;
     49         if (!vis[x][y][(f + 1) % 4]){
     50             vis[x][y][(f + 1) % 4] = true;
     51             que.push(point(x, y, (f + 1) % 4, d + 1));
     52         }
     53         for (int i = 0; i < 4; i++){
     54             int nx = x + dx[i], ny = y + dy[i];
     55             if (nx <= 0 || ny <= 0 || nx > n || ny > m) continue;
     56             if (atlas[nx][ny] == '*' || vis[nx][ny][f]) continue;
     57             if (!check(atlas[x][y], (f * 3 + i) % 4)) continue;//我们要选择的是和上面相反的方向
     58             if (!check(atlas[nx][ny], (f * 3 + i + 2) % 4)) continue;
     59             que.push(point(nx, ny, f, d + 1));
     60             vis[nx][ny][f % 4] = true;
     61         }
     62     }
     63     return -1;
     64 }
     65 
     66 int main(){
     67 #ifndef ONLINE_JUDGE
     68     freopen("input.txt", "r", stdin);
     69 #endif
     70     scanf("%d%d", &n, &m);
     71     for (int i = 1; i <= n; i++){
     72         scanf("%s", atlas[i] + 1);
     73     }
     74     scanf("%d%d", &xt, &yt);
     75     scanf("%d%d", &xm, &ym);
     76     int ans = bfs();
     77     printf("%d
    ", ans);
     78   return 0;
     79 }
     80 /*
     81                    _ooOoo_
     82                   o8888888o
     83                   88" . "88
     84                   (| -_- |)
     85                   O  =  /O
     86                ____/`---'\____
     87              .'  \|     |//  `.
     88             /  \|||  :  |||//  
     89            /  _||||| -:- |||||-  
     90            |   | \  -  /// |   |
     91            | \_|  ''---/''  |   |
     92              .-\__  `-`  ___/-. /
     93          ___`. .'  /--.--  `. . __
     94       ."" '<  `.___\_<|>_/___.'  >'"".
     95      | | :  `- \`.;` _ /`;.`/ - ` : | |
     96         `-.   \_ __ /__ _/   .-` /  /
     97 ======`-.____`-.___\_____/___.-`____.-'======
     98                    `=---='
     99 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    100          佛祖保佑       永无BUG
    101 */
    View Code
  • 相关阅读:
    【线段树】懒标记的维护
    【dp】luoguP4796 关于图 想不到是状压dp (┬_┬)
    【数论】莫比乌斯函数+中国剩余定理
    【积累】Burnside引理和Polya定理
    【排序优化】牛客练习赛54D
    ubuntu修改hostname
    apt-get命令详解
    微信历史版本下载
    vim自动补全快捷键
    Servlet实例
  • 原文地址:https://www.cnblogs.com/heimao5027/p/5544351.html
Copyright © 2020-2023  润新知