Challenge Program 2.1.5 Exhaustion Search >> Width First search (practice POJ3669)

Source: Internet
Author: User

Compare DFS and BFS first

Depth-First Search Dfs width first search BFS

You can clearly see that the search order is different.

DFS is to search the bottom of a single path, and then backtrack.

BFS is the search near the state, until the bottom, generally in solving the shortest path or minimum number of steps to apply.

BFS to use the queue it.

The use of queues take a look at http://blog.csdn.net/cindywry/article/details/51919282

  

Practice series ... ..... ... .... ..... ..... ..... ..... ....... .........................

Title: poj3669

Because of the number of steps (foldback) The problem was one day.

If that point can go, then push it into the queue and wait for the search.

1#include <stdio.h>2#include <queue>3#include <string.h>4 using namespacestd;5 intmap1[305][305],step[305][305];6typedef pair<int,int>P;7 structstu{8 intXintYintT;9 };Ten structStu ch[50002]; One  A intdx[5]={0,1,0, -1,0}; - intdy[5]={0,0,1,0, -1}; -  the voidZhaintM) { -memset (MAP1,-1,sizeof(MAP1)); -      for(inti =0; i < M; i++) -          for(intK =0; K <5; k++) {//Five Points is burst. +             intNX = ch[i].x +Dx[k]; -             intNY = ch[i].y +Dy[k]; +             if(NX >=0&& NX <303&& NY >=0&& NY <303&& (ch[i].t < Map1[ny][nx] | | map1[ny][nx] = =-1)) AMAP1[NY][NX] = ch[i].t;//give Everypoint the minimum burst time. at         } - } -  - intBFs () { -Queue <P>que; -Que.push (P (0,0)); inmemset (step,-1,sizeof(step)); -step[0][0]=0; to  while(Que.size ()) { +P p=Que.front (); - Que.pop (); the         if(Map1[p.first][p.second] = =-1){//find Map1 ' s-1 and print it *             returnStep[p.first][p.second]; $         }Panax Notoginseng     if(Step[p.first][p.second] >= Map1[p.first][p.second])Continue;//The point had Benn Burst,so jump it -      for(inti =1; I <5; i++) {//This is the point of four diretions, does not been destroy. the         intNX = P.first + Dx[i],ny = P.second +Dy[i]; +         if(nx>=0&& NY >=0&&step[nx][ny] = =-1)//if it can arrive and does not exceed the map A             { theQue.push (P (NX, NY));//Push It to the queue and go +step[nx][ny]=step[p.first][p.second]+1;//of Course,step must add 1 when go. -             } $     } $ } - return-1; - } the  - intMain ()Wuyi { the     intM,a; -      while(~SCANF ("%d",&M)) Wu     { -           for(intI=0; i<m; i++) Aboutscanf"%d%d%d",&ch[i].x,&ch[i].y,&ch[i].t); $ Zha (M); -A=BFS (); -printf"%d\n", a); -     } A     return 0; +}

Challenge Program 2.1.5 Exhaustion Search >> Width First search (practice POJ3669)

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.