"BFS" Oliver's rescue _struct

Source: Internet
Author: User
 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;
 }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.