Date: 2010-05-21
HDU 3004 the chess [BFS simulation]
Question: give an n * m chessboard and put your own "car", "horse", and "gun, "S" and other sub-"D ";
It is stipulated that only the three sons of the same party can act, and they cannot eat any child other than the other party's; the minimum part can be eaten by the other party;
Among them, "car", "horse", and "gun" are exactly the same as chess rules, including the lame rules of "horse;
Analysis: the algorithm is not complex. It is just a simple BFS simulation process.
But some details in the middle are very troublesome;
I wrote it in the afternoon and kept wa. Debug in the evening and find that a minus sign is mistakenly written into the plus sign. After the change, it will pass. Inniu.
Good questions, test the coding capability, and test the patience by the way.
If you write such a question, the code will be quite long. I have nearly 200 lines of code.
# Include <iostream> <br/> # include <algorithm> <br/> # include <cstring> <br/> # include <queue> <br/> using namespace STD; <br/> const int max = 12; </P> <p> char s [Max] [Max]; <br/> int LCX, lcy, LPX, lpy, lmx, lmy; <br/> int NCX, ncy, npx, Y, nmx, nmy; <br/> int R, C, Sx, Sy, p10 [10] = {1000000, 1010000,}; <br/> int V []; <br/> int DX [10] = {1, -, 0 }; <br/> int dy [10] = {, 1,-1}; <br/> int MX [30] = {-2, -2,-1, 1, 2, ,-1 }; <br/> int random [30] = {-, 1,-1,-2,-2 }; <br/> int BB [30] = {-1,-, 0}; <br/> int YY [30] =, 0, 0,-1,-1 }; <br/> bool Yes (int A, int B) <br/>{< br/> If (A> = 0 & A <R & B> = 0 & B <c) return 1; <br/> return 0; <br/>}< br/> bool OK (INT now) <br/> {<br/> S [LCX] [lcy] = '. '; <br/> S [LPX] [lpy] = '. '; <br/> S [lmx] [lmy] = '. '; </P> <p> NCX = now/100000; ncy = (now/10000) % 10; <br/> npx = (now/1000) % 10; YY = (now/100) % 1 0; <br/> nmx = (now/10) % 10; nmy = now % 10; <br/> S [NCX] [ncy] = 'C '; <br/> S [npx] [YY] = 'P'; <br/> S [nmx] [nmy] = 'M '; </P> <p> LCX = NCX; lcy = ncy; <br/> LPX = npx; lpy = YY; <br/> lmx = nmx; lmy = nmy; </P> <p> int x = NCX, y = ncy; <br/> int I, L, R; <br/> If (x = SX) <br/>{< br/> int f = 0; <br/> L = min (Y, Sy); r = max (Y, Sy ); <br/> for (I = L + 1; I <= R-1; I ++) if (s [x] [I]! = '.') {F = 1; break ;}< br/> If (! F) return 1; <br/>}< br/> else if (y = Sy) <br/>{< br/> int f = 0; <br/> L = min (x, SX); r = max (x, SX); <br/> for (I = L + 1; I <= R-1; I ++) if (s [I] [Y]! = '.') {F = 1; break ;}< br/> If (! F) return 1; <br/>}</P> <p> X = npx; y = y; <br/> If (x = SX) <br/>{< br/> int f = 0; <br/> L = min (Y, Sy); r = max (Y, Sy ); <br/> for (I = L + 1; I <= R-1; I ++) if (s [x] [I]! = '. ') F ++; <br/> If (F = 1) return 1; <br/>}< br/> else if (y = SY) <br/> {<br/> int f = 0; <br/> L = min (x, SX); r = max (x, SX ); <br/> for (I = L + 1; I <= R-1; I ++) if (s [I] [Y]! = '. ') F ++; <br/> If (F = 1) return 1; <br/>}</P> <p> X = nmx; y = nmy; <br/> for (I = 0; I <8; I ++) <br/> {<br/> If (Yes (x + mx [I], Y + parts [I]) & S [x + mx [I] [Y + ← [I] = 's' & S [x + BB [I] [Y + YY [i] = '. ') return 1; <br/>}</P> <p> return 0; <br/>}</P> <p> int main () <br/>{< br/> int I, j, cur; <br/> int Cn = 0; <br/> while (scanf ("% d ", & R, & C) = 2) <br/>{< br/> cur = 0; <br/> memset (v, 0, sizeof (V )); <br/> for (I = 0; I <r; I ++) <br/> {<br/> Scanf ("% s", s [I]); <br/> for (j = 0; j <C; j ++) <br/>{< br/> If (s [I] [J] = 's') {SX = I; Sy = J ;} <br/> else if (s [I] [J] = 'C') <br/>{< br/> LCX = I; <br/> lcy = J; <br/> cur = cur + I * P10 [5] + J * P10 [4]; <br/>}< br/> else if (s [I] [J] = 'P') <br/>{< br/> LPX = I; <br/> lpy = J; <br/> cur = cur + I * P10 [3] + J * P10 [2]; <br/>}< br/> else if (s [I] [J] = 'M') <br/>{< br/> lmx = I; <br/> lmy = J; <br/> cur = cur + I * P10 [1] + J; <br/>}< br/ >}</P> <p> printf ("Scenario # % d/N", ++ CN); </P> <p> V [cur] = 1; </P> <p> int ans = 0x5fffff; <br/> queue <int> my; <br/> my. push (cur); </P> <p> while (! My. empty () <br/>{< br/> int now = My. front (); my. pop (); <br/> cur = now; <br/> If (OK (now) {ans = V [now]; break ;} <br/> int GS; <br/> // che <br/> int x = now/100000, y = (now/10000) % 10; <br/> for (I = 0; I <4; I ++) <br/> {<br/> int Tx = x + dx [I], ty = Y + dy [I]; <br/> while (Yes (TX, Ty) & S [TX] [ty] = '. ') <br/> {<br/> GS = cur-x * P10 [5]-y * P10 [4] + Tx * P10 [5] + ty * P10 [4]; <br/> If (V [GS] = 0) <br/> {<br/> V [GS] = V [cur] + 1; <br/> my. pus H (GS); <br/>}< br/> Tx + = DX [I]; ty + = Dy [I]; <br/>}< br/> // Pao <br/> X = (now/1000) % 10; y = (now/100) % 10; <br/> for (I = 0; I <4; I ++) <br/> {<br/> int Tx = x + dx [I], ty = Y + dy [I]; <br/> while (Yes (TX, Ty) & S [TX] [ty] = '. ') <br/> {<br/> GS = cur-x * P10 [3]-y * P10 [2] + Tx * P10 [3] + ty * P10 [2]; <br/> If (V [GS] = 0) <br/> {<br/> V [GS] = V [cur] + 1; <br/> my. push (GS); <br/>}< br/> Tx + = DX [I]; ty + = Dy [I]; <br/>}</P> <p> // MA <br/> X = (now/10) % 10; y = now % 10; <br/> int Tx, Ty; <br/> for (I = 0; I <8; I ++) <br/> {<br/> If (Yes (x + mx [I], Y + distinct [I]) & S [x + mx [I] [Y + parts [I] = '. '& S [x + BB [I] [Y + YY [I] = '. ') <br/>{< br/> Tx = x + mx [I]; <br/> ty = Y + fill [I]; <br/> GS = cur-x * 10-y + Tx * 10 + ty; <br/> If (V [GS] = 0) <br/>{< br/> V [GS] = V [cur] + 1; <br/> my. push (GS); <br/>}</P> <p> If (ANS! = 0x5fffffff) printf ("% d/n", ANS); <br/> else puts ("Oh! That's impossible! /N "); <br/>}< br/> system (" pause "); <br/> return 0; <br/>}< br/>