hdu1180 strange staircase bfs

Topic links: http://icpc.njust.edu.cn/Problem/Hdu/1180/

Bfs have different topics and different places is the presence of sideways or vertically stairs, stairs with every passing moment to change the time if he had to, but only one can spend time 1 position other end of stairs to reach by stairs. So we just look at the position of the current encounter stairs toward the stairs, and orientation to determine whether the same person on the line, it takes the same time 1, then spent 2 different time.

code show as below:

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 typedef unsigned int ui;
 4 typedef long long ll;
 5 typedef unsigned long long ull;
 6 #define pf printf
 7 #define mem(a,b) memset(a,b,sizeof(a))
 8 #define prime1 1e9+7
 9 #define prime2 1e9+9
10 #define pi 3.14159265
11 #define lson l,mid,rt<<1
12 #define rson mid+1,r,rt<<1|1
13 #define scand(x) scanf("%llf",&x) 
14 #define f(i,a,b) for(int i=a;i<=b;i++)
15 #define scan(a) scanf("%d",&a)
16 #define dbg(args) cout<<#args<<":"<<args<<endl;
17 #define inf 0x3f3f3f3f
18 #define maxn 25
19 int n,m,t;
20 int sx,sy,tx,ty;
21 int dir[][2]={1,0,-1,0,0,1,0,-1};// horizontal and vertical 
22 is  BOOL VIS [MAXN] [MAXN];
 23 is  char the Map [MAXN] [MAXN];
 24  struct Node {
 25      int X, Y, STEP;
 26 is      Node ( int X, int Y, int S): X (X), Y (Y), STEP (S)} {
 27      Node () {}
 28      Friend BOOL  operator < (Node A, Node B)
 29      {
 30          return a.step> b.step; // Since priority top of the queue is the maximum, so that a minimum defined by the inverse operator is a top step 
31 is      }
 32  };
 33 is  Node CUR, NXT;
34 bool ok(node a)
35 {
36     return a.x>0&&a.x<=n&&a.y>0&&a.y<=m&&Map[a.x][a.y]!='*'&&!vis[a.x][a.y];
37 }
38 int bfs()
39 {
40     mem(vis,false);
41     priority_queue<node>q;
42     q.push(node(sx,sy,0));
43     vis[sx][sy]=1;
44     while(!q.empty())
45     {
46         cur=q.top();
47         q.pop();
48         if(cur.x==tx&&cur.y==ty)
49         {
50             return cur.step;
51         }
52         f(i,0,3)
53         {
54             nxt=cur;
55             nxt.x+=dir[i][0];
56             nxt.y+=dir[i][1];
57             nxt.step++;
58             if(nxt.x<1||nxt.x>n||nxt.y<1nxt.y ||> m || the Map [nxt.x] [nxt.y] == ' * ' ) Continue ;
 59              IF (VIS [nxt.x] [nxt.y]) Continue ;
 60              IF (the Map [ nxt.x] [nxt.y] == ' | ' || the Map [nxt.x] [nxt.y] == ' - ' ) // As long as there are stairs through principle, did not miss any one state 
61 is              {
 62 is                  char C = the Map [nxt.x] [nxt.y]; // Note that changing the value Map [cur.x] [cur.y], to keep it original state 
63 is                  IF (& nxt.step 1 ) // number of steps required for the transformation odd stairs 
64                  {
 65                      IF(C == ' | ' ) C = ' - ' ;
 66                      the else C = ' | ' ;
 67                   } 
 68                  nxt.x + = the dir [I] [ 0 ];
 69                  nxt.y + = the dir [I] [ . 1 ];
 70                  IF ((C == ' | ' && (I <= . 1 )) || (C == ' - ' && (I> . 1 ))) // when the stairs is a longitudinal transverse longitudinal or transverse stairs 
71                  {
 72                      nxt.step ++;
 73 is                  }
 74                  IF (OK (NXT)!) Continue ;
 75                  IF (nxt.x < . 1 || nxt.x> n-nxt.y || < . 1 || nxt.y> m || the Map [nxt.x ] [nxt.y] == ' * ' ) Continue ;
 76                  IF (VIS [nxt.x] [nxt.y]) Continue ; // every state (position) should change inquiry access record 
77              }
 78              VIS [nxt.x] [nxt.y] = . 1 ; // note that each disposed in a state to be enqueued access record 
79              q.push (NXT);
 80          }
 81      }
 82 }
83 int main()
84 {
85     //freopen("input.txt","r",stdin);
86     //freopen("output.txt","w",stdout);
87     std::ios::sync_with_stdio(false);
88     while(scanf("%d%d",&n,&m)!=EOF)
89     {
90         f(i,1,n)
91             f(j,1,m)
92             {
93                 scanf(" %c",&Map[i][j]);
94                 if(Map[i][j]=='S')sx=i,sy=j;
95                 if(Map[i][j]=='T')tx=i,ty=j;
96             }
97         pf("%d\n",bfs());
98     }
99  } 

 

Guess you like

Origin www.cnblogs.com/randy-lo/p/12509935.html