HDU-1180-诡异的楼梯

题目传送门:http://acm.hdu.edu.cn/showproblem.php?pid=1180

 

 

 

 

折腾了一下午,最后用优先队列做,这个应该也是UCS搜索吧。灵敏搜索!

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

View Code
  1 #include<iostream>
  2 #include<queue>
  3 #include <algorithm>   
  4 #include<string>
  5 using namespace std;
  6 
  7 const int Max = 25;
  8 char s[Max][Max];
  9 int ax, ay, ex, ey, mx, my;
 10 int n, m;
 11 const int to[4][2] = {0,1, 0,-1, 1,0, -1,0};   
 12 //{0,-1,0,1,1,0,-1,0};
 13 
 14 struct Node
 15 {
 16     Node(int i=-1, int j=-1):x(i), y(j){}
 17     bool operator==(const Node &n)const
 18     {
 19         return n.x == x && n.y == y;
 20     }
 21     bool operator<(const Node &n)const
 22     {
 23         return n.sum < sum;
 24     }
 25     int x, y;
 26     int sum;
 27 }path[Max][Max];
 28 
 29 Node S, T;
 30 
 31 int BFS()
 32 {
 33     char c;
 34     int k=0;
 35     priority_queue<struct Node>Q;
 36     Node go, st;
 37     S.sum = 0;
 38     T.sum = 0;
 39     path[T.x][T.y] = T;
 40     Q.push(S);
 41     while(!Q.empty())
 42     {
 43         st = Q.top();
 44         path[st.x][st.y] = st;
 45         Q.pop();
 46         if(st == T)
 47             break;
 48         for(int i=0; i<4; i++)
 49         {
 50             go = st;
 51             go.x += to[i][0]; 
 52             go.y += to[i][1];
 53             if(go.x>=0 && go.x<n && go.y>=0 && go.y<m && s[go.x][go.y]!='*')
 54             {    
 55                 if(s[go.x][go.y] == '.')
 56                 {
 57                     go.sum=st.sum+1;
 58                     s[go.x][go.y] = '*';
 59                     Q.push(go);
 60                 }
 61                 else 
 62                 {
 63                     if(st.sum & 1)  //奇数
 64                     {
 65                         if('|' == s[go.x][go.y])
 66                             c = '-';
 67                         else
 68                             c = '|';
 69                     }
 70                     else
 71                         c = s[go.x][go.y];
 72                     if(c == '|' && go.y != st.y)
 73                     {
 74                         go.sum =st.sum+1;
 75                         c = '-';
 76                     }
 77                     else if(c == '-' && go.x != st.x)
 78                     {
 79                         go.sum =st.sum+1;
 80                         c = '|';
 81                     }
 82                     if(c == '|')
 83                     {
 84                         if(go.x < st.x)
 85                             --go.x;
 86                         else if(go.x > st.x)
 87                             ++go.x;
 88                     }
 89                     else 
 90                     {
 91                         if(go.y < st.y)
 92                             --go.y;
 93                         else if(go.y > st.y)
 94                             ++go.y;
 95                     }
 96                     go.sum ++;
 97                     if(go.x>=0 && go.x<n && go.y>=0 && go.y<m && s[go.x][go.y]!='*')
 98                         Q.push(go);
 99                 }
100             }
101         }
102     }
103     return path[T.x][T.y].sum;
104 }
105 
106 int main()
107 {
108     int t;
109     while(cin>>n>>m)
110     {
111         for(int i=0; i<n; i++)
112         {
113             scanf("%s", s[i]);
114             for(int j=0; j<m; j++)
115             {
116                 if(s[i][j] == 'S')
117                 {
118                     S = Node(i, j);
119                     s[i][j] = '*';
120                 }
121                 if(s[i][j] == 'T')
122                 {
123                     T = Node(i, j);
124                     s[i][j] = '.';
125                 }
126             
127             }
128         }
129         cout<<BFS()<<endl;
130     }
131     return 0;
132 }
133 
134 
135 /*
136 
137 20 20
138 ***...**.*.*.*.*...*
139 ..*....**.**.*.*.*..
140 ..*...*.*..T..*.*.**
141 .**.*..-...*.**.*.*.
142 *...**.***...**..***
143 ...*....|...|.**..**
144 .*.-..**.**.*.**.**.
145 .*.**.**.**..***.**.
146 S.*****....**.**.*..
147 ***...**.*.*.*.*...*
148 ..*..*.**.**.*.*.*..
149 ..*..**.*..*..*.*.**
150 .**.**..**.*.**.*.*.
151 *..***.**.*..**..***
152 ..**..........**..**
153 .*.**.**.**.*.**.**.
154 .****.**.**..***.**.
155 .*.**.**.**.*.**.**.
156 .****.**.**..***.**.
157 .****.**.**..***.**.
158 
159 
160 
161 */

 

posted @ 2012-08-28 19:35  另Ⅰ中Feel▂  阅读(171)  评论(0编辑  收藏  举报