Z1. Breadth-first search (BFS) problem-solving ideas

/ ** 
the BFS solving idea 
features: Starting from certain nodes, infection adjacent nodes; node infection, reinfection of its adjacent nodes, and so on. 
Title common data structure comprising a two-dimensional array, tree, FIG. 
** / 

/ ** 
1). Specific two-dimensional array of nodes infected adjacent nodes, i.e., vertical and horizontal four directions, may be set as the array changes 
int [] dr new new int = [] {-. 1, 0,. 1, 0}; 
int [] = DC new new int [] {0, -1, 0,}. 1; 
2) a specific two-dimensional array of nodes infected node surrounding it. i.e. eight methods, an array of change may be set as follows: 
int [] = DR new new int [] {-. 1, -1, -1, 0, 0,. 1,. 1,. 1}; 
int [] = DC new new int [ ] {- 1, 0, 1, -1, 1, -1, 0, 1}; 
** / 

object // BFS is by traversing the particular node and the node may be infected, in order to calculate the required results. May seek the maximum and minimum, it may be statistical number, and so on. 
// this defines an internal class to store the coordinates of each node and the calculation results. 
the Node {class 
	int R & lt; 
	int C; 
	int Val; 
	public the Node (R & lt int, C int, int Val) { 
		this.r = R & lt; 
		this.c = C; 
		this.val = Val; 
	} 
} 
// definition of a queue (FIFO, first in first out) to store all the nodes involved 
Queue <the Node> = new new Queue the LinkedList <the Node> (); 
// traverse the two-dimensional array Grid 
int = R & lt grid.length, C = Grid [0] .length; 
for (int = 0 R & lt; R & lt <R & lt; R & lt ++) { 
	for (int C = 0; C <C; C ++) { 
		// isNeedToAdd actual implementation, typically by an index value and the actual values of the two factors 
		IF (isNeedToAdd (R & lt, C, Grid [R & lt] [C])) { 
			int Val = ...; // calculate the results 
			queue.add (new new the Node (R & lt, C, Val)); 
		} 
	} 
} 
BFS // 
// Note: BFS does not necessarily come directly to the final answer, it might get a final answer pre variables, where the need to analyze how the results obtained from the BFS what can be converted to the final answer. 
// define the final outcome variables ANS 
int ANS = 0; 
the while (! Queue.isEmpty ()) { 
	the Node Node queue.poll = (); 
	for (int = 0 K; K <. 4; K ++) { 
		int = NR Node. r + dr [k];
		NC = node.c DR + int [K];  
		// first need to determine the boundary
		IF (NR> = 0 && NR <&& R & lt NC> = 0 && NC <C) { 
			// memorandum is determined: In order not to repeat the processing nodes already searched the 
			// memorandum there are several treatment methods: changing the value of a node had been searched, a value judgment, this method does not take up additional storage space; 2. use a hash table. 
			IF (memoExist (NR, NC, Grid [NR]! [nc])) {// memorandum does not exist continues 
				addToMemo (); // Add memo 
				int nval = ....; // calculate the results 
				ans = ....; // intermediate results calculated final results , for example, selecting the maximum value = Math.max ANS (ANS, nVal); 
				queue.add (new new the Node (NR, NC, nVal)) 
			} 
		} 
	} 
}

  If a lot of problems out analysis using breadth-first search (BFS) to solve, need to think a few good following questions:

  • Node class design, the need to analyze the characteristics of good data
  • Analysis of the initial data a particular node, which is added to the queue. If the initial queue behind the need to use the data, consider storing two copies, one for searching neighboring nodes (using the queue storage), one for the latter to give a final answer (to select the data structure based on the actual problem).
  • How to analyze a particular node is converted to an adjacent node.
  • What analysis can get results from the BFS, how these results into the final answer (the most critical step)
  • Note that the border issue and use of the memo, avoid errors and duplication searches search.

  What issues suitable for use BFS:

  • Seeking a shortest path from the root node
  • Infection adjacent (usually this kind of topic can also use a depth-first algorithm DFS)

Guess you like

Origin www.cnblogs.com/zlxyt/p/11403358.html