DFS-hdu-2821-Pusher

Source: Internet
Author: User

Question link:

Http://acm.hdu.edu.cn/showproblem.php? PID = 1, 2821

Question meaning:

To a matrix of N * n, some locations are empty, and some locations have boxes (A represents a box, B represents two, and so on ). Let you select an empty position as the starting point, and then select a direction (top, bottom, left, right) for each step until it hits the box, and then remove the box at this position, all the remaining boxes are merged to the next position. Requirement: It must be separated from the box by more than one position before it can be moved.

Find a starting position to remove all the boxes and output the route.

Two points of attention after data Detection: 1. No case where the edge position exceeds a box; 2. Ensure that there is a solution.

Solution:

Start position of enumeration. DFS searches deeply. If a path can be used to remove all the boxes, the output is displayed.

Pay attention to saving the Back-to-site (do not use global variables to store the site, because recursive calls will overwrite the original saved site, WA several times ).

Code:

# Include <iostream> # include <cmath> # include <cstdio> # include <cstdlib> # include <string> # include <cstring> # include <algorithm> # include <vector> # include <map> # include <set> # include <stack> # include <list> # include <queue> # define EPS 1e-6 # define INF 0x1f1f1f1f # define PI ACOs (-1.0) # define ll _ int64 # define lson L, M, (RT <1) # define rson m + 1, R, (RT <1) | 1 // # pragma comment (linker, "/Stack: 1024000000,1024000000") using names Pace STD;/* freopen ("data. in "," r ", stdin); freopen (" data. out "," W ", stdout); */Char save [30] [30], save1 [30] [30]; int C, R, dir [4] [2] = {-}, {0,-1}; int Lim, CNT; char di [4] = {'U', 'R', 'D', 'L'}; struct inf {int X, Y;} s; bool iscan (INF & TT, int dd) {int xx = TT. X + dir [DD] [0], YY = TT. Y + dir [DD] [1]; If (XX <0 | XX> = r | YY <0 | YY> = C) // the next step is unavailable, no return false; If (save1 [XX] [YY]! = '. ') // The next step is not to return false; while (save1 [XX] [YY] = '. ') // walk in this direction until it is near the box {xx = xx + dir [DD] [0], YY = YY + dir [DD] [1]; if (XX <0 | XX> = r | YY <0 | YY> = C) // go out, no return false ;} if (save1 [XX] [YY] = 'A') // This location only has one box {CNT ++; save1 [XX] [YY] = '. '; TT. X = XX, TT. y = YY; return true;} else // There are multiple boxes in this location {int x = xx + dir [DD] [0], y = YY + dir [DD] [1]; If (x <0 | x> = r | Y <0 | Y> = C) // If the edge has multiple boxes {// CNT ++; // TT. X = XX, TT. y = YY; // save1 [XX] [YY] = sa Ve1 [XX] [YY]-1; // return true; // both write methods are supported. Other writing methods are also supported, because the test data does not exist. Return false ;} CNT ++; TT. X = XX, TT. y = YY; If (save1 [x] [Y]! = '. ') // If the next position is not. then, directly merge save1 [x] [Y] = save1 [x] [Y] + save1 [XX] [YY]-'A '; // note-'A' else // The next position is. then, you can directly obtain save1 [x] [Y] = save1 [XX] [YY]-1; save1 [XX] [YY] = '. '; return true ;}} bool flag; string an; void DFS (INF cur, string ans) {If (FLAG) // a path has been found: return; char TT [30] [30]; // note that the local variable for (INT I = 0; I <4; I ++) must be used to save the field) // follow the four directions {memcpy (TT, save1, sizeof (save1); inf tmp = cur; int temp = CNT; // when tracing is convenient, others are not changed/* If (TEST) {for (Int J = 0; j <r; j ++) printf ("% d % s \ n", J, save1 [J]);} */If (! Iscan (TMP, I) continue;/* If (TEST) {printf ("% d-> % d CNT: % d \ n", cur. x, cur. y, TMP. x, TMP. y, CNT); putchar ('\ n'); For (Int J = 0; j <r; j ++) printf ("% d % s \ n ", j, save1 [J]);} */string TM = ans; TM + = di [I]; If (CNT = lim) // find a path that can be completely moved {An = TM; flag = true; return;} DFS (TMP, Tm); CNT = temp; // trace back to memcpy (save1, TT, sizeof (TT);} int main () {While (~ Scanf ("% d", & C, & R) {Lim = 0; For (INT I = 0; I <r; I ++) {scanf ("% s", save [I]); For (Int J = 0; j <C; j ++) if (save [I] [J]! = '. ') Lim + = (save [I] [J]-'A' + 1); // count the number of boxes} // putchar (' \ n '); flag = false; For (INT I = 0; I <R &! Flag; I ++) for (Int J = 0; j <C &&! Flag; j ++) {If (save [I] [J]! = '. ') // Start position of the enumeration. Note that the start position is not continue; S. X = I, S. y = J; CNT = 0; memcpy (save1, save, sizeof (SAVE); DFS (S, ""); If (FLAG) {printf ("% d \ n", I, j); cout <An <Endl ;}} return 0 ;}

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.