# bzoj1085 [SCOI2005]骑士精神

## 1085: [SCOI2005]骑士精神

Time Limit: 10 Sec  Memory Limit: 162 MB
Submit: 2490  Solved: 1422
[Submit][Status][Discuss]

## Description

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

## Input

第一行有一个正整数T(T<=10)，表示一共有T组数据。接下来有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 <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <queue>
#include <stack>
#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <string>
#include <cmath>

using namespace std;

int t,a[10][10],x,y,endd[10][10];
bool flag;
char c;

int dx[] = { -1,1,-2,2,-2,2,-1,1 };
int dy[] = { -2,-2,-1,-1,1,1,2,2 };

void init()
{
endd[1][1] = endd[1][2] = endd[1][3] = endd[1][4] = endd[1][5] = endd[2][2] = endd[2][3] = endd[2][4] = endd[2][5] = endd[3][4] = endd[3][5] = endd[4][5] = 1;
endd[3][3] = 2;
endd[2][1] = endd[3][1] = endd[3][2] = endd[4][1] = endd[4][2] = endd[4][3] = endd[4][4] = endd[5][1] = endd[5][2] = endd[5][3] = endd[5][4] = endd[5][5] = 0;
}

bool check()
{
for (int i = 1; i <= 5; i++)
for (int j = 1; j <= 5; j++)
if (endd[i][j] != a[i][j])
return false;
return true;
}

int gujia()
{
int cnt = 0;
for (int i = 1;i <= 5; i++)
for (int j = 1; j <= 5; j++)
if (a[i][j] != endd[i][j])
cnt++;
return cnt;
}

void dfs(int step,int maxn,int xx,int yy)
{
//printf("%d %d %d %d\n", step, maxn, x, y);
if (step == maxn)
{
if (check())
flag = 1;
return;
}
if (flag)
return;
for (int i = 0; i < 8; i++)
{
int nx = xx + dx[i], ny = yy + dy[i];
if (nx > 0 && nx <= 5 && ny > 0 && ny <= 5)
{
swap(a[nx][ny], a[xx][yy]);
if (step + gujia() <= maxn)
dfs(step + 1, maxn,nx,ny);
swap(a[nx][ny], a[xx][yy]);
}
}
return;
}

int main()
{
init();
scanf("%d", &t);
while (t--)
{
flag = 0;
x = y = 0;
for (int i = 1; i <= 5; i++)
for (int j = 1; j <= 5; j++)
{
c = getchar();
while (c != '1' && c != '0' && c != '*')
c = getchar();
if (c == '1')
a[i][j] = 1;
if (c == '0')
a[i][j] = 0;
if (c == '*')
{
a[i][j] = 2;
x = i;
y = j;
}
}
if (check())
{
printf("0\n");
continue;
}
for (int i = 1; i <= 15; i++)
{
dfs(0, i, x, y);
if (flag)
{
printf("%d\n",i);
break;
}
}
if (!flag)
printf("-1\n");
}

return 0;
}

posted @ 2017-08-02 22:49  zbtrs  阅读(123)  评论(0编辑  收藏