• [HDOJ3442]Three Kingdoms


    广搜(BFS)~ 水题

    View Code
    1 #include <cstdio>
    2 #include <cstring>
    3 #include <cstdlib>
    4
    5 usingnamespace std;
    6
    7 constint SIZE =64;
    8 constint range[] = {2,3,0,2,1};
    9 constint dx[] = {-1,0,1,0};
    10 constint dy[] = {0,1,0,-1};
    11
    12 int hi;
    13 int wi;
    14 int sx;
    15 int sy;
    16 int ex;
    17 int ey;
    18 int damage[SIZE][SIZE];
    19 char g[SIZE][SIZE];
    20
    21 struct Q
    22 {
    23 int x,y,state,hp,pre;
    24 }queue[SIZE * SIZE *32];
    25
    26 bool vis[SIZE][SIZE][32];
    27
    28 bool legal(int x,int y)
    29 {
    30 return x >=0&& x < hi && y >=0&& y < wi;
    31 }
    32
    33 int cutDown(int val)
    34 {
    35 int ret =0;
    36 for (int i =0;i <5;i++)
    37 if ((val >> i) &1) ret += i +1;
    38 return ret;
    39 }
    40
    41 bool bfs(int hp)
    42 {
    43 int qh =-1;
    44 int qe =0;
    45
    46 int px;
    47 int py;
    48 int ps;
    49 int ph;
    50 int nx;
    51 int ny;
    52 int ns;
    53 int nh;
    54
    55 memset(vis,false,sizeof(vis));
    56 queue[0].x = sx;
    57 queue[0].y = sy;
    58 queue[0].state =0;
    59 queue[0].hp = hp;
    60 queue[0].pre =-1;
    61
    62 vis[sx][sy][0] =true;
    63
    64 while (qh != qe)
    65 {
    66 qh++;
    67
    68 px = queue[qh].x;
    69 py = queue[qh].y;
    70 ps = queue[qh].state;
    71 ph = queue[qh].hp;
    72
    73 if (px == ex && py == ey) returntrue;
    74
    75 for (int i =0;i <4;i++)
    76 {
    77 nx = px + dx[i];
    78 ny = py + dy[i];
    79
    80 if (!legal(nx,ny)) continue;
    81
    82 if (g[nx][ny] !='.') continue;
    83
    84 nh = ph - cutDown((((~ps) &31) & damage[nx][ny]) &31);
    85 ns = ((ps | damage[nx][ny]) &31);
    86
    87 if (nh <0) continue;
    88
    89 if (vis[nx][ny][ns]) continue;
    90
    91 qe++;
    92 queue[qe].x = nx;
    93 queue[qe].y = ny;
    94 queue[qe].state = ns;
    95 queue[qe].hp = nh;
    96 queue[qe].pre = qh;
    97 vis[nx][ny][ns] =true;
    98
    99 }
    100 }
    101 returnfalse;
    102 }
    103 int main()
    104 {
    105 int cas;
    106 scanf("%d",&cas);
    107 for (int cc =1;cc <= cas;cc++)
    108 {
    109 memset(damage,0,sizeof(damage));
    110 scanf("%d %d",&hi,&wi);
    111 for (int i =0;i < hi;i++) scanf("%s",g[i]);
    112 for (int i =0;i < hi;i++)
    113 for (int j =0;j < wi;j++)
    114 if (g[i][j] =='$') sx = i, sy = j, g[i][j] ='.';
    115 elseif (g[i][j] =='!') ex = i, ey = j, g[i][j] ='.';
    116 elseif (g[i][j] >='A'&& g[i][j] <='E')
    117 {
    118 int index = g[i][j] -'A';
    119 for (int idx =-range[index];idx <= range[index];idx++)
    120 for (int idy =-range[index];idy <= range[index];idy++)
    121 if (abs(idx) + abs(idy) <= range[index])
    122 {
    123 int nx = i + idx;
    124 int ny = j + idy;
    125 if (legal(nx,ny)) damage[nx][ny] |= (1<< index);
    126 }
    127 if(g[i][j] =='C') g[i][j] ='.';
    128 }
    129
    130 int ans =-1;
    131 for (int i =0;i <16;i++)
    132 if (bfs(i))
    133 {
    134 ans = i;
    135 break;
    136 }
    137 printf("Case %d: %d\n",cc,ans);
    138 }
    139 return0;
    140 }
  • 相关阅读:
    Java LinkList遍历方式
    Java LinkedList的实现原理
    ArrayList 原理(2)
    ArrayList 原理(1)
    Java中HashMap的实现原理
    Java HashMap两种遍历方式
    【转】Unity3d:读取FBX中的动画
    C#与U3D中字符串尾0
    U3D中的又一个坑
    MaxScript 学习笔记【有转载】
  • 原文地址:https://www.cnblogs.com/debugcool/p/HDOJ3442.html
Copyright © 2020-2023  润新知