CSU 1119 collecting coins

Source: Internet
Author: User

BFS + DFS

Complex search questions.

Because the data is small, there are up to five rock databases, up to 10 coin databases, and up to 4 ^ 5 = 1024 states for moving rock;

Ideas:

Take the coin that can be obtained in the current status and set the current location of the map to '.' (the location of the coin to be removed is empty)

After coin is taken away, you can search for it once. When rock is found to determine whether it can be pushed, you can create a new map and set the Rock Point '. '(null). The rock point to be moved is set to 'X' (only one time can be moved ). In this way, the recursion goes down, because there are only five rock S, and the most specific layer is 5.

Compare the current status map with the previously obtained maximum value after each scan, and obtain a greater margin (sometimes more coin is obtained without moving rock !)

 

PS: I started to set a variable to be lazy, and the result was not worth the candle. It took a long time to get stuck... It seems that you should be careful in the future. Do not change the previous values at will. You 'd better not make such a low-level mistake by hitting more...

1 # include <iostream> 2 # include <cstring> 3 # include <cstdio> 4 # include <algorithm> 5 # include <cmath> 6 # include <queue> 7 using namespace std; 8 9 char map [1050] [20] [20]; // In the first dimension, only 5 is enough. At that time, I didn't think much about the status of 1024. It doesn't matter whether 1024 is used anyway, small Data ~~ 10 int visit [1050] [20] [20]; 11 int n, m, Ma; 12 13 int dir [4] [2] = {, 0, 0,-1, 0}; 14 15 struct node {16 int X, Y; 17} st; 18 19 void CPY (int d, int S) {20 for (INT I = 0; I <n; I ++) 21 for (Int J = 0; j <m; j ++) 22 map [d] [I] [J] = map [s] [I] [J]; 23} 24 25 void rvisit (int o) {26 for (INT I = 0; I <n; I ++) 27 for (Int J = 0; j <m; j ++) 28 visit [O] [I] [J] = 0; 29} 30 31 int BFS (int o, int ans, node st) {// cout <St. x <"" <s T. Y <Endl; 32 node A, B, C; 33 queue <node> q; 34 while (! Q. empty () 35 Q. pop (); 36 Q. push (ST); 37 rvisit (o); 38 visit [O] [st. x] [st. y] = 1; 39 while (! Q. empty () {40 A = Q. front (); 41 Q. pop (); 42 int XX, YY; 43 for (INT I = 0; I <4; I ++) {44 xx =. X + dir [I] [0]; 45 YY =. Y + dir [I] [1]; 46 If (visit [O] [XX] [YY]) 47 continue; 48 if (XX <0 | XX> = n | YY <0 | YY> = m) 49 continue; 50 if (Map [O] [XX] [YY] = 'X' | map [O] [XX] [YY] = 'O') 51 continue; 52 If (Map [O] [XX] [YY] = 'C') {53 ans ++; 54 map [O] [XX] [YY] = '. '; 55} 56 B. X = xx; B. y = YY; 57 Q. push (B); 58 visit [O] [XX] [YY] = 1; 59} 60} 61 MA = max (MA, ANS); 62 Q. push (ST); 63 rvisit (o); 64 visit [O] [st. x] [st. y] = 1; 65 while (! Q. empty () {66 A = Q. front (); // If (O = 0) cout <o <":" <. x <"" <. Y <Endl; 67 Q. pop (); 68 int XX, YY; 69 for (INT I = 0; I <4; I ++) {70 xx =. X + dir [I] [0]; 71 YY =. Y + dir [I] [1]; 72 If (visit [O] [XX] [YY]) 73 continue; 74 if (XX <0 | XX> = n | YY <0 | YY> = m) 75 continue; 76 if (Map [O] [XX] [YY] = 'X') 77 continue; 78 If (Map [O] [XX] [YY] = 'O') {79 int XXX, YYY; 80 xxx = xx + dir [I] [0]; 81 YYY = YY + dir [I] [1]; 82 If (xxx <0 | XXX> = n | YYY <0 | YYY> = m) 83 continue; 84 If (Map [O] [xxx] [YYY] = '. ') {85 CPY (O + 1, O); 86 map [O + 1] [XX] [YY] = '. '; 87 map [O + 1] [xxx] [YYY] = 'X'; // cout <o <":" <. x <"" <. Y <"|" <XX <"" <YY <"|" <XXX <"" <YYY <Endl; 88 C. X = xx; C. y = YY; 89 BFS (O + 1, ANS, c); 90} 91 92 continue; 93} 94 B. X = xx; B. y = YY; // If (O = 0) cout <. x <"" <. Y <"|" <XX <"" <YY <Endl; 95 Q. push (B); 96 visit [O] [XX] [YY] = 1; 97} 98} 99} 100 101 int main () {102 int T; 103 scanf ("% d", & T); 104 while (t --) {105 scanf ("% d", & N, & M ); 106 for (INT I = 0; I <n; I ++) {107 scanf ("% s", map [0] [I]); 108 For (Int J = 0; j <m; j ++) {109 If (Map [0] [I] [J] = 's') {110 St. X = I; ST. y = J; 111 // map [0] [I] [J] = '. '; 112} 113} 114} // cout <St. x <"" <St. Y <Endl; cout <map [0] [st. x] [st. y] <Endl; 115 Ma = 0; 116 BFS (117, St); 118 printf ("% d \ n", Ma); 119} 120 return 0}

 

CSU 1119 collecting coins

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.