HDU 1728 BFS deformation minimum turn number __BFS

Source: Internet
Author: User
HDU 1728
Background: WA 29 times, finally, with the help of other people's ideas.

The first idea is to store each position of the visit array as the minimum number of turns required to reach the current coordinate point, and finally to the minimum number of turns to reach that point for each of the traversed points. The minimum turning points are also handed down to a minimum turning points .... This layer is deferred until the end point.

The second way: in the ordinary case of the BFS is every step of the distance of 1, find all the next step into the queue, and then this layer of recursion, and in this case we bfs each point out of the four points in all directions to join the queue, so that these points are only one more turn, want to be in the distance plus one turns number plus one. To find the shortest distance, add one step at a time as a BFS variable.
To find the minimum turn number, add one turn each time as a BFS variable.
Good harmony.

First Idea code:

#include <map> #include <set> #include <stack> #include <queue> #include <vector> #include <cstdio> #include <cstring> #include <cstdlib> #include <iostream> #include <algorithm> #
Define M 209 #define INF 100000000 #define LL Long int using namespace STD;
int t,n,m,k,x1,x2,y1,y2,ans=inf,dir[4][2]={-1,0,0,1,1,0,0,-1};
Char Maps[m][m];
int visit[m][m]; struct Place{int x,y,step,last;}

TEMP1,TEMP2;
     void BFs (void) {temp1.x=x1;
     Temp1.y=y1;
     Temp1.step=0;
     Temp1.last=-1;
     Queue<place> Q;
     Q.push (TEMP1);
         while (!q.empty ()) {Temp1=q.front ();
         Q.pop ();
         if (Temp1.step > K) continue;
            if (temp1.x = = x2 && temp1.y = = y2) {if (Temp1.step < ans) ans=temp1.step;
         if (ans <= k) return; for (int i=0;i < 4;i++) {if (temp1.x+dir[i][0) > 0 && temp1.x +dir[i][0] <= N & & Temp1.y+dir[i][1] >0 && temp1.y+dir[i][1] <= m) {temp2.x=temp1.x+dir[i][0];
                 TEMP2.Y=TEMP1.Y+DIR[I][1];
                 if (temp1.last!=-1 && temp1.last!= i) temp2.step=temp1.step+1;
                 else Temp2.step=temp1.step;
                 temp2.last=i; if (maps[temp2.x][temp2.y] = = '. ' && temp2.step <= visit[temp2.x][temp2.y]) {visit[temp2.x]
                     [Temp2.y]=temp2.step;
                 Q.push (TEMP2);
    int main (void) {scanf ("%d", &t);
        while (t--) {ans=inf;
        memset (visit,100000000001,sizeof (visit));
        for (int i=0;i < m;i++) for (int j=0;j < m;j++) Visit[i][j]=inf;
        printf ("visit:%d%d\n", visit[0][0],visit[1][2]);
        scanf ("%d%d", &n,&m); GetChar ();
            for (int i=1;i <= n;i++) {for (int j=1;j <= m;j++) {scanf ("%c", &maps[i][j]);
          }  GetChar ();
        } scanf ("%d%d%d%d%d", &k,&y1,&x1,&y2,&x2);
        BFS ();
        if (ans <= k) printf ("yes\n");
    else printf ("no\n");
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.