DFS uses recursion and does not have to use redundant data structures for simplicity. But pay attention to pruning.
BFS uses queues to find the optimal solution. The optimal solution can always be found, and pruning is also required in some cases.
These two methods are used according to the specific problem.
For example, both DFS and BFS can be used to solve the problem.
This was not the case at the beginning, and the running result was correct, but the memory was too large. Baidu used other people's methods. In fact, they only stored the values below, so I opened an dis [] [] [] array on the basis of the original one. To be honest, this is the first time I used a 3D array, I just stored the following and submitted the AC! Heheh ·······
I used to find a KFC and use BFS once, so the memory is too large. It seems that t
format is as follows:Line 1th: Two positive integers, n,m. 1≤n,m≤50, which represents the size of a small town.Next there are n lines, each with a M-lowercase letter, representing the town's map.OutputA total of k rows, each row corresponding to a set of data. At least how many pumps must be bought in order to get all the water out of the grid.Sample input9CBABCBABCCBABCBABCCBABCBABCCBABCBABC 5CCCCCCBBBCCBABCCBBBCCCCCC4Sample output12"Analysis" I wrote in B
DescriptionYou are given the pots of the volume of A and B liters respectively. The following operations can be performed:Fill (i) Fill the pot I (1≤i≤2) from the tap;DROP (i) empty the pot I to the drain;Pour (i,j) pour from pot I to pot J; After this operation either the pot J was full (and there could be some water left in the pot I), or the pot I was empty (and All its contents has been moved to the pot J).Write a program to find the shortest possible sequence of these operations that'll yie
direction is where, to divide the odd lines and even lines to discuss, If it is an odd line, then he can and (X,y-1), (x,y+1), (x-1,y-1), (X-1,y), (x+1,y-1), (x+1,y) connected, if it is an even line, it can and (x,y-1), (x,y+1), (x+1,y+1), (x+1,y ), (x-1,y+1), (x-1,y) connected, first from the initial position to do a BFS, all the same color and connected ball marker count, and then start from the first row to do a second
Title Link: BZOJ-1098Problem analysisOnly when there are edges between the two points can they be in different buildings, that is, if there is no edge between the two points they must be in the same building.Then the request is to find the original map of the connected block.However, the number of sides of the original map is n^2 level, very large, we can not directly find the complement map.You can use a linked list to optimize the BFS approach, star
Title Description Description Inside the maze of N*n, "#" is the Wall, "." For the road, "s" for the beginning, "E" for the end, altogether 4 directions can walk. From the upper left corner ((0,0) "s" position to the lower right corner ((n-1,n-1) "E") position, you can walk the general output Yes, can not go output no.Enter a description input Description The first behavior of the input is an integer m, which indicates the number of mazes.The first behavior of each maze data followed by an integ
]==0, first BFS once, the leading 0 is removed. BFs got Wx,wy.2, then is to find the smallest path. A walking count for (int i=wx+wy;i1#include 2#include 3#include 4#include 5#include 6 using namespacestd;7 #defineN 10068 intn,m;9 intMp[n][n];Ten intVis[n][n]; One structnode{ A intx, y; - }st; - intdirx[]={1,0,-1,0}; the intdiry[]={0,1,0,-1}; - intWx,wy; - voidBFs () { -QueueQ; + Q.push (ST); - Node t1,
Title Link: http://acm.hdu.edu.cn/showproblem.php?pid=1242Test instructions: "R" is the starting point, "a" is the end point, '. ' Is the road, ' X ' is the Guard, ' # ' is not the place to go. Kill the guard to spend more than 1 units of time, each move up or down to spend a unit of time. Ask for the least amount of time to finish.Idea: This n*m map and take the right (one is 1, one is 2) of the general use of bfs+ priority queue, if not with the rig
http://hihocoder.com/problemset/problem/1139This tip is written in two points, but it should also be felt, at least the topic is AC ...Two points of thought is the value of the two-point answer, see can be in the K-step, get this answer value, can use BFS to determine.Not two points, is the need for a dis[] array to save in the first k step, each point of the minimum value of the enemy, because the BFS in t
Topic Portal1 /*2 bfs+ Simulation: dp[i][j][p] means to go to i,j, the direction of the number of steps of P;3 BFS in 4 different situations, and finally in the end 4 direction to find the minimum value:)4 */5#include 6#include 7#include 8#include 9#include string>Ten#include One using namespacestd; A - Const intMAXN = 1e2 +Ten; - Const intINF =0x3f3f3f3f; the intdir[4][4][2] = { -{{0, -1}, {0,1}, {-1,0},
"BFS" hdu 1104 remainderTitle Link: Hdu 1104 remainderVery good search topic, but there are a few key issues to be aware of.
Shortest path, decisive bfs+queue
Path storage problem, only wanted to store the results of each step in the queue (int) Q, and later found that the path can not be recorded, select the way to store nodes and string to save the path, queue (node) q, open a temporary node
Title Link: http://acm.hdu.edu.cn/showproblem.php?pid=1429, the first write-like pressure, feeling can also. The idea of state representation is actually to represent the situation of each layer with a numeric value, while state compression simplifies this representation using bitwise operations.
ExercisesIn the current state to add a key value, indicating that the number of keys taken now, the key value is actually a binary representation, 001 to get a key, 1011 to get a, B, d these thre
The positive solution is Tarjan, I did not writeWith two BFs, the first BFS is done on the back of the original image, starting with N and finding all the points that can be reached from N.The second BFS starts from the starting point, saving the minimum value mp[i] on each point to the N-point path.Finally, the maximum value of w[i]-mp[i] can be calculated by tr
Test Instructions:Link * * Method: **bfs+ Shortest circuitparsing:This problem is still very interesting. The first idea gets stuck, the first one is two cents +bfs, but if so, what can be done? To a point of traffic. So you have to change your mind. You may want to redefine the shortest path. Consider all 0 of the points in the diagram as feasible, and each time you search from that point to 0 points, and
clockwise rotation of a lattice, such as can be transformed to 17245368Give you the original state of the magic plate and the target State, please give a minimum number of transformation steps from the initial state to the head, if there are many transformation schemes to take the smallest dictionary order.The test data of input contains two lines, representing the initial state and the state of the magic Plate respectively.Output satisfies the test instructions transformation step for each set
Topic Portal1 /*2 BFS: three-dimensional BFS, plus direction. Use Dp[x][y][d] to record the minimum number of turns currently required3 */4#include 5#include 6#include 7#include 8#include 9 using namespacestd;Ten One Const intMAXN = 1e2 +Ten; A Const intINF =0x3f3f3f3f; - structP - { the intx, y, Z; - }now, to; - CharMAZE[MAXN][MAXN]; - intdp[maxn][maxn][4]; + BOOLinq[maxn][maxn][4]; - intdx[4] = {-1,1
lines describing the map. Assume both N and M are between 2 and inclusive. There would be the same number of ' H ' s and ' M's on the map; And there'll is at the most houses. Input would terminate with 0 0 for N and M.Output:For each test case, the output one line with the single integer, which are the minimum amount, in dollars, and you need to pay.Sample Input:2 2.mh.5 5HH.. m...............mm. H7 8...H ..... H....... H....mmmhmmmm ... H....... H....... H.... 0 0Sample Output:21028Test instru
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.