标签:有关 个数 clu code int time name span freopen
·有关IDA*
是带有估值函数的迭代加深搜索,表现出出色的效率。
估值函数可以简单的定义为「已经到位的骑士的个数」。
然后就是普通的迭代加深了。
算法酷炫不一定赢,搜索好才是成功。
——Loli
Code:
#include <iostream> #include <math.h> #include <stdio.h> #include <algorithm> #include <string.h> #include <fstream> #define re register #define GC getchar() #define Clean(X,K) memset(X,K,sizeof(X)) #define Jud(X,Y) (X<0||X>=N||Y<0||Y>=N) using namespace std ; int Qread () { int X = 0 ; char C = GC ; while (C > ‘9‘ || C < ‘0‘) C = GC ; while (C >=‘0‘ && C <=‘9‘) { X = X * 10 + C - ‘0‘ ; C = GC ; } return X ; } const int Dx[8] = {1,1,-1,-1,2,2,-2,-2},Dy[8] = {2,-2,2,-2,1,-1,1,-1},N = 5 , Aim[N + 1][N + 1] = { {1,1,1,1,1,-1}, {0,1,1,1,1,-1}, {0,0,2,1,1,-1}, {0,0,0,0,1,-1}, {0,0,0,0,0,-1}, {-1,-1,-1,-1,-1,-1} }; int A[N + 1][N + 1] , Sx , Sy ; int G () { int Ans = 0 ; for (re int i = 0 ; i < N; ++ i) for (re int j = 0 ; j < N; ++ j) if (A[i][j] != Aim[i][j]) ++ Ans ; return Ans ; } int Ans , Fl = 0 ; void DFS (int X , int Y , int Stp) { int Star = G() ; if (Stp + Star > Ans + 1) return ; if (Fl) return ; if (Star == 0) { Fl = 1 ; return ; } for (re int i = 0 ; i < 8 ;++ i) { int Tx = Dx[i] + X , Ty = Dy[i] + Y ; if (Jud(Tx , Ty)) continue ; swap(A[X][Y] , A[Tx][Ty]) ; DFS (Tx , Ty , Stp + 1) ; swap (A[X][Y] , A[Tx][Ty]) ; } } int main () { // freopen ("P2324.in" , "r" , stdin) ; int Times = Qread () ; while (Times -- ) { for (re int i = 0 ; i < N; ++ i) { for (re int j = 0 ; j < N; ++ j) { char C = GC ; while (C != ‘*‘ && C != ‘0‘ && C != ‘1‘) C = GC ; if (C == ‘1‘) A[i][j] = 1 ; if (C == ‘0‘) A[i][j] = 0 ; if (C == ‘*‘) { A[i][j] = 2 ; Sx = i , Sy = j ; } } } Fl = 0 ; for (Ans = 0 ; Ans <= 15 ; ++ Ans) { DFS (Sx , Sy , 0) ; if (Fl) { cout << Ans <<endl; break ; } } if (!Fl)cout << -1 <<endl; } fclose (stdin) , fclose (stdout) ; return 0 ; }
Thanks!
标签:有关 个数 clu code int time name span freopen
原文地址:https://www.cnblogs.com/bj2002/p/10573908.html