uva10422

#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <queue>
#include <set>


using namespace std;
struct node
{
    char sta[5][5];
    int x, y;
    int path;
} now, temp;




int move[8][2] = {{2, 1}, {2, -1}, {-2, 1}, {-2, -1}, {1, 2}, {1, -2}, {-1, 2}, {-1, -2}};
struct cmp
{
    bool operator() (node a, node b) const
    {
        return memcmp(a.sta, b.sta, 25) < 0;
    }
};


set<node,cmp> vis;
queue<node>que;


char aim[5][5] =
{
    {'1', '1', '1', '1', '1'},
    {'0', '1', '1', '1', '1'},
    {'0', '0', ' ', '1', '1'},
    {'0', '0', '0', '0', '1'},
    {'0', '0', '0', '0', '0'}
};






void bfs( )
{
    que.push(now);
    vis.insert(now);


    if(memcmp(now.sta, aim, sizeof(aim)) == 0)
    {
        printf("Solvable in 0 move(s).\n");
        return ;
    }
    while(!que.empty())
    {
        now = que.front();
        que.pop();


        if(now.path > 10)
        {
            printf("Unsolvable in less than 11 move(s).\n");
            return ;
        }
        if(memcmp(now.sta, aim, sizeof(aim)) == 0)
        {
            printf("Solvable in %d move(s).\n",now.path);
            return ;
        }
        for( int i = 0; i < 8; i++)
        {
            temp = now;
            temp.x += move[i][0];
            temp.y += move[i][1];
            temp.path++;
            if(temp.x >=0 && temp.x < 5 && temp.y >= 0 && temp.y < 5)
            {
                char tt;
                tt = temp.sta[temp.x][temp.y];
                temp.sta[temp.x][temp.y] = temp.sta[now.x][now.y];
                temp.sta[now.x][now.y] = tt;
                if(!vis.count(temp))
                {
                    vis.insert(temp);
                    que.push(temp);
                }
            }
        }
    }
}
void init()
{
    vis.clear();
    while(!que.empty())
        que.pop();
}


int main()
{
    int t, sx, sy;
    scanf("%d",&t);


    while(t--)
    {
        init();
        for( int i = 0; i < 5; i++)
        {
            getchar();
            for( int j = 0; j < 5; j++)
            {
                scanf("%c",&now.sta[i][j]);
                if(now.sta[i][j] == ' ')
                {
                    now.x = i;
                    now.y = j;
                    now.path = 0;
                }
            }
        }
        bfs();
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值