HDU-2259-Continuous same game (2) (BFS + DFS + simulation)

Source: Internet
Author: User
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]) ;}}

HDU-2259-Continuous same game (2) (BFS + DFS + simulation)

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.