Breadth First
Description:
Ali trapped in the maze, Snoopy to save him, Snoopy can go up, down, left, right four directions, each walk step (lattice) will drink a bottle of more power. Now give it a maze map. May I ask: how many bottles of Snoopy do you need at least to get out of the maze?
Input:
First enter a number T, indicating the number of test data, the following input is the T maze, each maze input should contain the following data, enter the size of the maze of N (n<=15), the size of the maze is n*n. Then enter the maze, with the capital letter "S" to indicate the position of the Snoopy, with the lowercase letter "E" to indicate the location of Ali trapped, with "." Indicates a blank, with "*" for the barrier, you know Ali and Snoopy are only one.
Output:
The output requires the least amount of benefits of the number of bottles m (data guarantee there must be a minimum number of benefits required for many bottles)
Sample Input:
2
8
S.. *....
.*...**.
.. *.**..
.*.. *.. *
*.. *e.**
........
.***.. *.
....*...
8
S.. *....
.*...**.
.**.**..
.*.. *.. *
*.. **.**
........
.***.. *.
....*.. E
Sample Output:
12
16
Test instructions Analysis: This problem is the simplest to find the shortest distance of the topic, that is, the typical BFS problem.
Breadth First search:http://baike.baidu.com/view/288267.htm If you do not want to see such a long text explanation, you can look at the following ppt:http://www.docin.com/ P-542536008.html
Below we continue to analyze this topic, we select the second set of data for analysis. First, I'll start by storing the data from the array labeled [1][1]. Let's create a wall around the map ( Figure 1). We also need to open an array of the same size as the map visit to record the points that have been traversed. Initialize all points to 0 and pass the dots to 1.
Fig. 1 Fig. 2 Fig. 3
The coordinates of s are then recorded. Queue S and Mark Visit[s.x][s.y]=1 to see if the S point is equal to E, the first element pops out, or the result is returned. Then look at the upper and lower left and right 4 points adjacent to S (up and down in order), if the point is not a wall (ie ' * ') and has not traversed (visit[that point. x][the point. Y] equals 0), the point is enqueued, and the point is marked visit[. x][the point. y]=1; after the adjacent points are viewed, then take the first element of the team to see if the point equals e .... (Repeat the steps in the green section until the queue is empty). For ease of understanding, the above is affixed with Figure 2 and Figure 3. The number in the figure is the position where the nth step arrives.
The source code is posted below:
1#include <cstdio>2#include <cstring>3#include <queue>4 using namespacestd;5 Charmap[ -][ -];//Map6 BOOLvisit[ -][ -];//used to record status7 intdir[][2]={{1,0},{0,1},{-1,0},{0,-1}};//direction8 struct Point9{//record point coordinates and number of stepsTen intx,y,cnt; One }; A voidBFS (intSxintSy) - { -Queue<point>A; the Point now,next; -now.x = SX; Now.y = sy; now.cnt =0; - A.push (now); -Visit[sx][sy] =1; + while(!a.empty ()) - { +Point temp =A.front (); A.pop (); A if(MAP[TEMP.X][TEMP.Y] = ='E') at { -printf"%d\n", temp.cnt); - return ; - } - for(inti =0; I <4; i++) - { inNext.x = temp.x+dir[i][0]; -Next.y = temp.y+dir[i][1]; tonext.cnt = temp.cnt+1; + if((!visit[next.x][next.y]) && (map[next.x][next.y]!='*')) -{A.push (next); Visit[next.x][next.y] =1;} the } * } $ return ;Panax Notoginseng } - intMainvoid) the { + intI,j,t,m,n; Ascanf"%d",&t); the while(t--) + { - intSx,sy; $memset (Visit,0,sizeof(visit)); $scanf"%d",&n); - for(i =1; I <= N; i++) -scanf"%s", &map[i][1]); the for(i =0; I <= (n+1); i++) - for(j =0; J <= (n+1); J + +)Wuyi { the if(Map[i][j] = ='S') -SX = i, sy =J; Wu if((i==0) || (j==0) || (i==n+1) || (j==n+1)) -MAP[I][J] ='*'; About } $ BFS (sx,sy); - } - - return 0; A}View Code
This is my first search question, did an afternoon, just really understand BFS. In the past also has not been BFS, look at other people's code is very long, and some can not understand. Or the above red link in the PPT to help a lot, do not understand more than a few times to see PPT. Here is the PPT I do can also look at: http://files.cnblogs.com/files/Muia/%E8%BF%B7%E5%AE%AB.ppt
Breadth First search (BFS)