UVA 10422 Knights in FEN (BFS + 判重)

Problem D

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).

题意:根据题目中给定的棋盘的起始状态。走到自己输入的目标状态。如果小于11步输出步数。。

思路: BFS。。判重可以用哈希判重。。不过我是用了map。。这题跟八数码有点像。不过马走的是日子格

代码:

#include <stdio.h>
#include <string.h>
#include <string>
#include <queue>
#include <map>
#include <algorithm>
using namespace std;

int t;
int d[8] = {-7 ,-11, -9, -3, 3, 9, 11, 7};
char c[6][6];
char end[30];
struct QUE {
    char status[30];
    int move;
    int space;
} q, p;

map<string, int> vis;
queue<QUE> Q;

void bfs() {
    vis.clear();
    while (!Q.empty()) {Q.pop();}
    strcpy(q.status, "111110111100 110000100000");
    q.move = 0;
    q.space = 12;
    vis[q.status] = 1;
    Q.push(q);
    while (!Q.empty()) {
	p = Q.front();
	if (strcmp(p.status, end) == 0 || p.move > 10)
	    return;
	Q.pop();
	for (int i = 0; i < 8; i ++) {
	    if (i == 0 && (p.space % 5 == 0 || (p.space - 1) % 5 == 0 || p.space < 5)) continue;
	    if (i == 1 && (p.space % 5 == 0 || p.space < 10)) continue; 
	    if (i == 2 && ((p.space + 1) % 5 == 0 || p.space < 10)) continue;
	    if (i == 3 && ((p.space + 1) % 5 == 0 || (p.space + 2) % 5 == 0 || p.space < 5)) continue;
	    if (i == 4 && (p.space % 5 == 0 || (p.space - 1) % 5 == 0 || p.space >= 20)) continue;
	    if (i == 5 && (p.space % 5 == 0 || p.space >= 15)) continue;
	    if (i == 6 && ((p.space + 1) % 5 == 0 || p.space >= 15)) continue;
	    if (i == 7 && ((p.space + 1) % 5 == 0 || (p.space + 2) % 5 == 0 || p.space >= 20)) continue;
	    q = p;
	    swap (q.status[q.space], q.status[q.space + d[i]]);
	    if (vis[q.status] != 1) {
		vis[q.status] = 1;
		q.move ++;
		q.space += d[i];
		Q.push(q);
	    }
	}
    }
}

int main() {
    scanf("%d", &t);
    getchar();
    while (t --) {
	for (int i = 0; i < 5; i ++) {
	    gets(c[i]);
	    for (int j = 0; j < 5; j ++)
		end[i * 5 + j] = c[i][j];
	}
	end[25] = '\0';
	bfs();
	if (p.move <= 10)
	    printf("Solvable in %d move(s).\n", p.move);
	else
	    printf("Unsolvable in less than 11 move(s).\n");
    }
    return 0;
}


你可能感兴趣的:(UVA 10422 Knights in FEN (BFS + 判重))