hdoj1180 诡异的楼梯(bfs+奇偶判断)

手癌!日常手癌!被自己气死!

 1 #include<iostream>
 2 #include<cstring>
 3 #include<queue>
 4 #include<algorithm>
 5 #define maxn 25
 6 using namespace std;
 7 const int dx[4]={-1,1,0,0},dy[4]={0,0,-1,1};
 8 struct point{
 9     int x,y;
10     int time;
11 }start;
12 char map[maxn][maxn];
13 int v[maxn][maxn];
14 int n,m,ans;
15 int check(point next){
16     if (map[next.x][next.y]=='*' || next.x<0 || next.x>=n || next.y<0 || next.y>=m || v[next.x][next.y]) return 1;
17     else return 0;
18 }
19 int bfs(point start){
20     queue<point> Q;
21     Q.push(start);
22     v[start.x][start.y]=1;
23     while (!Q.empty()){
24         point pre=Q.front();
25         Q.pop();
26         if (map[pre.x][pre.y]=='T'){
27             return pre.time;
28         }
29         for (int i=0;i<4;i++){
30             point next;
31             next.x=pre.x+dx[i];
32             next.y=pre.y+dy[i];
33             next.time=pre.time+1;
34             if (check(next)) continue;
35             if (map[next.x][next.y]=='.' || map[next.x][next.y]=='T'){
36                 Q.push(next);
37                 v[next.x][next.y]=1;
38             }
39             else if (map[next.x][next.y]=='|' && !(pre.time&1) || map[next.x][next.y]=='-' && (pre.time&1)){  //表示横着走 
40                 if (i>1){
41                     next=pre;
42                     next.time++;
43                     Q.push(next);
44                     continue; 
45                 }
46                 next.x+=dx[i];
47                 if (check(next)) continue;
48                 Q.push(next);
49                 v[next.x][next.y]=1; 
50             }
51             else {  // 表示竖着走 
52                 if (i<2){
53                     next=pre;
54                     next.time++;
55                     Q.push(next);
56                     continue;
57                 }
58                 next.y+=dy[i];
59                 if (check(next)) continue;
60                 Q.push(next);
61                 v[next.x][next.y]=1;
62             }
63         }
64     }
65 }
66 int main(){
67     while (cin >> n >> m){
68         for (int i=0;i<n;i++){
69             for (int j=0;j<m;j++){
70                 cin >> map[i][j];
71                 if (map[i][j]=='S'){
72                     start.x=i;
73                     start.y=j;
74                     start.time=0; 
75                 }
76             }
77         }
78         memset(v,0,sizeof(v));
79         ans=bfs(start);
80         cout << ans << endl;
81     }
82     return 0;
83 }

 

posted @ 2018-03-13 15:46  Changer-qyz  阅读(150)  评论(0编辑  收藏  举报