Title AddressBeginner BFS, the first time to use BFS to do the problem. The problem is a basic BFS model, which requires a little attention to +1 of the guard time, and the shortest time rather than the number of steps.1#include 2#include 3#include 4 #defineS 2055 #defineINF 1000006 using namespacestd;7 structLo//build a structure, store point coordinates, and so
Breadth FirstDescription: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 (nOutput:The output require
Question Link
Http://acm.hdu.edu.cn/showproblem.php? PID = 1, 1548
The number of elevators on each layer. For example, the number of floors on the N layer is K,
Therefore, this layer can only be of the upper or lower K layers, but cannot be lower than or higher than N layers,
Given the start point and end point, the minimum number of keys required
My idea: You can use BFS or dijsktra)
BFS
AC code
# Include
of mod 2 obtains each bit of K (0 or 1)What about it?
The answer is yes.
First, we useSame modulus TheoremOptimize the method for getting the remainder
(A * B) % N = (a % N * B % N) % N
(A + B) % N = (a % N + B % N) % N
Code1:
1 # Include 2 # Include 3 # Include String . H> 4 # Include 5 Using Namespace STD; 6 7 Int MoD [ 600000 ]; 8 Int Res [ 200 ]; 9 10 Int Main () 11 { 12 Int N; 13 While (~ Scanf ( " % D " , N ) N) 14 { 15 MoD [ 1
Poj 3278 Catch That Cow (bfs search), pojbfsCatch That Cow
Time Limit:2000 MS
Memory Limit:65536 K
Total Submissions:46715
Accepted:14673
Description
Farmer John has been informed of the location of a fugitive cow and wants to catch her immediately. He starts at a pointN(0 ≤N≤ 100,000) on a number line and the cow is at a pointK(0 ≤K≤ 100,000) on the same number line. Farmer John has two modes of transportation: w
in the dictionary and get a complete picture of the structure. According to the requirements of the topic, equivalent to the shortest path of one vertex to another in this graph, we usually use breadth first. This problem, we can only use the simplest way to do, each change the word of a letter, and then gradually search, the shortest path, the minimum depth of the tree with BFS most suitable. Take a look at the time
The word adjacent t
Reprint please indicate the source, thank you http://blog.csdn.net/ACM_cxlove? Viewmode = contents by --- cxlove
The first write bidirectional BFs.
Bidirectional BFS searches from the start and end points. If the intersection points appear, the target point is reachable. This requirement is to arrive within eight steps. Each step has 16 states, and the complexity of 16 ^ 8 is still very high. Because the st
statuses of the magic board, please give the transformation steps from the initial state to the smallest number of changes in the object state. If there are multiple conversion schemes, take the smallest Lexicographic Order.
Each group of input test data includes two rows, representing the initial and object states of the magic board.
Output converts the output of each group of test data to meet the requirements of the question.
Sample Input
12345678172453681234567882754631
Sample output
CAC
Au
one. I used brute force to solve the question. I always decided whether I could divide the question by two. Then I also wanted to use brute force at the beginning, but later I do some of the circumstances can not be considered, this question is also more than the copy of the http://blog.csdn.net/whjkm/article/details/26985421 (the previous question of The Link), and then carefully read the question, there is still a big difference with that question. The simple question is that it starts from 0
Question Link: Http://acm.hdu.edu.cn/showproblem.php? PID = 1, 1180
Theme: There are a bunch of stairs in the maze, and the stairs change horizontally and vertically. These stairs change to the opposite state at an odd number of times, and the next point in the forward direction will be reached by the way through the stairs (skip the stairs ).
At the same time, you can wait in the same place and ask the minimum time to reach the destination.
Solutions:
An interesting question.
Or
Tags: ACM Wang Guiping Graph Theory
Find a center in all cities to meet the maximum and minimum output distance between the center and all bus stations and the center with the minimum and minimum distance numbers.
The most basic BFS for each bus station BFS dis [I] indicates the number of I to the maximum distance of all bus stations BFS after all sites dis [I] m
Question: Give a map. Sun Wukong (k) saves Tang Monk (T). The map's indicates a snake. It takes up to five dead snakes a minute, '1 '~ 'M' indicates M keys (M
Solution: BFs. Each node maintains four values:
X, Y: Current Coordinate
Key: Key keys have been collected.
Step: How many steps have been taken
S: The access status of the snake. (2 ^ 5 indicates that the number of snakes has been killed. If 1 is set to 1, the snake has been killed)
Then, take
. Space is the place where I can go. # No, It is around # Note: it can only be separated in S or a. the space area cannot be split.Algorithm: min spanning tree + BFS idea: Build a graph and convert it to Min spanning tree, then directly apply the prime or Kruskal TemplateBecause only the S and a parts can be separated, S and a are the same, use BFs to traverse S and each A to find the shortest distance betw
Test instructions to give two four-bit primes A, B, each time only a certain number of a can be modified to make it another four-bit prime, ask at least how many operations, can make a change to B. BFS search can be done directly1#include 2 using namespacestd;3 4 BOOLIsPrime (intN) {//Prime Judgment5 if(n = =2|| n = =3)return true;6 Else{7 intK = sqrt (n) +1;8 for(inti =2; I ){9 if(n% i = =0)return false;Ten
: Bessie can pass through the open space 1: Inaccessible areas due to various reasons 2: Betsy's position now 3: the Knights ' location 4: The land output of the shrubs which Bessie needed Outputs a positive integer d, that is, how many days it will take for Bessie to finish the task given by the Knights. Sample INPUT8 44 1 0 0 0 0 1 00 0 0 1 0 1 0 00 2 1 1 3 0 4 00 0 0 4 1 1 1 0INPUT DETAILS:Width=8, height=4. Bessie starts on the third row, only a few squares awayFrom the Knights.Sample Outp
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,
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.