# bzoj1085骑士精神 A*

A*搜索 实际上只有空格再走

 1 #include<iostream>
2 #include<cstdio>
3 #include<cstdlib>
4 #include<cstring>
5 #include<cmath>
6 #include<ctime>
7 #include<algorithm>
8 #define rep(i,l,r) for(int i=l;i<r;i++)
9 #define clr(a,x) memset(a,x,sizeof(a))
10 #define legal(x,y) (x<n&&0<=x&&y<n&&0<=y)
11 using namespace std;
12 const int n=5;
13 const int g[n][n]={{1,1,1,1,1}
14                   ,{0,1,1,1,1}
15                   ,{0,0,2,1,1}
16                   ,{0,0,0,0,1}
17                   ,{0,0,0,0,0}};
18 const int dir[8][2]={{1,2},{2,1},{1,-2},{-2,1},{-1,2},{2,-1},{-1,-2},{-2,-1}};
19 int now[n][n],ans;
20 bool flag;
21 bool same()
22 {
23     rep(i,0,n)
24         rep(j,0,n)
25             if(now[i][j]!=g[i][j]) return 0;
26     return 1;
27 }
28 bool estimate(int d)
29 {
30     rep(j,0,n)
31         rep(k,0,n)
32             if(now[j][k]!=g[j][k])
33             if(++d>ans) return 0;
34     return 1;
35 }
36 int cnt=0;
37 void dfs(int x,int y,int d)
38 {
39     if(d==ans&&same()){
40         flag=1;
41         return;
42     }
43     if(flag) return;
44     rep(i,0,8){
45         int x1=x+dir[i][0],y1=y+dir[i][1];
46         if(legal(x1,y1)){
47             swap(now[x][y],now[x1][y1]);
48             if(estimate(d)) dfs(x1,y1,d+1);
49             swap(now[x][y],now[x1][y1]);
50         }
51     }
52 }
53 int main()
54 {
55     int t;
56     cin>>t;
57     while(t--){
58         clr(now,0);
59         int x,y;
60         flag=0;
61         rep(i,0,n)
62             rep(j,0,n){
63                 char c=getchar();
64                 while(c!='*'&&!isdigit(c)) c=getchar();
65                 if(c=='*') now[x=i][y=j]=2;
66                 else now[i][j]=c-'0';
67         }
68         for(ans=1;ans<=15;ans++){
69             dfs(x,y,0);
70             if(flag) break;
71         }
72         printf("%d\n",flag?ans:-1);
73     }
74     return 0;
75 }
View Code

## 1085: [SCOI2005]骑士精神

Time Limit: 10 Sec  Memory Limit: 162 MB
Submit: 1110  Solved: 602
[Submit][Status][Discuss]

2
10110
01*11
10111
01001
00000
01011
110*1
01110
01010
00100

7
-1

## Source

[Submit][Status][Discuss]
posted @ 2015-07-10 09:45  ChenThree  阅读(113)  评论(0编辑  收藏