USACO 2017 January Contest Gold T3: Cow Navigation

Subject to the effect

Bessie mistake FJ himself trapped in the side of the barn. Because of her poor eyesight, she needs your help in the rescue.

Is a plan view of a barn checkered, some grid (i.e., units) is empty, then the other is not through the stack of firewood. Bessie start from the bottom left corner (square 1,1) want to move all the way to the top right corner. You can guide her, telling her a sequence of instructions, instructions may be "forward", "turn left 90 degrees", "turn right 90 degrees." You need to be able to make her come to the shortest instruction sequence used to reach the destination. If you leave the barn or to instruct Bessie firewood pile, she does not move, it will skip to the next command sequence.

Unfortunately, Bessie did not know she was a direction towards the beginning of (or may be on the right), and the sequence without regard to this case, (making bessie either up or to the right, in accordance with the instructions given can reach end.)

Note: to the end will not be moved again

 (2N20)

Topic analysis

Because N is small, and for this problem, we do not know anything, so consider search.

For a start, do not know is upward or to the right, so we are vis-dimensional array to open six were recorded case of a start up or right.

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 const int MAXN=25;
 4 
 5 int dx[5]={-1,0,1,0};
 6 int dy[5]={0,1,0,-1};
 7 
 8 int n;
 9 bool blk[MAXN][MAXN],vis[MAXN][MAXN][4][MAXN][MAXN][4];
10 int dis[MAXN][MAXN][4][MAXN][MAXN][4];
11 struct Node{
12     int x,y,d;
13     inline bool Ok(){return x>=1&&x<=n&&y>=1&&y<=n&&!blk[x][y];}
14 };
15 struct q_Node{
16     Node p1,p2;
17     void mrk(){vis[p1.x][p1.y][p1.d][p2.x][p2.y][p2.d]=true;}
18     bool IsMrked(){return vis[p1.x][p1.y][p1.d][p2.x][p2.y][p2.d];}
19     int &get(){return dis[p1.x][p1.y][p1.d][p2.x][p2.y][p2.d];}
20 }q1,p,nxt;
21 queue<q_Node> q;
22 inline void Move(Node &d,Node f,int w){
23     d=f;
24     if(w==0){
25         if(d.x==1&&d.y==n) return;
26         d.x+=dx[d.d];
27         d.y+=dy[d.d];
28         if(!d.Ok()) d=f;
29     }
30     else if(w==1){
31         ++d.d;
32         if(d.d==4) d.d=0;
33     }
34     else if(w==2){
35         --d.d;
36         if(d.d==-1) d.d=3;
37     }
38 }
39 int main(){
40     memset(dis,0x3f,sizeof(dis));
41     scanf("%d",&n);
42     for(int i=1;i<=n;++i){
43         getchar();
44         for(int j=1;j<=n;++j)
45             blk[i][j]=(getchar()=='H');
46     }
47     q1.p1.x=q1.p2.x=n;
48     q1.p1.y=q1.p2.y=q1.p2.d=1;
49     q1.get()=0;
50     q.push(q1);
51     while(!q.empty()){
52         p=q.front();
53         q.pop();
54         int w=p.get();
55         for(int i=0;i<3;++i){
56             Move(nxt.p1,p.p1,i);
57             Move(nxt.p2,p.p2,i);
58             int &nd=nxt.get();
59             if(nd>w+1){
60                 nd=w+1;
61                 if(!nxt.IsMrked()){
62                     q.push(nxt);
63                     nxt.mrk();
64                 }
65             }
66         }
67     }
68     int ans=0x3f3f3f3f;
69     for(int i=0;i<4;++i)
70         for(int j=0;j<4;++j)
71             ans=min(ans,dis[1][n][i][1][n][j]);
72     printf("%d\n",ans);
73     return 0;
74 }

 

Guess you like

Origin www.cnblogs.com/LI-dox/p/11228397.html