HDU 1175 (BFS) repeatedly look

Source: Internet
Author: User

The title is here: http://acm.hdu.edu.cn/showproblem.php?pid=1175

We are very familiar with the repeatedly look, the principle is basically this, a typical search. Here is a wide-search.

Unlike a normal search, a line that requires a transition cannot transition more than two times, that is, one more step (the number of times to store the transition) and one dir (the direction of the record) in the struct.

The direction is initially 1, and when the direction of the walk is different from before the step, step should add one,

It is also important to note that to ensure that all routes have the least number of transitions, the visit array here is used to hold the minimum number of transitions for each point,

So the initialization should be a big

Specific look at the code

1#include <iostream>2#include <cstring>3#include <cstdio>4#include <queue>5#include <climits>6 using namespacestd;7 intmap[1005][1005],visit[1005][1005];8 structPoint {9     intx, y;Ten     intstep,dir;//Record the number and direction of transitions One } ; A intdx[4]={0,0,1,-1}; - intdy[4]={1,-1,0,0}; - intN,m,ex,ey; the intBFsintSxintSy) - { -     inti; -Queue<point>Q; + Point Next,now; -now.x=sx;now.y=Sy; +now.step=0; Anow.dir=-1; at Q.push (now); -      while(!q.empty ()) -     { -now=Q.front (); - Q.pop (); -         if(now.x==ex&&now.y==ey) in             return 1; -          for(i=0;i<4; i++) to         { +next.x=now.x+Dx[i]; -next.y=now.y+Dy[i]; thenext.step=Now.step; *Next.dir=i; $            if(next.dir!=now.dir&&now.dir!=-1)//change the direction to add one, but the first step is not a turning pointPanax Notoginsengnext.step++; -            if((next.x<=n&&next.x>=1) && (next.y<=m&&next.y>=1) && (map[next.x][next.y]==0|| (next.x==ex&&next.y==ey ))) the            { +                if(next.step<3) A                { the                    if(next.step<Visit[next.x][next.y])//Let the visit array save the minimum number of transitions to each point +                    { -visit[next.x][next.y]=Next.step; $ Q.push (next); $                    } -                } -            } the         } -     }Wuyi     return 0; the } - intMain () Wu { -     intI,j,t,sx,sy; About      while(~SCANF ("%d%d",&n,&m)) $     { -         if(n==0&&m==0) -              Break; -          for(i=1; i<=n;i++){ A              for(j=1; j<=m;j++){ +scanf"%d",&map[i][j]); the             } -         } $scanf"%d",&t); the          while(t--) the         { theCin>>sx>>sy>>ex>>ey; the             if(sx==ex&&sy==ey) -             { inprintf"no\n"); the                 Continue; the             } About             if(map[sx][sy]==0|| map[ex][ey]==0|| map[sx][sy]!=Map[ex][ey])//Don't miss out on these special cases the             { theprintf"no\n"); the                 Continue; +             } -              for(i =1; I <= N; i++) the              for(j =1; J <= M; J + +)BayiVISIT[I][J] =10000;//Initialize to a very large number the             if(BFS (sx,sy) = =1) theprintf"yes\n"); -             Else -printf"no\n"); the         } the  the     } the     return 0; -}

HDU 1175 (BFS) repeatedly look

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.