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");
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

kgduu

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值