HDU 2234 no question I
Untitled ITime Limit: 10000/10000 ms (Java/Other) Memory Limit: 32768/32768 K (Java/Other) Total Submission (s): 2 Accepted Submission (s ): 1 Problem Description one day robot John is playing A simple intelligent game. This game is like this, in a 4*4 matrix, there are four types of things: 1, 4, 2, 3, and 4, respectively, each step is small. A can move the four numbers in the same row to the left or the right, or move the four numbers in the same column up or down one step (1, 2, 3, 4 to the left, and then 2, 3, 4, 1 ), mr. A wants to know that four numbers on each row of the matrix can be the same or the four numbers on each column can be the same after the last few steps. But Mr. A does not want to take too many steps. He only needs to know whether the minimum number of steps is less than or equal to 5 steps. If yes, the output is accurate. Otherwise, the output is-1.
Input first Input an integer T, indicating that there are T groups of data. Input four rows for each group of data, and four columns in each row represent this matrix.
Output outputs a positive integer for each input group to indicate the minimum number of moving steps. If the number is greater than 5, the Output is-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
Source HDOJ 2008 Summer Exercise (2)-Hold by Captain Xu
Changes the status of four points at a time.
If the four changes are estimated to be correct, the minimum number of moves is (ANS + 3)/4;
#include
#include
#include
#include
#include
#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,len); break; } } if(len>5) printf(-1); }}