[explanation] [usaco2007 OCT] Objective course-C++

Keywords: PHP

subject
Description
Consider a n x n (1 < = n < = 100) square pasture with 1 square grid. Some of the squares that cows can't step on are marked 'x'.
For example, the following figure:

. . B x .
. x x A .
. . . x .
. x . . .
. . x . .

Bessie found herself at point A. she wanted to add salt to the salt block at point B. Slow and clumsy animals, such as cows, hate to turn. Nevertheless, of course, they will turn when necessary. For a given pasture, please calculate the minimum number of turns from a to B. At the beginning, Bessie could face either direction. Bessie knew she would arrive.
Input

Line 1: an integer N
Row 2... N + 1: line i+1 has n characters ('.,' x ',' A ',' B '), indicating the status of each point.
Output
Line 1: an integer with the minimum number of turns.
Sample Input
3
.xA
...
Bx.
Sample Output
2
thinking
It's said that this problem can be solved in the order of violent search + metaphysical dir array, AC, but I haven't tried it.
Positive solution: BFS plus a little memory.
What's more, it should be noted that the shortest path does not represent the minimum number of turns, but the later search may be better, of course, it may not, so we need to add a memory search;
Memorization: f[i][j][k] is used to indicate that in (i,j) direction k(0-3 represents a direction respectively), each time k is changed to search, and finally output the minimum number of turns in four directions with the end point as the coordinate. The vis array marked in the process should also be opened into three-dimensional, otherwise it is not convenient to mark.

Code

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 int n,sx,sy,tx,ty,ans=0x3f3f3f3f;
 4 char a[110][110],s[110];
 5 int f[110][110][4],dx[4]={0,0,1,-1},dy[4]={1,-1,0,0};
 6 bool vis[110][110][4];
 7 // 0  1  2  3
 8 //Right, left, up and down 
 9 struct node
10 {
11     int x,y,dir;
12 };
13 queue <node> q;
14 int min_(int a,int b,int c,int d)
15 {
16     if (a>b) a=b;
17     if (a>c) a=c;
18     if (a>d) a=d;
19     return a;
20 }
21 void init()
22 {
23     for(int i=0;i<110;i++)
24         for(int j=0;j<110;j++)
25             for(int k=0;k<4;k++)
26                 f[i][j][k]=0x3f3f3f3f;
27     f[sx][sy][0]=0;
28     f[sx][sy][1]=0;
29     f[sx][sy][2]=0;
30     f[sx][sy][3]=0;
31     vis[sx][sy][0]=1;
32     vis[sx][sy][1]=1;
33     vis[sx][sy][2]=1;
34     vis[sx][sy][3]=1;
35     q.push((node){sx,sy,0});
36     q.push((node){sx,sy,1}); 
37     q.push((node){sx,sy,2});
38     q.push((node){sx,sy,3});
39     
40 }
41 bool not_in(int x,int y)
42 {
43     return x<1||x>n||y<1||y>n||a[x][y]=='x';
44 }
45 void bfs()
46 {
47     init();
48     while(!q.empty())
49     {
50         node now=q.front();
51         q.pop();
52         vis[now.x][now.y][now.dir]=false;
53         for (int i=0;i<4;i++)
54         {
55             int x=now.x+dx[i],y=now.y+dy[i];
56             if(not_in(x,y))continue;
57             int l;
58             if(i==now.dir)l=0;
59             else l=1;
60             if (f[x][y][i]>f[now.x][now.y][now.dir]+l)
61             { 
62                 f[x][y][i]=f[now.x][now.y][now.dir]+l;
63                 if(!vis[x][y][i])
64                     vis[x][y][i]=1,q.push((node){x,y,i});
65             }
66         }
67     }
68 }
69 int main()
70 {
71     cin>>n;
72     for(int i=1;i<=n;i++)
73     {
74         for(int j=1;j<=n;j++)
75         {
76             cin>>a[i][j];
77             if(a[i][j]=='A')sx=i,sy=j;
78             if(a[i][j]=='B')tx=i,ty=j;
79         }
80     }
81     bfs();
82     ans=min_(f[tx][ty][0],f[tx][ty][1],f[tx][ty][2],f[tx][ty][3]);
83     if(ans!=0x3f3f3f3f)
84         cout<<ans<<endl;
85     else cout<<-1<<endl;
86     return 0;
87 }

Posted by Collin on Mon, 21 Oct 2019 13:21:59 -0700