"topic Link: NYOJ-58"
Classic Search questions, presumably this problem with a wide search will be more, so I first make is also wide search, but in fact, deep search is also possible.
If pruning is not considered, the two methods of practice consume the same, but the deep search is a little lower than the wide search memory.
I think, because the wide search needs is the queue, so compared to the recursive column more memory consumption?
Of course, DFS does not need to use the stack, as it says, but recursion is used.
BFS:
Because the BFS is to be traversed one after another, the structure is used to hold the coordinates and the number of the current
1. Each step, through the defined structure, extracts a from the queue (that is, the coordinates of the previous step, the number of steps (the number of steps per cumulative))
2. On the basis of a to determine the surrounding a four direction, to find the position can continue to walk (i.e., non-barrier, boundary), and the position of the coordinates, into the queue
To proceed to the next step, cycle the above 1.2 two-step operation.
1#include <iostream>2#include <cstdio>3#include <cstring>4#include <algorithm>5#include <queue>6 using namespacestd;7 intdir[4][2]= {1,0,-1,0,0,1,0,-1};8 structpoint{9 intX,y,step;Ten }; One intBFS (Point S,point E,intmap[9][9]){ Aqueue<point>tp;//custom type of queue - inti; -Point T;//Save current coordinates, TEMP variable the //s means before - //e indicates the target -s.step=0;//save number of steps -map[s.x][s.y]=1;//Mark here has passed +Tp.push (s);//Initialize the queue, s (x, y) initially as the starting coordinates, step = 0 - while(!tp.empty ()) {//Loop until queue is empty +S=tp.front ();//each cycle s equals the head of the team. ATp.pop ();//Delete Team Head at if(S.X==E.X&&S.Y==E.Y)//if the current coordinates are equal to the target coordinates - returnS.step;//returns the current number of steps - //Traverse four different directions - //if it is a channel (0), increase the number of steps - for(intI=0; i<4; i++){ -t.x=s.x+dir[i][0]; int.y=s.y+dir[i][1]; - if(map[t.x][t.y]==0){//if it is a channel tot.step=s.step+1; +map[t.x][t.y]=1;//Mark here has passed, and marked as a wall - Tp.push (t); the } * } $ }Panax Notoginseng } - intMain () { the intT; +scanf"%d",&t); A while(t--){ the Point s,e; + intmap[9][9]= {1,1,1,1,1,1,1,1,1, - 1,0,0,1,0,0,1,0,1, $ 1,0,0,1,1,0,0,0,1, $ 1,0,1,0,1,1,0,1,1, - 1,0,0,0,0,1,0,0,1, - 1,1,0,1,0,1,0,0,1, the 1,1,0,1,0,1,0,0,1, - 1,1,0,1,0,0,0,0,1,Wuyi 1,1,1,1,1,1,1,1,1,}; thescanf"%d%d%d%d",&s.x,&s.y,&e.x,&e.y); -printf"%d\n", BFS (S,e,map)); Wu } - return 0; About}
Dfs:
Dfs there's nothing to say, don't understand can look before the DFS blog
Only here, no single two-dimensional array see[][] is used to determine if this coordinate is already searched.
Instead of test instructions, the current position is changed to ' 1 ', which is the barrier, the boundary, then the same goal is reached when the recursion is extended for four weeks.
Of course, at the end of the DFS function, you need to change the map[][] back to ' 0 ', because the order of recursive execution is from top to bottom and back up
Because it is more than one set of test data, it is necessary to return the maze "back to the original" when recursive
#include <iostream>using namespacestd;#defineMin (a) a < b? A:bintmap[9][9] = {1,1,1,1,1,1,1,1,1, 1,0,0,1,0,0,1,0,1, 1,0,0,1,1,0,0,0,1, 1,0,1,0,1,1,0,1,1, 1,0,0,0,0,1,0,0,1, 1,1,0,1,0,1,0,0,1, 1,1,0,1,0,1,0,0,1, 1,1,0,1,0,0,0,0,1, 1,1,1,1,1,1,1,1,1,};intA,b,c,d,num; voidDfsintXintYints) { if(Map[x][y])return; if(x = = c && y = =d) {num=min (s,num); return; } s++; Map[x][y]=1; DFS (x-1, y,s); DFS (x+1, y,s); DFS (x, y-1, s); DFS (x, y+1, s); Map[x][y]=0; }intMain () {intN; CIN>>N; while(n--) {num=10000; CIN>> a >> b >> c >>D; DFS (A, B,0); cout<< Num <<Endl; } return 0;}
Dfs,bfs explain Ppt:click here | | There
"DFS/BFS" nyoj-58-minimum number of steps (Maze shortest path problem)