zoukankan      html  css  js  c++  java
  • 【HDOJ】2416 Treasure of the Chimp Island

    bfs()。题目的数据乱码。应该如下:

    *****#*********
    *.1....4..$...*
    *..***..2.....*
    *..2..*****..2*
    *..3..******37A
    *****9..56....*
    *.....******..*
    ***CA**********
    
    *****
    *$3**
    *.2**
    ***#*
    
    --
      1 #include <iostream>
      2 #include <queue>
      3 #include <cstring>
      4 #include <cstdio>
      5 #include <algorithm>
      6 using namespace std;
      7 
      8 #define isUpper(ch) (ch>='A' && ch<='Z')
      9 #define isLower(ch) (ch>='a' && ch<='z')
     10 #define isDigit(ch) (ch>='0' && ch<='9')
     11 #define INF 0x7f7f7f7f
     12 #define MAXN 105
     13 
     14 typedef struct node_t {
     15     int x, y, z, t;
     16     node_t() {}
     17     node_t(int xx,int yy, int zz, int tt) {
     18         x = xx; y = yy; z = zz; t = tt;
     19     }
     20     friend bool operator < (const node_t a, const node_t b) {
     21         return a.t > b.t;
     22     }
     23 } node_t;
     24 
     25 int visit[MAXN][MAXN][30];
     26 char map[MAXN][MAXN];
     27 int m, n;
     28 int dir[4][2] = {-1,0,1,0,0,-1,0,1};
     29 
     30 bool check(int x, int y) {
     31     return x<0||x>=m||y<0||y>=n;
     32 }
     33 
     34 int bfs() {
     35     int x, y, z, t;
     36     int i, j;
     37     priority_queue<node_t> Q;
     38 
     39     memset(visit, 0x7f, sizeof(visit));
     40     for (i=0; i<m; ++i) {
     41         for (j=0; j<n; ++j) {
     42             if (map[i][j]=='#') {
     43                 Q.push(node_t(i, j, 0, 0));
     44                 visit[i][j][0] = 0;
     45             } else if (isUpper(map[i][j])) {
     46                 Q.push(node_t(i, j, map[i][j]-'A'+1, 0));
     47                 visit[i][j][map[i][j]-'A'+1] = 0;
     48             }
     49         }
     50     }
     51 
     52     while (!Q.empty()) {
     53         node_t nd = Q.top();
     54 
     55         if (map[nd.x][nd.y] == '$')
     56             return nd.t;
     57 
     58         Q.pop();
     59         for (i=0; i<4; ++i) {
     60             x = nd.x + dir[i][0];
     61             y = nd.y + dir[i][1];
     62             if (check(x, y) || map[x][y]=='*')
     63                 continue;
     64             if (map[x][y] == '.') {
     65                 if (nd.t < visit[x][y][nd.z]) {
     66                     visit[x][y][nd.z] = nd.t;
     67                     Q.push(node_t(x, y, nd.z, nd.t));
     68                 }
     69             } else if (map[x][y] == '$') {
     70                 Q.push(node_t(x, y, nd.z, nd.t));
     71             } else if (isDigit(map[x][y])) {
     72                 if (nd.z > 0 && nd.t < visit[x][y][nd.z-1]) {
     73                     visit[x][y][nd.z-1] = nd.t;
     74                     Q.push(node_t(x, y, nd.z-1, nd.t));
     75                 }
     76                 t = nd.t+map[x][y]-'0';
     77                 if (t < visit[x][y][nd.z]) {
     78                     visit[x][y][nd.z] = t;
     79                     Q.push(node_t(x, y, nd.z, t));
     80                 }
     81             }
     82         }
     83     }
     84 
     85     return -1;
     86 }
     87 
     88 int main() {
     89 #ifndef ONLINE_JUDGE
     90     freopen("data.in", "r", stdin);
     91 #endif
     92 
     93     int i = 0, tmp;
     94 
     95     while (gets(map[i++]) != NULL) {
     96         if (map[0][0] == '-')
     97             break;
     98         while (gets(map[i])!=NULL && strlen(map[i])!=0)
     99             ++i;
    100         m = i;
    101         n = strlen(map[0]);
    102         tmp = bfs();
    103         if (tmp == -1)
    104             printf("IMPOSSIBLE
    ");
    105         else
    106             printf("%d
    ", tmp);
    107         i = 0;
    108     }
    109 
    110     return 0;
    111 }
  • 相关阅读:
    大三上学期周总结
    《代码整洁之道》阅读笔记(三)
    大三上学期周总结
    【测试技能】常用adb命令记录
    【Jmeter】Mac本JMeter实现压力测试实例
    【音视频】IP 地区定位,有坑
    【服务器】mp(edge)内存使用情况扩展
    【python】TypeError: 'Response' object is not subscriptable
    【python】单元测试框架改造接口自动化case
    【git】放弃本地修改,拉取远端最新代码
  • 原文地址:https://www.cnblogs.com/bombe1013/p/4046329.html
Copyright © 2011-2022 走看看