24h購物| | PChome| 登入
2013-07-05 15:41:29| 人氣2,407| 回應0 | 上一篇 | 下一篇

[UVA][bfs] 10422 - Knights in FEN

推薦 0 收藏 0 轉貼0 訂閱站台

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

(Problem Setter: Piotr Rudnicki, University of Alberta, Canada)

 

 

A man is as great as his dreams.”

 

 



題目就是只給空格在中間, 藉由移動讓它變成目標盤面。

我們考慮從目標盤面 bfs 出來, 原本預設想要用雙向 bfs。
O(8^5 + 8^5) = O(32768)
但發現全建表也不過 5XXXX 個狀態, 因此乾脆全建了


#include <stdio.h>
#include <map>
#include <queue>
#include <iostream>
#include <algorithm>
using namespace std;
map<long long, int> R;
int dx[] = {1,1,-1,-1,2,2,-2,-2};
int dy[] = {2,-2,2,-2,1,-1,1,-1};
int build() {
    int board[5][5] = {
        {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}
    };
    int i, j, k, ox, oy, tx, ty;
    long long state = 0, init, tmp;
    for(i = 0; i < 5; i++)
        for(j = 0; j < 5; j++)
            state = state*3 + board[i][j];
    queue<long long> Q;
    R[state] = 0, init = state;
    Q.push(state);
    while(!Q.empty()) {
        state = Q.front(), Q.pop();
        int step = R[state];
        if(step == 10)   continue;
        for(i = 4, tmp = state; i >= 0; i--) {
            for(j = 4; j >= 0; j--) {
                board[i][j] = tmp%3, tmp /= 3;
                if(board[i][j] == 0)
                    ox = i, oy = j;
            }
        }
        /*for(i = 0; i < 5; i++, puts(""))
            for(j = 0; j < 5; j++)
                printf("%d", board[i][j]);
        puts("----");*/
        for(k = 0; k < 8; k++) {
            tx = ox+dx[k], ty = oy+dy[k];
            if(tx < 0 || ty < 0 || tx >= 5 || ty >= 5)
                continue;
            swap(board[tx][ty], board[ox][oy]);
            state = 0;
            for(i = 0; i < 5; i++)
                for(j = 0; j < 5; j++)
                    state = state*3 + board[i][j];
            int &val = R[state];
            if(val == 0 && state != init) {
                val = step+1;
                Q.push(state);
            }
            swap(board[tx][ty], board[ox][oy]);
        }
    }
}
int main() {
    build();
    int testcase;
    int i, j;
    char g[10][10];
    scanf("%d", &testcase);
    while(getchar() != '\n');
    while(testcase--) {
        for(i = 0; i < 5; i++)
            gets(g[i]);
        long long state = 0;
        for(i = 0; i < 5; i++) {
            for(j = 0; j < 5; j++) {
                if(g[i][j] == '1')
                    state = state*3 + 1;
                else if(g[i][j] == '0')
                    state = state*3 + 2;
                else
                    state = state*3;
            }
        }
        if(R.find(state) == R.end())
            puts("Unsolvable in less than 11 move(s).");
        else
            printf("Solvable in %d move(s).\n", R[state]);
    }
    return 0;
}
/*
2
01011
110 1
01110
01010
00100
10110
01 11
10111
01001
00000
*/

台長: Morris
人氣(2,407) | 回應(0)| 推薦 (0)| 收藏 (0)| 轉寄
全站分類: 不分類 | 個人分類: UVA |
此分類下一篇:[UVA] 10410 - Tree Reconstruction
此分類上一篇:[UVA][遞迴] 12627 - Erratic Expansion

是 (若未登入"個人新聞台帳號"則看不到回覆唷!)
* 請輸入識別碼:
請輸入圖片中算式的結果(可能為0) 
(有*為必填)
TOP
詳全文