Author: July January 1, 2011
---------------------------------
My reference: Introduction to AlgorithmsMy statement: Personal Original, reprinted please indicate the source.
OK.
There are many articles on such BFS and DFS algorithms on the Internet. However, there is no such reason.After reading this article, I think,You will have a thorough understanding of the breadth-first search and depth-first search of graphs.
---------------------
We started wi
HDU-2259-Continuous Same Game (2) (BFS + DFS + simulation), dfsbfsProblem DescriptionAfter repeated attempts, LL finds the greedy strategy is very awful in practice. even there is no apparent evidence to proof it is better than a random one. so he has to drop this strategy and try to discover a better one.InputThere are 100 test cases. each test case begins with two integers n, m (5 OutputFor each test case, first output a single line containing the n
Link: zoj 3820 building fire stations
Given a tree, select two gas stations and ask the minimum value of the distance between all points and the gas station, and output a method of gas station establishment.
Solution: The second distance is used to determine the complexity of the function is O (n). The complexity should be O (nlogn). Even if the constant coefficient is too large, the system ran 4.5 S, it's also drunk.The judgment function performs BFS
Http://poj.org/problem? Id = 2965
The 16 locations have two states, which are represented by a 16-bit binary. Beg is obtained by bitwise OR calculation. In BFs, use a hexadecimal number that contains only 0, 1 to implement the flip operation.
The rest is simply BFs.
Code:# Include # Include # Include Using namespace STD;
Int dir [16] = {0x111f, 0x222f, 0x444f, 0x888f,
0x11f1, 0x22f2, 0x44f4, 0x88f8,
0x1f11,
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
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.