To understand the future will be found in fact is simulation ...
Because you can only assign a whole row or a whole column at a time, the target matrix must have one row at the beginning or a column with the same number, and then find the corresponding operation to mark that row or column as visited. Then the new matrix must be able to find a row or a column of numbers are the same, and then find the corresponding operation, mark that row or column, and so on, and then do not use the operation to find a random order. In fact, the initial matrix is not much use ...
1#include <iostream>2#include <cstring>3#include <cstdio>4 using namespacestd;5 6 Const intN =101;7 Const intM =501;8 intMp1[n][n];9 intMp2[n][n];Ten intGoal[n][n]; One BOOLVisit[n][n]; A BOOLUsed[m]; - intAns[m]; - intN, M, CNT; the - structNode - { - Charop[2]; + intx, y; - } Node[m]; + A intCheckrow (introw) at { - intj =1; - while(j <= N && Visit[row][j]) J + +; - if(J = = n +1)return-1; - intnum =Goal[row][j]; - for(intK = j +1; K <= N; k++ ) in { - if(Visit[row][k])Continue; to if(num! = Goal[row][k])return-1; + } - returnnum; the } * $ intCheckcol (intCol)Panax Notoginseng { - inti =1; the while(I <= n && visit[i][col]) i++; + if(i = = n +1)return-1; A intnum =Goal[i][col]; the for(intK = i +1; K <= N; k++ ) + { - if(Visit[k][col])Continue; $ if(num! = Goal[k][col])return-1; $ } - returnnum; - } the - voidSolve ()Wuyi { thememset (Visit,0,sizeof(visit)); -memset (Used,0,sizeof(used)); WuCNT =1; - while(1 ) About { $ BOOLFlag =false; - for(introw =1; Row <= N; row++ ) - { - intTMP =checkrow (row); A if(tmp! =-1&& mp1[row][tmp]! =-1 ) + { theans[cnt++] =mp1[row][tmp]; -USED[MP1[ROW][TMP]] =true; $ for(intK =1; K <= N; k++ ) the { theVISIT[ROW][K] =1; the } theFlag =true; - } in } the for(intCol =1; Col <= N; col++ ) the { About intTMP =Checkcol (col); the if(tmp! =-1&& mp2[col][tmp]! =-1 ) the { theans[cnt++] =mp2[col][tmp]; +USED[MP2[COL][TMP]] =true; - for(intK =1; K <= N; k++ ) the {BayiVisit[k][col] =1; the } theFlag =true; - } - } the if(!flag) Break; the } the for(inti =1; I <= m; i++ ) the { - if( !Used[i]) the { theans[cnt++] =i; the }94 } the for(inti = m; i >1; i-- ) the { theprintf"%d", Ans[i]);98 } Aboutprintf"%d\n", ans[1]); - }101 102 intMain ()103 {104 intT; thescanf"%d", &t);106 while(t-- )107 {108scanf"%d%d", &n, &m);109 for(inti =1; I <= N; i++ ) the {111 for(intj =1; J <= N; J + + ) the {113scanf"%d", &goal[i][j]); the } the } the for(inti =1; I <= N; i++ )117 {118 for(intj =1; J <= N; J + + )119 { -scanf"%d", &goal[i][j]);121 }122 }123memset (MP1,-1,sizeof(MP1));124memset (MP2,-1,sizeof(MP2)); the for(inti =1; I <= m; i++ )126 {127scanf"%s%d%d", &node[i].op, &node[i].x, &node[i].y); - if(node[i].op[0] =='H' )129 { theMP1[NODE[I].X][NODE[I].Y] =i;131 } the Else133 {134MP2[NODE[I].X][NODE[I].Y] =i;135 }136 }137 solve ();138 }139 return 0; $}
HDU 5386 Simulation