[Hpuoj] 1152: Checkerboard transformations [Search]

Source: Internet
Author: User
Tags printf
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<&lt ; 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; }

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.