# BZOJ 1085 [SCOI2005]骑士精神 【A*启发式搜索】

## 1085: [SCOI2005]骑士精神

Time Limit: 10 Sec  Memory Limit: 162 MB
Submit: 2838  Solved: 1663
[Submit][Status][Discuss]

## Description

在一个5×5的棋盘上有12个白色的骑士和12个黑色的骑士， 且有一个空位。在任何时候一个骑士都能按照骑

## Input

第一行有一个正整数T(T<=10)，表示一共有N组数据。接下来有T个5×5的矩阵，0表示白色骑士，1表示黑色骑

## Output

对于每组数据都输出一行。如果能在15步以内（包括15步）到达目标状态，则输出步数，否则输出－1。

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

## Sample Output

7
-1

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define LL long long int
#define REP(i,n) for (int i = 1; i <= (n); i++)
#define Redge(u) for (int k = head[u]; k != -1; k = edge[k].next)
using namespace std;
const int maxn = 100005,maxm = 100005,INF = 1000000000;
int ans[5][5] =
{{1,1,1,1,1},
{0,1,1,1,1},
{0,0,2,1,1},
{0,0,0,0,1},
{0,0,0,0,0}};
int a[5][5],X[8] = {-1,-2,-2,-1,1,2,2,1},Y[8] = {-2,-1,1,2,2,1,-1,-2};
int k;
bool flag;
bool judge(){
for (int i = 0; i < 5; i++)
for (int j = 0; j < 5; j++)
if (a[i][j] != ans[i][j]) return false;
return true;
}
bool check(int s){
int cnt = 0;
for (int i = 0; i < 5; i++)
for (int j = 0; j < 5; j++)
if (a[i][j] != ans[i][j]) {cnt++;if (cnt + s > k) return false;}
return true;
}
void dfs(int s,int x,int y){
if (s == k) {if (judge()) flag = true; return;}
int nx,ny;
for (int i = 0; i < 8; i++){
nx = x + X[i]; ny = y + Y[i];
if (nx < 0 || ny < 0 || nx > 4 || ny > 4) continue;
swap(a[x][y],a[nx][ny]);
if (check(s)) dfs(s + 1,nx,ny);
swap(a[x][y],a[nx][ny]);
}
}
int main(){
int Tim;cin>>Tim;
while (Tim--){
memset(a,0,sizeof(a)); flag = false;
char c; int x,y;
for (int i = 0; i < 5; i++)
for (int j = 0; j < 5; j++){
c = getchar();
while (c != '1' && c != '0' && c != '*') c = getchar();
if (c == '*') a[i][j] = 2,x = i,y = j;
else a[i][j] = c - '0';
}
for (k = 1; k <= 15; k++){
dfs(0,x,y);
if (flag){printf("%d\n",k);break;}
}
if (!flag) printf("-1\n");
}
return 0;
}


posted @ 2017-12-07 13:49  Mychael  阅读(47)  评论(0编辑  收藏