problem description "with your help, Oliver finally caught up with little X, but one day, the bad guys took the little x." This is when Oliver's hero saves the Beautiful. So, Oliver found a dream, borrowed a machine, the machine shows a map of the grid, it tells Oliver where to go, where not to go. And Oliver is in the lower-right corner of the map, and Little X is in the upper-left corner. Time is urgent, Oliver wants to know how many squares to go at least to find small x.
(Only go straight).
The input format sos.in a total of n+1 rows, the first behavior n, the following n rows n columns 0-1 matrices, 1 means they cannot pass, and 0 can pass (the upper-left and lower-right corners are 0).
The "output format" sos.out a number for the least number of walking squares.
"Sample Input" "Sample output" 5 9 0 1 1 1 1 0 0 1 1 1 1 0 0 0 1 1 1 1 0 1 1 1 1 0 "data range" for 0 of the data, n<30.
#include <iostream> #include <cstdio> #include <cstring> #include <ctime> #include <cmath > #include <cstdlib> #include <algorithm> #define LOC #define MXN 30+10 #define MXQ 200000 using Namespac
e std;
int n,m;
int MP[MXN][MXN];
BOOL V[MXN][MXN];
const int MX[5]={0,1,0,-1};
const int my[5]={1,0,-1,0};
struct nd {int x,y;
int stp;
}Q[MXQ];
int l,r;
inline void BFs () {int x,y;
L=r=1;
V[1][1]=true;
Q[r].x=q[r].y=1,q[r].stp=1;
while (L<=r) {for (int i=0;i<4;++i) {x=q[l].x+mx[i],y=q[l].y+my[i];
if (x==n&&y==n) {printf ("%d", q[l].stp+1);
Exit (0); } if (x>=1&&x<=n&&y>=1&&y<=n&&!v[x][y]&&!mp[x
][y]) {r= (r+1)%mxq;
V[x][y]=true; Q[r].x=x, q[r].y=y;
q[r].stp=q[l].stp+1;
} l= (l+1)%mxq;
int main () {#ifdef loc freopen ("sos.in", "R", stdin);
Freopen ("Sos.out", "w", stdout);
#endif scanf ("%d", &n);
for (int i=1;i<=n;++i) for (int j=1;j<=n;++j) scanf ("%d", &mp[i][j]);
BFS ();
return 0;
}