UVa10422 - Knights in FEN

#include <stdio.h>
#include <string.h>

#define N 5
#define MAX 6000000
#define HASHSIZE 83849729

typedef struct node {
    int x, y;
    int step;
    int a[N][N];
} node;

int target[N][N] = {{1,1,1,1,1}, {0,1,1,1,1}, {0,0,2,1,1},{0,0,0,0,1},{0,0,0,0,0}};
int x, y;
node state[MAX];
int dx[] = {1, 1, 2, 2, -1, -1, -2, -2};
int dy[] = {2, -2, 1, -1, 2, -2, 1, -1};
int head[HASHSIZE], next[MAX];

int bfs();
int hash(node s);
int try_to_insert(int s);
void print(node s);

int main()
{
    int iCase;
    int i, j;
    char ch;
    int ans;

#ifndef ONLINE_JUDGE
    freopen("d:\\UVa\\uva_in.txt", "r", stdin);
#endif

    scanf("%d", &iCase);
    getchar();
    while (iCase--) {
        for (i = 0; i < N; i++) {
            for (j = 0; j < N; j++) {
                scanf("%c", &ch);
                if (' ' == ch) {
                    x = i, y = j;
                    state[0].a[i][j] = 2;
                } else
                    state[0].a[i][j] = ch - '0';
            }
            getchar();
        }
        if (memcmp(state[0].a, target, sizeof(target)) == 0) {
            printf("Solvable in 0 move(s).\n");
            continue;
        }



        ans = bfs();
        if (!ans)
            printf("Unsolvable in less than 11 move(s).\n");
        else
            printf("Solvable in %d move(s).\n", ans);
    }
    return 0;
}

int bfs()
{
    int front = 0, rear = 1;
    node tmp, newstate;
    int i;
    int newx, newy;

    state[0].x = x;
    state[0].y = y;
    state[0].step = 0;
    memset(head, -1, sizeof(head));
    memset(next, -1, sizeof(next));

    while (front < rear) {
        tmp = state[front];
        if (tmp.step < 10) {
            for (i = 0; i < 8; i++) {

                newx = tmp.x + dx[i];
                newy = tmp.y + dy[i];

                if (newx >= 0 && newx < 5 && newy >= 0 && newy < 5) {
                    newstate = tmp;
                    newstate.a[newx][newy] = tmp.a[tmp.x][tmp.y];
                    newstate.a[tmp.x][tmp.y] = tmp.a[newx][newy];
                    newstate.x = newx;
                    newstate.y = newy;



                    state[rear] = newstate;
                    if (try_to_insert(rear)) {
                        state[rear].step++;
                        if (memcmp(state[rear].a, target, sizeof(target)) == 0)
                            return state[rear].step;
                        rear++;
                    }
                }
            }
        } else
            return 0;
        front++;
    }
}

int hash(node s)
{
    int i, j, k = 0;
    int res = 0;

    for (i = 0; i < N; i++) {
        for (j = 0; j < N; j++) {
            if (s.a[i][j]) res += (1 << k) * s.a[i][j];
            k++;
        }
    }
    res += 1 << k;
    return res;
}

int try_to_insert(int s)
{
    int h = hash(state[s]);
    int u = head[h];

    while (u != -1) {
        if (memcmp(state[u].a, state[s].a, sizeof(state[s].a)) == 0)
            return 0;
        u = next[u];
    }

    next[s] = head[h];
    head[h] = s;
    return 1;
}


void print(node s)
{
    int i, j;

    for (i = 0; i < N; i++) {
        for (j = 0; j < N; j++) {
            printf("%d ", s.a[i][j]);
        }
        printf("\n");
    }
    printf("\n");
}

你可能感兴趣的:(UVa10422 - Knights in FEN)