zoukankan      html  css  js  c++  java
  • uva 10047 the monocyle (四维bfs)

    算法指南白书

    维护一个四维数组,走一步更新一步

     1 #include<cstdio>
     2 #include<cstring>
     3 #include<queue>
     4 #include<algorithm>
     5 using namespace std;
     6 
     7 const int INF = 1000000000;
     8 const int maxr = 25 + 5;
     9 const int maxc = 25 + 5;
    10 int R, C, sr, sc, tr, tc;
    11 char maze[maxr][maxc];
    12 
    13 struct State {
    14     int r, c, dir, color;
    15     State(int r, int c, int dir, int color):r(r),c(c),dir(dir),color(color) {}
    16 };
    17 
    18 const int dr[] = {-1,0,1,0}; // north, west, south, east
    19 const int dc[] = {0,-1,0,1};
    20 int d[maxr][maxc][4][5], vis[maxr][maxc][4][5];//横坐标,纵坐标,方向,颜色
    21 
    22 int ans;
    23 queue<State> Q;
    24 
    25 void update(int r, int c, int dir, int color, int v) {
    26     if(r < 0 || r >= R || c < 0 || c >= C) return; // 不能走出边界
    27     if(maze[r][c] == '.' && !vis[r][c][dir][color]) {
    28         Q.push(State(r, c, dir, color));
    29         vis[r][c][dir][color] = 1;
    30         d[r][c][dir][color] = v;
    31         if(r == tr && c == tc && color == 0) ans = min(ans, v); // 更新答案
    32     }
    33 }
    34 
    35 void bfs(State st) {
    36     d[st.r][st.c][st.dir][st.color] = 0;
    37     vis[st.r][st.c][st.dir][st.color] = 1;
    38     Q.push(st);
    39     while(!Q.empty()) {
    40         st = Q.front();
    41         Q.pop();
    42         int v = d[st.r][st.c][st.dir][st.color] + 1;
    43         update(st.r, st.c, (st.dir+1)%4, st.color, v); // 左转
    44         update(st.r, st.c, (st.dir+3)%4, st.color, v); // 右转
    45         update(st.r+dr[st.dir], st.c+dc[st.dir], st.dir, (st.color+1)%5, v); // 前进
    46     }
    47 }
    48 
    49 int main() {
    50     int kase = 0;
    51     while(scanf("%d%d", &R, &C) == 2 && R && C) {
    52         for(int i = 0; i < R; i++) {
    53             scanf("%s", maze[i]);
    54             for(int j = 0; j < C; j++)
    55                 if(maze[i][j] == 'S') {
    56                     sr = i;
    57                     sc = j;
    58                 } else if(maze[i][j] == 'T') {
    59                     tr = i;
    60                     tc = j;
    61                 }
    62         }
    63         maze[sr][sc] = maze[tr][tc] = '.';
    64         ans = INF;
    65         memset(vis, 0, sizeof(vis));
    66         bfs(State(sr, sc, 0, 0));
    67 
    68         if(kase > 0) printf("
    ");
    69         printf("Case #%d
    ", ++kase);
    70         if(ans == INF) printf("destination not reachable
    ");
    71         else printf("minimum time = %d sec
    ", ans);
    72     }
    73 }
    View Code
  • 相关阅读:
    javascript的window.open()具体解释
    Async.js解决Node.js操作MySQL的回调大坑
    入门--JTBC系统学习(1)
    Hadoop1.2.1 全然分布式集群搭建实操笔记
    Hessian原理与程序设计
    pycharm最新注册方法 pycharm最新激活方法 2016pycharm最新注册方法
    如何修改linux时间? 校正linux系统的时间
    python 内建函数 type() 和 isinstance() 介绍
    标准类型内建函数 str()和 repr() (及 `` 运算符) 简单介绍
    标准类型内建函数 cmp()介绍
  • 原文地址:https://www.cnblogs.com/ITUPC/p/5312976.html
Copyright © 2011-2022 走看看