HDU 2234 Untitled I

Source: Internet
Author: User

Untitled itime limit:10000/10000ms (Java/other) Memory limit:32768/32768k (Java/other) total submission (s): 2 Accepted Su Bmission (s): 1Problem description One day robot little A is playing a simple intellectual game, this game is like this, in a 4*4 matrix of 4 1, 4 2, 4 3 and 4 4 different things, respectively, Each step of small a can move the 4 number of the same row to the left or to the right one step or the same column of 4 numbers up or down one step (1,2,3,4 to the left after the 2,3,4,1), Little a now wants to know that the minimum number of moves can be the same as the 4 numbers on each line of the matrix or the 4 numbers on each column. But little a does not want to go too many steps, he just know the minimum number of steps is equal to 5 steps, the output is the correct number of steps, otherwise output-1.
Input first enters an integer t, which indicates that there is a T group of data. For each set of data, enter 4 rows, and each row of 4 columns represents the matrix.
Output a positive integer for each set of input outputs represents the minimum number of moving steps, which is greater than 5 for output-1.
Sample Input
21 2 3 41 2 3 41 2 3 42 3 4 14 1 1 11 2 2 22 3 3 33 4 4 4

Sample Output
11

Sourcehdoj Summer Exercise (2)-Hold by Captain Xu

Change four point states at a time

The estimated four changes are correct at least the number of moves required is (ans+3)/4;


#include <iostream> #include <cstdio> #include <queue> #include <cstring> #include <string    > #define INF 1<<30using namespace std;int get_h (int b[4][4]) {int ans=inf,tmp=0;        for (int i=0;i<4;i++) {bool flag[5];        int cnt=4;        memset (flag,false,sizeof (flag));                for (int j=0;j<4;j++) if (!flag[b[i][j]]) {cnt--;            Flag[b[i][j]]=true;    } tmp+=3-cnt;    } ans=min (Tmp,ans);    Tmp=0;        for (int j=0;j<4;j++) {bool flag[5];        int cnt=4;        memset (flag,false,sizeof (flag));                for (int i=0;i<4;i++) if (!flag[b[i][j]]) {cnt--;            Flag[b[i][j]]=true;    } tmp+=3-cnt;    } ans=min (Tmp,ans); Return (ANS+3)/4;}    BOOL Dfs (int len,int a[4][4],int kind,int kind1) {if (Get_h (a) >len) return false;    if (len==0) return true;    int aa[4][4]; for (int i=0; i<4; i++) {if (kind==i&&kind1==2) {; } else {for (int j=0; j<4; j + +) for (int k=0; k<4; k++) AA [j]            [K]=a[j][k];                for (int j=0; j<4; J + +) if (j) aa[i][j]=a[i][j-1];            else aa[i][j]=a[i][3];        if (Dfs (len-1,aa,i,1)) return true;        } if (kind==i&&kind1==1); else {for (int j=0; j<4; j + +) for (int k=0; k<4; k++) aa[j][k]=a[            J][K];                for (int j=0; j<4; J + +) if (j!=3) aa[i][j]=a[i][j+1];            else aa[i][j]=a[i][0];        if (Dfs (len-1,aa,i,2)) return true;        } if (kind==i&&kind1==3) {; } else {for (int j=0; j<4; j + +) for (int k=0; k<4; k++) AA [j]            [K]=a[j][k];       for (int j=0; j<4; J + +) if (j!=3) aa[j][i]=a[j+1][i];         else Aa[j][i]=a[0][i];        if (Dfs (len-1,aa,i,4)) return true;        } if (kind==i&&kind1==4) {; }else {for (int j=0; j<4; j + +) for (int k=0; k<4; k++) aa[j][k]=a [j]            [K];                for (int j=0; j<4; J + +) if (j) Aa[j][i]=a[j-1][i];            else Aa[j][i]=a[3][i];        if (Dfs (len-1,aa,i,3)) return true; }} return false;}    int main () {int t;    scanf ("%d", &t);        while (t--) {int a[4][4];        for (int. i=0; i<4; i++) {for (int j=0; j<4; j + +) scanf ("%d", &a[i][j]);        } int len; For (Len=get_h (a); len<=5; len++) {if (Dfs (len,a,-1,-1)) {printf ("%d\n", Len                );            Break    }} if (len>5) printf (" -1\n"); }}



Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

HDU 2234 Untitled I

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.