hdoj 1242

 1 #include <iostream>
 2 #include <queue>
 3 #define inf 0x7ffffff
 4 using namespace std;
 5 char map[203][203];
 6 int visit[203][203];
 7 int n, m, di, dj;
 8 int dir[4][2] = {{1, 0},{-1, 0},{0, 1},{0, -1}};
 9 struct node
10 {
11     int i, j;
12     int cnt;
13     int f;
14 };
15 bool check(node s)
16 {
17     if (s.i >= 0 && s.i < n && s.j >= 0 && s.j < m && !visit[s.i][s.j] && map[s.i][s.j] != '#')
18         return 1;
19     return 0;
20 }
21 int bfs(int i, int j)
22 {
23     queue<node> q;
24     node pre, last;
25     pre.i = i, pre.j = j, pre.cnt = 0, pre.f = 0;
26     visit[i][j] = 1;
27     q.push(pre);
28     memset(visit, 0, sizeof(visit));
29     int min = inf;
30     while (!q.empty())
31     {
32         pre = q.front();
33         q.pop();        
34         if (pre.f == 1)
35         {
36             pre.cnt++;
37             pre.f = 0;
38             q.push(pre);
39             continue;
40         }
41         if (pre.i == di && pre.j == dj)
42             return pre.cnt;
43         for (i = 0; i < 4; i++)
44         {
45             last.i = pre.i + dir[i][0];
46             last.j = pre.j + dir[i][1];
47             last.cnt = pre.cnt + 1;
48             last.f = 0;
49             if (check(last))
50             {
51                 if (map[last.i][last.j] == 'x')
52                     last.f = 1;
53                 visit[last.i][last.j] = 1;
54                 q.push(last);
55             }
56         }
57     }
58     return 0;
59 }
60 int main()
61 {
62     int si, sj, i, j;
63     while (scanf("%d %d", &n, &m) != -1)
64     {
65         for (i = 0; i < n; i++)
66         {
67             for (j = 0; j < m; j++)
68             {
69                 cin >> map[i][j];
70                 if (map[i][j] == 'r')
71                     si = i, sj = j;
72                 else if (map[i][j] == 'a')
73                     di = i, dj = j;
74             }
75         }
76         int temp = bfs(si, sj);
77         if (!temp)
78             printf("Poor ANGEL has to stay in the prison all his life.\n");
79         else
80             printf("%d\n", temp);
81     }
82     return 0;
83 }

posted on 2012-08-14 22:02  Xor<>OR  阅读(178)  评论(0)    收藏  举报

导航