Topic links
Violence DFS of course not
So iterate and deepen it. ida*
1#include <iostream>2#include <cstdio>3#include <cstring>4 using namespacestd;5 intTt,k,flag;6 intans[5][5]=7 {8{1,1,1,1,1},9{0,1,1,1,1},Ten{0,0,2,1,1}, One{0,0,0,0,1}, A{0,0,0,0,0} - }; - intxx[8]={1,1,-1,-1,2,2,-2,-2},yy[8]={2,-2,2,-2,1,-1,1,-1}; the intPD1 (inta[5][5]) - { - for(intI=0;i<5; i++) - for(intj=0;j<5; j + +) + if(Ans[i][j]!=a[i][j])return 0; - return 1; + } A intPD2 (inta[5][5],ints) at { - intv=0; - for(intI=0;i<5; i++) - for(intj=0;j<5; j + +) - if(a[i][j]!=Ans[i][j]) - { inv++; - if(v+s>k)return 0; to } + return 1; - } the voidDfsintSinta[5][5],intXinty) * { $ if(s==k)Panax Notoginseng { - if(PD1 (a)) flag=1; the return; + } A if(flag==1)return; the for(intI=0;i<8; i++) + { - intdx=x+xx[i],dy=y+Yy[i]; $ if(dx<0|| Dx>4|| dy<0|| Dy>4) $ Continue; - swap (A[x][y],a[dx][dy]); - if(PD2 (a,s)) DFS (s+1, a,dx,dy); the swap (A[x][y],a[dx][dy]); - }Wuyi } the intMain () - { Wuscanf"%d",&TT); - while(tt--) About { $ inta[5][5];intx, y; -Memset (A,0,sizeof(a)); - for(intI=0;i<5; i++) - { A Chars[Ten];SCANF ("%s", s); + for(intj=0;j<5; j + +) the if(s[j]=='*') a[i][j]=2, x=i,y=J; - Elsea[i][j]=s[j]-'0'; $ } the for(k=1; k<= the; k++) the { theDfs0, a,x,y); the if(flag) - { inprintf"%d\n", k); the Break; the } About } the if(!flag) printf ("-1\n"); the Elseflag=0; the } + return 0; -}
bzoj1085 [SCOI2005] Knight Spirit