HDU 1401(Solitaire)

#include <iostream>
#include <map>
#include <queue>
using namespace std;

struct point //棋子
{
    int x;
    int y;
};

struct node //棋盘
{
    point p[4]; //四个棋子
    int step; //移动步数
} st, ed;

map<__int64, int> mp;
int dx[4] = { 0,0,1,-1 };
int dy[4] = { 1,-1,0,0 };

//判断结点p是否在棋盘范围内
int check(point p)
{
    if (p.x < 0 || p.x >= 8 || p.y < 0 || p.y >= 8)
        return 0;
    else
        return 1;
}

//计算结点now的哈希值
__int64 getHash(node now)
{
    __int64 ret = 0;
    for (int i = 0; i < 4; i++)
        ret |= ((__int64)1) << (now.p[i].x * 8 + now.p[i].y);
    return ret;
}

//广搜,记录从当前状态走四步,能到达的所有状态
//type:1表示从起始状态走四步,2表示从终止状态走四步
int BFS(int type, node now)
{
    if (type == 2) //终止状态与已有状态重合
    {
        if (mp.find(getHash(now)) != mp.end())
            return 1;
    }

    mp[getHash(now)] = type;
    now.step = 0;
    queue<node> q;
    q.push(now);
    while (!q.empty())
    {
        now = q.front();
        q.pop();
        if (now.step >= 4)
            continue;
        int i, j, k;
        node next;
        for (i = 0; i < 4; i++) //遍历四个方向
        {
            for (j = 0; j < 4; j++) //遍历四个棋子
            {
                next = now;
                next.step++;
                next.p[j].x += dx[i];
                next.p[j].y += dy[i];
                if (!check(next.p[j]))
                    continue;
                __int64 temp = getHash(next);
                for (k = 0; k < 4; k++) //判断是否和其他棋子重合
                {
                    if (k == j)
                        continue;
                    if (next.p[j].x == next.p[k].x && next.p[j].y == next.p[k].y)
                        break;
                }
                if (k == 4) //不重合
                {
                    if (mp.find(temp) == mp.end()) //该棋盘状态未出现过
                    {
                        mp[temp] = type;
                        q.push(next);
                    }
                    else if (mp[temp] != type)
                        return 1;
                }
                else //重合
                {
                    //向相同方向再走一步
                    next.p[j].x += dx[i];
                    next.p[j].y += dy[i];

                    temp = getHash(next);
                    if (!check(next.p[j]))
                        continue;
                    for (k = 0; k < 4; k++)
                    {
                        if (k == j)
                            continue;
                        if (next.p[j].x == next.p[k].x && next.p[j].y == next.p[k].y)
                            break;
                    }
                    if (k < 4)
                        continue;
                    if (mp.find(temp) == mp.end())
                    {
                        mp[temp] = type;
                        q.push(next);
                    }
                    else if (mp[temp] != type)
                        return 1;
                }
            }
        }
    }
    return 0;
}

int main()
{
    while (cin >> st.p[0].x >> st.p[0].y)
    {
        for (int i = 1; i < 4; i++)
            cin >> st.p[i].x >> st.p[i].y;
        for (int i = 0; i < 4; i++)
            cin >> ed.p[i].x >> ed.p[i].y;

        for (int i = 0; i < 4; i++)
        {
            st.p[i].x--;
            st.p[i].y--;
            ed.p[i].x--;
            ed.p[i].y--;
        } 
            
        mp.clear();
        BFS(1, st); //起始状态走四步,记录所有情况(哈希值)
        int flag = BFS(2, ed); //终止状态走四步,是否能和已有状态重合
        if (flag)
            cout << "YES" << endl;
        else
            cout << "NO" << endl;
    }
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值