Solitaire
Time Limit: 2000/1000 MS (Java/others) memory limit: 65536/32768 K (Java/Others)
Total submission (s): 2526 accepted submission (s): 822
Problem descriptionsolitaire is a game played on a chessboard 8x8. the rows and columns of the chessboard are numbered from 1 to 8, from the top to the bottom and from left to right respectively.
There are four identical pieces on the board. In one move it is allowed:
> Move a piece to an empty neighboring field (Up, down, left or right ),
> Jump over one neighboring piece to an empty field (Up, down, left or right ).
There are 4 moves allowed for each piece in the configuration shown above. as an example let's consider a piece placed in the row 4, column 4. it can be moved one row up, two rows down, one column left or two columns right.
Write a program that:
> Reads two chessboard invocations from the standard input,
> Verifies whether the second one is reachable from the first one in at most 8 moves,
> Writes the result to the standard output.
Inputeach of two input lines contains 8 integers A1, A2 ,..., a8 separated by single spaces and describes one configuration of pieces on the chessboard. integers a2j-1 and a2j (1 <= j <= 4) describe the position of one piece-the row number and the column number
Respectively. process to the end of file.
Outputthe output shoshould contain one word for each test case-Yes If a configuration described in the second input line is reachable from the configuration described in the first input line in at most 8 moves, or one word no otherwise.
Sample Input
4 4 4 5 5 4 6 52 4 3 3 3 6 4 6
Sample output
YES
Sourcesouthwestern Europe 2002
Recommendignatius. l fuck. It took more than an hour to access wa in a small place ~~
# Include <iostream> # include <cstring> # include <cstdio> # include <algorithm> # define INF 0x3f3f3f3f # define n 1000000 # define bug printf ("here! \ N ") using namespace STD; struct point {int X, Y ;}; struct node {point num [4]; int D ;}; node Q1 [N], q2 [N]; char vis [8] [8] [8] [8] [8] [8] [8] [8]; int map [10] [10]; int XX [4] = {, 0,-1}; int YY [4] = }; bool CMP (point LHS, point RHs) {If (LHS. x! = RHS. x) return LHS. x <RHS. x; else return LHS. Y <RHS. y;} void make_vis (node start, char word) {vis [start. num [0]. x] [start. num [0]. y] [start. num [1]. x] [start. num [1]. y] [start. num [2]. x] [start. num [2]. y] [start. num [3]. x] [start. num [3]. y] = word;} void make_map (node cur) {map [cur. num [0]. x] [cur. num [0]. y] = 1; Map [cur. num [1]. x] [cur. num [1]. y] = 1; Map [cur. num [2]. x] [cur. num [2]. y] = 1; Map [cur. num [3]. x] [cur. num [3]. y] = 1;} Char Visit (node start) {return vis [start. num [0]. x] [start. num [0]. y] [start. num [1]. x] [start. num [1]. y] [start. num [2]. x] [start. num [2]. y] [start. num [3]. x] [start. num [3]. y];} int judge (int x, int y) {If (x <0 | x> = 8 | Y <0 | Y> = 8) return 0; if (Map [x] [Y] = 1) return 1; return 2;} int BFS (node start, node end) {memset (VIS, 0, sizeof (VIS); int front1 = 0, tail1 = 1, front2 = 0, tail2 = 1; sort (start. num, start. num + 4, CMP); sort (end. num, E Nd. num + 4, CMP); make_vis (START, '1'); make_vis (end, '2'); Q1 [0] = start; Q2 [0] = end; q1 [0]. D = 0; Q2 [0]. D = 0; while (front1 <tail1 | front2 <tail2) {int TMP = tail1; For (; front1 <TMP; front1 ++) {node cur = Q1 [front1]; memset (MAP, 0, sizeof (MAP); make_map (cur); int I; for (I = 0; I <4; I ++) {Int J; For (j = 0; j <4; j ++) {node next = cur; // ah, this cannot be outside the J loop, because next may be changed below. D value ~~ Next. num [I]. X = cur. num [I]. X + XX [J]; next. num [I]. y = cur. num [I]. Y + YY [J]; If (Judge (next. num [I]. x, next. num [I]. y) = 0) continue; If (Judge (next. num [I]. x, next. num [I]. y) = 1) {next. num [I]. X = next. num [I]. X + XX [J]; next. num [I]. y = next. num [I]. Y + YY [J]; If (Judge (next. num [I]. x, next. num [I]. Y )! = 2) continue;} Sort (next. num, next. num + 4, CMP); If (cur. d + 1> 8) Return-1; if (visit (next) = '1') continue; else if (visit (next) = '2 ') {return next. d + cur. d + 1;} make_vis (next, '1'); next. D = cur. d + 1; Q1 [tail1 ++] = next ;}} TMP = tail2; For (; front2 <TMP; front2 ++) {node cur = Q2 [front2]; memset (MAP, 0, sizeof (MAP); make_map (cur); int I; for (I = 0; I <4; I ++) {Int J; for (j = 0; j <4; j ++) {node next = cur; next. num [I]. X = Cu R. num [I]. X + XX [J]; next. num [I]. y = cur. num [I]. Y + YY [J]; If (Judge (next. num [I]. x, next. num [I]. y) = 0) continue; If (Judge (next. num [I]. x, next. num [I]. y) = 1) {next. num [I]. X = next. num [I]. X + XX [J]; next. num [I]. y = next. num [I]. Y + YY [J]; If (Judge (next. num [I]. x, next. num [I]. Y )! = 2) continue;} Sort (next. num, next. num + 4, CMP); If (cur. d + 1> 8) Return-1; if (visit (next) = '2') continue; else if (visit (next) = '1 ') {return next. d + cur. d + 1;} make_vis (next, '2'); next. D = cur. d + 1; Q2 [tail2 ++] = next ;}}} return-1 ;}int main () {int X, Y; node T1, T2; while (scanf ("% d", & X, & Y )! = EOF) {t1.num [0]. X = X-1; t1.num [0]. y = Y-1; int I; for (I = 1; I <4; I ++) {scanf ("% d", & X, & Y ); t1.num [I]. X = X-1; t1.num [I]. y = Y-1;} for (I = 0; I <4; I ++) {scanf ("% d", & X, & Y ); t2.num [I]. X = X-1; t2.num [I]. y = Y-1;} int res = BFS (T1, T2); If (RES =-1 | res> 8) printf ("NO \ n "); else printf ("Yes \ n");} return 0 ;}