HDOJ 1180

梯子可以重复走。想不明白。

  1 #include <iostream>
  2 #include <cstdio>
  3 #include <cmath>
  4 #include <cstring>
  5 #include <algorithm>
  6 #include <queue>
  7 using namespace std;
  8 struct node
  9 {
 10     int x,y,step;
 11     bool operator<(const node &a)const
 12     {
 13         return step>a.step;
 14     }
 15 }sta;
 16 char a[25][25];
 17 int vis[25][25],n,m,dir[5][2]={-1,0,1,0,0,-1,0,1};
 18 void bfs()
 19 {
 20     node temp,next;
 21     priority_queue<node>q;
 22     q.push(sta);
 23     while(!q.empty())
 24     {
 25         temp=q.top();q.pop();
 26         if(a[temp.x][temp.y]=='T')
 27         {
 28             printf("%d\n",temp.step);
 29             return;
 30         }
 31         for(int i=0;i<4;i++)
 32         {
 33             next.x=temp.x+dir[i][0];
 34             next.y=temp.y+dir[i][1];
 35             next.step=temp.step+1;
 36             if(next.x>=0&&next.y>=0&&next.x<n&&next.y<m&&vis[next.x][next.y]==0)
 37             {
 38                 if(a[next.x][next.y]=='.'||a[next.x][next.y]=='T')
 39                 {
 40                     q.push(next);
 41                     vis[next.x][next.y]=1;
 42                     continue;
 43                 }
 44                 else if(a[next.x][next.y]=='|')
 45                 {
 46                     if(temp.step%2==0)
 47                     {
 48                         node po;
 49                         po.x=next.x+dir[i][0];
 50                         po.y=next.y+dir[i][1];
 51                         if(i==0||i==1)
 52                         {
 53                             if(po.x>=0&&po.y>=0&&po.x<n&&po.y<m&&vis[po.x][po.y]==0)
 54                             {
 55                                 po.step=next.step;
 56                                 q.push(po);
 57                                 vis[po.x][po.y]=1;
 58                             }
 59                         }
 60                         else
 61                         {
 62                             if(po.x>=0&&po.y>=0&&po.x<n&&po.y<m&&vis[po.x][po.y]==0)
 63                             {
 64                                 po.step=next.step+1;
 65                                 q.push(po);
 66                                 vis[po.x][po.y]=1;
 67                             }
 68                         }
 69                     }
 70                     else
 71                     {
 72                         node po;
 73                         po.x=next.x+dir[i][0];
 74                         po.y=next.y+dir[i][1];
 75                         if(i==2||i==3)
 76                         {
 77                             if(po.x>=0&&po.y>=0&&po.x<n&&po.y<m&&vis[po.x][po.y]==0)
 78                             {
 79                                 po.step=next.step;
 80                                 q.push(po);
 81                                 vis[po.x][po.y]=1;
 82                             }
 83                         }
 84                         else
 85                         {
 86                             if(po.x>=0&&po.y>=0&&po.x<n&&po.y<m&&vis[po.x][po.y]==0)
 87                             {
 88                                 po.step=next.step+1;
 89                                 q.push(po);
 90                                 vis[po.x][po.y]=1;
 91                             }
 92                         }
 93                     }
 94                 }
 95                 else if(a[next.x][next.y]=='-')
 96                 {
 97                     if(temp.step%2==0)
 98                     {
 99                         node po;
100                         po.x=next.x+dir[i][0];
101                         po.y=next.y+dir[i][1];
102                         if(i==2||i==3)
103                         {
104                             if(po.x>=0&&po.y>=0&&po.x<n&&po.y<m&&vis[po.x][po.y]==0)
105                             {
106                                 po.step=next.step;
107                                 q.push(po);
108                                 vis[po.x][po.y]=1;
109                             }
110                         }
111                         else
112                         {
113                             if(po.x>=0&&po.y>=0&&po.x<n&&po.y<m&&vis[po.x][po.y]==0)
114                             {
115                                 po.step=next.step+1;
116                                 q.push(po);
117                                 vis[po.x][po.y]=1;
118                             }
119                         }
120                     }
121                     else
122                     {
123                         node po;
124                         po.x=next.x+dir[i][0];
125                         po.y=next.y+dir[i][1];
126                         if(i==0||i==1)
127                         {
128                             if(po.x>=0&&po.y>=0&&po.x<n&&po.y<m&&vis[po.x][po.y]==0)
129                             {
130                                 po.step=next.step;
131                                 q.push(po);
132                                 vis[po.x][po.y]=1;
133                             }
134                         }
135                         else
136                         {
137                             if(po.x>=0&&po.y>=0&&po.x<n&&po.y<m&&vis[po.x][po.y]==0)
138                             {
139                                 po.step=next.step+1;
140                                 q.push(po);
141                                 vis[po.x][po.y]=1;
142                             }
143                         }
144                     }
145                 }
146             }
147         }
148     }
149 }
150 int main(int argc, char *argv[])
151 {
152     int i,j;
153     while(scanf("%d%d",&n,&m)!=EOF)
154     {
155         memset(vis,0,sizeof(vis));
156         for(i=0;i<n;i++)
157             scanf("%s",a[i]);
158         for(i=0;i<n;i++)
159         {
160             for(j=0;j<m;j++)
161             {
162                 if(a[i][j]=='*')
163                     vis[i][j]=1;
164                 if(a[i][j]=='S')
165                 {
166                     sta.x=i;sta.y=j;sta.step=0;
167                     vis[i][j]=1;
168                 }
169             }
170         }
171         bfs();
172     } 
173     return 0;
174 }

猜你喜欢

转载自www.cnblogs.com/huluxin/p/9357754.html