UVA - 10422 Knights in FEN(bfs+hash)

Knights in FEN

Input: standard input

Output: standard output

Time Limit: 10 seconds

 

There are black and white knights on a 5 by 5 chessboard. There are twelve of each color, and there is one square that is empty. At any time, a knight can move into an empty square as long as it moves like a knight in normal chess (what else did you expect?).

Given an initial position of the board, the question is: what is the minimum number of moves in which we can reach the final position which is:

Input

First line of the input file contains an integer N (N<14) that indicates how many sets of inputs are there. The description of each set is given below:

Each set consists of five lines; each line represents one row of a chessboard. The positions occupied by white knights are marked by 0 and the positions occupied by black knights are marked by 1. The space corresponds to the empty square on board.

There is no blank line between the two sets of input.

The first set of the sample input below corresponds to this configuration:

Output

For each set your task is to find the minimum number of moves leading from the starting input configuration to the final one. If that number is bigger than 10, then output one line stating

Unsolvable in less than 11 move(s).     

 

otherwise output one line stating

Solvable in n move(s).

where n <= 10.

The output for each set is produced in a single line as shown in the sample output.

Sample Input

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

Sample Output

Unsolvable in less than 11 move(s).     
Solvable in 7 move(s).


题目大意:给出一个5*5的棋盘,求出至少要多少步才可以恢复到初始状态,如果大于10步就输出"Unsolvable in less than 11 move(s).",否则输出步数。

解析:这题的思路是bfs+hash,bfs用于枚举出所有的状态,hash用于判重。
八数码问题类似,基本照着刘汝佳的书上写,


#include <cstdio>
#include <cstring>
#include <set>
using namespace std;
const int N = 5;
const int dx[]={-2,-2,-1,-1, 1, 1, 2,2};    
const int dy[]={-1, 1,-2, 2,-2, 2,-1,1};  
const int MAXSTATE = 1000000;
const int goal[] ={
1,1,1,1,1,
2,1,1,1,1,
2,2,0,1,1,
2,2,2,2,1,
2,2,2,2,2};

typedef int State[N*N];
State st[MAXSTATE];
int dist[MAXSTATE];

set<int> vis;
inline void init_lookup_table() {
	vis.clear();
}
inline int try_to_insert(int s) {
	int v = 0;
	for(int i = 0; i < 25; i++) {
		v = v*2 + st[s][i];
	}
	if(vis.count(v)) {
		return 0;
	}
	vis.insert(v);
	return 1;
}

inline int bfs() {
	memset(dist,0,sizeof(dist));
	init_lookup_table();
	int front = 1,rear = 2;
	while(front < rear) {
		State& s = st[front];
		if(!memcmp(goal,s,sizeof(s))) {
			return front;
		}
		if(dist[front] > 10) {
			return 0;
		}
		int z;
		for(z = 0; z < 25; z++) {
			if(!s[z]) {
				break;
			}
		}
		int x = z/5 , y = z%5;
		for(int d = 0; d < 8; d++) {
			int newx = x + dx[d];
			int newy = y + dy[d];
			int newz = newx * 5 + newy;
			if(newx < 0 || newx >= 5 || newy < 0 || newy >= 5) {
				continue;
			}
			State& t = st[rear];
			memcpy(&t,&s,sizeof(s));
			t[newz] = s[z];
			t[z] = s[newz];
			dist[rear] = dist[front] + 1;
			if(try_to_insert(rear)) {
				rear++;
			}
		}
		front++;
	}
	return 0;
}
int main() {
	int t;
	char tmp[10];
	while(scanf("%d",&t) != EOF) {
		getchar();
		while(t--) {
			for(int i = 0; i < 5; i++) {
				gets(tmp);
				for(int j = 0; j < 5; j++) {
					if(tmp[j] == '0') {
						st[1][i*5+j] = 2;
					}else if(tmp[j] == '1') {
						st[1][i*5+j] = 1;
					}else if(tmp[j] == ' ') {
						st[1][i*5+j] = 0;
					}
				}
			}
			int ans = bfs();
			if(ans > 0) {
				printf("Solvable in %d move(s).\n",dist[ans]);
			}else {
				printf("Unsolvable in less than 11 move(s).\n");
			}
		}
	}
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值