一、骑士精神(Knight)
在一个5×5的棋盘上有12个白色的骑士和12个黑色的骑士, 且有一个空位。在任何时候一个骑士都能按照骑士的走法(它可以走到和它横坐标相差为1,纵坐标相差为2或者横坐标相差为2,纵坐标相差为1的格子)移动到空位上。
给定一个初始的棋盘,怎样才能经过移动变成如下目标棋盘:
为了体现出骑士精神,他们必须以最少的步数完成任务。
输入文件:
第一行有一个正整数T(T<=10),表示一共有N组数据。接下来有T个5×5的矩阵,0表示白色骑士,1表示黑色骑士,*表示空位。两组数据之间没有空行。
输出文件:
对于每组数据都输出一行。如果能在15步以内(包括15步)到达目标状态,则输出步数,否则输出-1。
Sample Input
2
10110
01*11
10111
01001
00000
01011
110*1
01110
01010
00100
Sample Output
7
-1
源代码如下:
/******************************************************************************************************
** Copyright (C) 2011.07.01-2013.07.01
** Author: famousDT <13730828587@163.com>
** Edit date: 2011-10-22
******************************************************************************************************/
#include <stdio.h>
#include <stdlib.h>//abs,atof(string to float),atoi,atol,atoll
#include <math.h>//atan,acos,asin,atan2(a,b)(a/b atan),ceil,floor,cos,exp(x)(e^x),fabs,log(for E),log10
#include <vector>
#include <queue>
#include <map>
#include <time.h>
#include <set>
#include <list>
#include <stack>
#include <string>
#include <iostream>
#include <assert.h>
#include <string.h>//memcpy(to,from,count
#include <ctype.h>//character process:isalpha,isdigit,islower,tolower,isblank,iscntrl,isprll
#include <algorithm>
using namespace std;
//typedef long long ll;
#define MY_PI acos(-1)
#define MY_MAX(a, b) ((a) > (b) ? (a) : (b))
#define MY_MIN(a, b) ((a) < (b) ? (a) : (b))
#define MY_MALLOC(n, type) ((type *)malloc((n) * sizeof(type)))
#define MY_ABS(a) (((a) >= 0) ? (a) : (-(a)))
#define MY_INT_MAX 0x7fffffff
#define LOW_BIT(a) ((a) & (-(a)))//last none zero value
/*==========================================================*\
| IDA*
\*==========================================================*/
#define STEP 15 + 1
const char goal[][6] = {"11111","01111","00*11","00001","00000"};
char s[6][6];
int ans = 0;;
int bound;
int cx[] = {2, 2, 1, 1, -1, -1, -2, -2};
int cy[] = {1, -1, 2, -2, 2, -2, 1, -1};
int h()//与目标有几个骑士不同
{
int t, i, j;
for (t = i = 0; i < 5; ++i)
for (j = 0; j < 5; ++j)
if (s[i][j] != goal[i][j]) ++t;
return t;
}
int dfs(int x, int y, int dv, int pre_move)
{
int hv = h ();
if (hv + dv > bound) return hv + dv;
if (hv == 0) {
ans = 1;
return dv;
}
int next_bound = 0x7fffffff;
int i;
for (i = 0; i < 8; ++i) {
if (i + pre_move == 7) continue;
int nx = x + cx[i];
int ny = y + cy[i];
if (nx >= 0 && nx < 5 && ny >= 0 && ny < 5) {
swap(s[x][y], s[nx][ny]);
int new_bound = dfs(nx, ny, dv + 1, i);
if (ans) return new_bound;
next_bound = MY_MIN(new_bound, next_bound);
swap(s[x][y], s[nx][ny]);
}
}
return next_bound;
}
void IDA_star(int sx, int sy)
{
ans = 0;
bound = h();
while (!ans && bound <= STEP)
bound = dfs(sx, sy, 0, -STEP);
}
int main()
{
FILE *in,*out;
ans = 0;
in = fopen("Knight.in","rt");
out = fopen("Knight.out","wt");
int cases;
int i, j, sx, sy;
char c;
fscanf(in, "%d%c", &cases, &c);
while (cases--) {
for (i = 0; i < 5; ++i) fscanf(in, "%s", s[i]);
for (i = 0; i < 5; ++i) {
for (j = 0; j < 5; ++j) {
if (s[i][j] == '*') {
sx = i;
sy = j;
break;
}
}
if (j < 5) break;
}
IDA_star(sx, sy);
if (ans) fprintf(out, "%d\n", bound);
else fprintf(out, "-1\n");
}
system("pause");
fclose(in);
fclose(out);
return 0;
}