HDU5335 Work out Hierarchy traversal

Source: Internet
Author: User

Topic Links:

HDu5335




Test instructions

1000X1000 's map asks what is the smallest binary sequence that goes through (1000,1000) through four directions.




Problem Solving Ideas:

First, you should find a value of 0 and closest to the lower-right corner of the BFS (x+y value is the largest, with more than one saved).

This will ensure that the next sequence is the shortest.


The next best strategy for each step is only two possible: right or down, that is, the next layer of new nodes are on the same slash

If you encounter 0, then you must go to save, and if the current layer has 0 and 1, then the node with 1 does not need to continue to expand

At the end of the search, the minimum value of the current layer is output each time, and the smallest binary sequence can be obtained.



Code:

#include <iostream> #include <cstdio> #include <cstring> #include <queue> #include <vector > #define MAXN 1005using namespace std;struct node{int x, y;} head,t;int map[maxn][maxn];int Vis[maxn][maxn];int dir[ 4][2]= {1,0,0,1,-1,0,0,-1};int n,m;int ok () {if (t.x<1| | t.y<1| | t.x>n| | t.y>m| |    VIS[T.X][T.Y]) return 0; return 1;}    void BFs () {vector<node>w;    int maxx=0;    memset (vis,0,sizeof (VIS));    queue<node>q;   Q.push (node) {0,1});        Starting from outside the graph while (!q.empty ()) {Head=q.front ();        Q.pop ();                if (Head.x+head.y>maxx) {if (head.x==n&&head.y==m)//has a path that is all 0 {                printf ("0\n");            Return            } maxx=head.x+head.y;            W.clear ();        W.push_back (head);        } if (Head.x+head.y==maxx) W.push_back (head);            for (int i=0; i<4; i++) {t.x=head.x+dir[i][0]; T.y=head.y+dIR[I][1]; if (!ok () | | |            Map[t.x][t.y]==1) continue;            Vis[t.x][t.y]=1;        Q.push (t);    }} vector<node>q2[2];    int now=0,next=1;    int flag;    if (map[1][1]!=1) {for (int i=0; i<w.size (); i++) Q2[0].push_back (W[i]);    } else//does not have a leading 0 q2[0].push_back ((node) {0,1});        while (1)//hierarchy traversal {flag=0;            for (int i=0;i<q2[now].size (); i++) {head=q2[now][i];                for (int i=0;i<2;i++) {t.x=head.x+dir[i][0];                T.Y=HEAD.Y+DIR[I][1]; if (!ok () | | |                VIS[T.X][T.Y]) continue;                Vis[t.x][t.y]=1;                    if (t.x==n&&t.y==m) {printf ("%d\n", Map[t.x][t.y]);                Return                  } if (map[t.x][t.y]==0) {if (!flag)      Q2[next].clear ();                    flag=1;                Q2[next].push_back (t);            } else if (!flag&&map[t.x][t.y]==1) q2[next].push_back (t);        }} q2[now].clear ();        if (flag)//The layer has 0 printf ("0");        else printf ("1");    Swap (Now,next);    }}int Main () {//Freopen ("In.txt", "R", stdin);    int T;    Char STR[MAXN];    int Len;    scanf ("%d", &t);        while (t--) {scanf ("%d%d", &n,&m);            for (int i=1; i<=n; i++) {scanf ("%s", str+1);            Len=strlen (str+1);        for (int j=1; j<=len; j + +) map[i][j]=str[j]-' 0 ';    } BFS (); } return 0;}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

HDU5335 Work out Hierarchy traversal

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.