Title Description
Given a 4*4 01 chessboard, 1 represents a pawn, 0 represents a space, and a pawn 1 can be moved to a space next to or below four positions in each of the adjacent places. Then given your target chessboard, ask you at least how many steps you can take to turn the current chessboard into a target checkerboard state. input
The first line enters an integer t, which indicates that there is a T group of test data. Next, we give a 4*4 of only 0 and 1, with a blank line in the middle of the current chessboard and 4*4 's target chessboard. Output
Outputs an integer representing the minimum number of steps, if output-1 cannot be reached. Sample Input
1
0001
0011
1100
1111
1011
1101
0000
1101 Sample Output
8 Ideas
Hong Kong Giant offers the idea:
The chessboard is 4x4 4x4, with 216 2^{16} states, so you can indicate each state individually by number. The state can be memorized by a number indicating the state, from the starting position of the BFS run once, the Complexity O (216⋅16⋅4) O (2^{16}\cdot \cdot 4).
Note: 222=4194304 2^{22}=4194304
link:http://acm.hpu.edu.cn/problem.php?id=1152
#pragma GCC optimize ("O2") #include <stdio.h> #include <cstring> #include <cmath> #include <
algorithm> #include <vector> #include <queue> #include <map> using namespace std;
int s,t,dp[1<<17];
const int OX[]={0,0,1,-1};
const int oy[]={1,-1,0,0};
int get (int &x) {X=0;int z,cnt=0;
for (int i=0,z;i<16;++i) {scanf ("%1d", &z);
if (z) ++cnt,x+=1<<i;
} return CNT;
} bool Ex (int v,int x,int y) {return (v>> (x*4+y)) &1;}
/* void print (int n) {for (int i=0;i<16;++i) {if (i!=0&&i%4==0) Putchar (' \ n ');
printf ("%d", n>>i&1);
} putchar (' \ n ');
} */void work () {memset (dp,-1,sizeof (DP));
Queue<int> que;
Que.push (s);
dp[s]=0;
while (!que.empty ()) {int V=que.front (); Que.pop ();
if (v==t) break;
printf ("[%d]----\ n", Dp[v]);
Print (V);
for (int i=0;i<16;++i) {if (v>>i&1) { int x=i/4,y=i%4;
for (int j=0;j<4;++j) {int x=x+ox[j];
int Y=Y+OY[J]; if (0<=x&&x<4&&0<=y&&y<4&&!ex (v,x,y)) {int v=~ (1<< ; i) & V |
1<< (4*x+y);
if (dp[v]!=-1) continue;
dp[v]=dp[v]+1;
Que.push (v);
}}}}} while (!que.empty ()) Que.pop ();
printf ("%d\n", dp[t]);
} int main () {int t,z;
scanf ("%d", &t);
while (t--) {if (get (s)!=get (T)) puts ("1");
else work ();
} return 0; }