HDU 2102 A plan (three-dimensional labyrinth BFS)

Source: Internet
Author: User

Topic Links:

pid=2102 "> Portal

Test instructions

A labyrinth of three dimensions, starting at the first level of S (0,0,0), asking if it is possible to go to the second layer of p within the specified time

Secretariat ' * ' means not to go, '. ' The representative can walk, ' # ' on behalf of the portal, here is a trick, walk to the portal

Must be transmitted. I didn't notice the WA many times before.

And in the initial time to be able to deal with the map, (' * ', ' # '), (' # ', ' * '), (' # ', ' # ')this affirmation

is not able to walk, so can turn them all into ' * '

The code is as follows:

#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include < queue>using namespace Std;const int maxn = 50;char mp[2][maxn][maxn];int vis[2][maxn][maxn];int dx[4]= {1,-1,0,0};int    dy[4]= {0,0,-1,1};int n,m,limit;struct nod {int x, y, Z;    int step; Nod () {} nod (int _x,int _y,int _z): X (_x), Y (_y), Z (_z) {}};bool check (nod tmp) {if (vis[tmp.z][tmp.x][tmp.y]) return FA    Lse if (tmp.x>=0&&tmp.x<n&&tmp.y>=0&&tmp.y<m&&mp[tmp.z][tmp.x][tmp.y]!=    ' * ') return true; return false;}    BOOL BFs () {nod st=nod (0,0,0);    Vis[0][0][0]=1;    St.step=0;    Queue<nod> Q;    Q.push (ST); while (!        Q.empty ()) {nod tmp = Q.front ();        cout<<tmp.z<< "<<tmp.x<<" "<<tmp.y<<" "<<tmp.step<<endl;        Q.pop ();        if (mp[tmp.z][tmp.x][tmp.y]== ' P ' &&tmp.step<=limit) {return true; } else if (Mp[tmp.z][tmp.x][tmp.y]== ' P ') {return false;            } for (int i=0; i<4&&mp[tmp.z][tmp.x][tmp.y]!= ' # '; i++) {nod top;            top.x=tmp.x+dx[i],top.y=tmp.y+dy[i],top.z=tmp.z,top.step=tmp.step+1;                if (check (top)) {Q.push (top);            Vis[top.z][top.x][top.y]=1;            }} if (mp[tmp.z][tmp.x][tmp.y]== ' # ') {tmp.z^=1;            Q.push (TMP);        Vis[tmp.z][tmp.x][tmp.y]=1; }} return false;}    int main () {int t;    scanf ("%d", &t);        while (t--) {scanf ("%d%d%d", &n,&m,&limit);        for (int i=0; i<n; i++) scanf ("%s", Mp[0][i]);        for (int i=0; i<n; i++) scanf ("%s", Mp[1][i]); for (int i=0, i<n; i++) {for (int j=0; j<m; J + +) {if (mp[0][i][j]== ' # ' &&mp[1][i][j]                = = ' # ') mp[0][i][j]=mp[1][i][j]= ' * ';                if (mp[0][i][j]== ' # ' &&mp[1][i][j]== ' * ')    mp[0][i][j]=mp[1][i][j]= ' * ';            if (mp[0][i][j]== ' * ' &&mp[1][i][j]== ' # ') mp[0][i][j]=mp[1][i][j]= ' * ';        }} memset (Vis,0,sizeof (VIS));        if (BFS ())) puts ("YES");    Else puts ("NO"); } return 0;}


HDU 2102 A plan (three-dimensional labyrinth BFS)

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.