HDU-2259-Continuous Same Game (2) (BFS + DFS + simulation), dfsbfs
Problem 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 <= n, m <= 20), which is the size of the board. then n lines follow, each contains m characters, indicating the color of the block. there are 5 colors, and each with equal probability.
OutputFor each test case, first output a single line containing the number of steps S. then S lines follow, each contains two integers indicating the position of an arbitrary block of the group you want to remove.
For the I-th test case, let Ai is the total point of your scheme and Bi is the total point of the greedy strategy. then I will calculate the average of (Ai/Bi), 1 <= I <= 100. you will get WA if your ouput is invalid or this average is less than 1.5.
Sample Input
5 53555231154332222113412314
Sample Output
52 20 01 12 14 4Hint35552 35000 00000 00000 00000 0000031154 31552 05552 00002 00002 0000033222 33154 01154 01104 00004 0000021134 21134 21134 21134 20034 2003012314 12314 12314 12314 12314 12312The total point is 6+12+12+12+6=48.
Idea: BFS is used in general, and DFS is used when the grid is eliminated. Each status is evaluated by a value val, which is maintained by a priority queue. Each time, the element with the greatest evaluation value is queued and the answer is updated. The problem does not require the optimal solution. You can solve the problem about 25 times.
# Include <cstdio> # include <algorithm> # include <queue> using namespace std; struct S {char mp [20] [21]; int step, ans, x [50], y [50], val; int num [20] [20]; bool operator <(const S & p) const {return val <p. val ;}t, ans, tt; int n, m, nxt [4] [2] ={{}, {}, {-}, {0, -1 }}, cnt; char temp [20] [21]; bool vis [20] [20]; void dfs (int x, int y, char c) {int I, j; for (I = 0; I <4; I ++) {x + = nxt [I] [0]; y + = nxt [I] [1]; if (x> = 0 & x <n & y> = 0 && Y <m &&! Vis [x] [y] & temp [x] [y] = c) {vis [x] [y] = 1; cnt ++; dfs (x, y, c) ;}x-= nxt [I] [0]; y-= nxt [I] [1] ;}} void tran (int x, int y, char num, char (* d) [21]) {for (int I = 0; I <4; I ++) {x + = nxt [I] [0]; y + = nxt [I] [1]; if (x> = 0 & x <n & y> = 0 & y <m & d [x] [y] = num) {d [x] [y] = '0'; tran (x, y, num, d);} x-= nxt [I] [0]; y-= nxt [I] [1] ;}} void move (char (* d) [21]) {int I, j, k; for (I = n-1; i> = 0; I --) // move down {for (j = 0; j <m; j ++) {if (d [I] [j] = '0 ') {For (k = I-1; k> = 0; k --) {if (d [k] [j]> '0 ') {d [I] [j] = d [k] [j]; d [k] [j] = '0'; break ;}}}}} int t = m-1; while (t --) // move to the left. Note that the two consecutive columns are empty. {for (j = 0; j <m-1; j ++) {for (I = 0; I <n; I ++) if (d [I] [j]> '0') break; if (I = n) {for (I = 0; I <n; I ++) {d [I] [j] = d [I] [j + 1]; d [I] [j + 1] = '0' ;}}} void eval (S & node) // evaluate {int I, j; for (I = 0; I <n; I ++) for (j = 0; j <m; j ++) vis [I] [j] = 0, temp [I] [j] = node. mp [I] [j]; node. val = node. ans; for (I = 0; I <n; I ++) for (j = 0; j <m; j ++) {node. num [I] [j] = 0; if (temp [I] [j]> '0 '&&! Vis [I] [j]) {vis [I] [j] = 1; cnt = 1; dfs (I, j, temp [I] [j]); if (cnt> 1) {node. num [I] [j] = cnt; node. val + = cnt * (cnt-1) ;}}} int main () {int I, j, p, q, tot; while (~ Scanf ("% d", & n, & m) {for (I = 0; I <n; I ++) scanf ("% s", t. mp [I]); t. step = 0; t. ans = 0; t. val = 0; ans. ans = 0; priority_queue <S> que; eval (t); que. push (t); tot = 0; while (! Que. empty () {t = que. top (); if (t. ans> ans. ans) ans = t; // update tot ++; if (tot> 24) break; // exit que after 25 cycles. pop (); for (I = 0; I <n; I ++) for (j = 0; j <m; j ++) {if (t. num [I] [j]) {tt = t; tran (I, j, t. mp [I] [j], t. mp); // clear the grid t. mp [I] [j] = '0'; move (t. mp); // move the grid t. x [t. step] = I; t. y [t. step] = j; t. step ++; t. ans + = t. num [I] [j] * (t. num [I] [j]-1); eval (t); // re-evaluate que. push (t); t = tt ;}} printf ("% d \ n", ans. step); for (I = 0; I <ans. step; I ++) printf ("% d \ n", ans. x [I], ans. y [I]) ;}}
C language BFS, DFS Function Code
There is no fixed form.
Write according to the specific situation
The key is thinking.
Bfs expands nodes before increasing depth
Dfs adds depth first, returns to the end, and then expands the node.
One is to use a large amount of space, and the other is to traverse all paths, which is more time-consuming.
Q: What is the deep Priority Search mark in ACM? PS: I have learned graphs in the data structure, and I know both DFS and BFS.
Nodes with contribution values can be recursive.
The following is an example:
# Includes
Included
Class TreeNode {
Public: BR/> TreeNode * left;
'S TreeNode * rights;
Int value;
TreeNode (V): Value (V)
{
Left = NULL; BR/> right-click = NULL;
}
TreeNode (){
(Left! = NULL) delete left;
(Right! = NULL) delete permission; BR p>}
};
// Add p to the current node of the tree. If the p value is smaller than the current node, set the left subtree to true.
Invalid addToTree (* CURR of TreeNode, * p of TreeNode ){
(P-value ){
(CURR-> left = NULL ){
CURR-> left = P;
}
Other addToTree (CURR-> left, P );
} Others {
(CURR-> right-click = NULL ){
CURR-> right-click = P;
}
Other addToTree (CURR->;, P); ....../>}
}
Invalid printTree (* p of TreeNode ){
(P = NULL) return;
PrintTree (P->; left );
Output ("% d", P-value );
PrintTree (P-> right );
}
Invalid primary (){
Interpret a [] = {3, 4, 1, 2, 5 };
Tree node * root = new TreeNode ([0]);
(I = 1; I <5; I ++ ){
TreeNode * p = new TreeNode (A [I]);
AddToTree (root, P );
}
PrintTree (Root); // print the node value of the sequential tourism tree.
Delete the root directory;
}