Rush Hour Puzzle

探讨了Rush Hour拼图游戏的算法解决方案,该游戏目标是通过移动其他车辆来让红色车辆完全离开棋盘。使用广度优先搜索(BFS)和矩阵哈希技术判断谜题是否能在10步内解决。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Rush Hour Puzzle

题目描述 :
Rush Hour is a puzzle game invented by Nob Yoshigahara in the 1970s. It is now being manufactured by ThinkFun. The board is a 6 × 6 grid with grooves in the tiles to allow vehicles to slide. Cars and trucks are both one square wide, but cars are two squares long and trucks are three squares long. Vehicles can only be moved forward or backward along a straight line on the grid. The goal of the game is to get the only red car totally out through the exit of the board by moving the other vehicles out of its way. Figure 1 gives an example of Rush Hour puzzle.
在这里插入图片描述

Figure 1: An example of Rush Hour puzzle.
We give each vehicle of a puzzle a unique id, numbered from 1 to the number of vehicles, in which the red car’s id is 1. The board information of a puzzle is represented by a 6 × 6 matrix,named board matrix. Each entry of a board matrix is the id of the vehicle placed on that groove, and the entries are filled with 0 if there exists no vehicle on those grooves. The exit of the board is located at the right end side of the 3rd row. Figure 2 shows the board matrix corresponding to the puzzle in Figure 1.
Moving a piece (car or truck) by one unit (a groove) is called a step. A puzzle is easy if it can be solved (the red car totally out through the exit of the board) in no more than 10 steps.
Please write a program to judge whether a puzzle is easy or not.
在这里插入图片描述

Figure 2: The board matrix corresponding to the puzzle in Figure 1.

输入
The input contains 6 lines, each line indicates the content (6 integers separated by a blank) of each row of a board matrix.
输出
Output the minimum number of steps for solving the input puzzle if the puzzle is easy, otherwise output -1.
样例输入
0 2 0 6 6 0
0 2 0 0 7 0
0 3 1 1 7 0
0 3 4 4 8 0
0 5 5 5 8 0
0 0 0 0 0 0
样例输出
6
提示
• There are at most 10 vehicles on the board for each puzzle.
• Only the red car can be moved out of the board for each puzzle.
题目大意:车的长度由2和3,连续的非零数字代表一辆车,现在要把“1”代表的车移出去(出口固定在第三行末尾),一次移动一格,问把车移动出去的最小次数(注意移出去还要2次),若答案不存在或在10步以上,则输出-1。
表面意思,bfs加矩阵哈希(我用了map处理)。但我保证,这是我写过最长+最长时间的bfs搜索。。。

#pragma GCC optimize(3,"Ofast","inline")
#pragma G++ optimize(3)
#include<bits/stdc++.h>
#include <iostream>
#include <cstdio>
#include <fstream>
#include <algorithm>
#include <cmath>
#include <deque>
#include <vector>
#include <queue>
#include <string>
#include <cstring>
#include <map>
#include <stack>
#include <set>
#include <sstream>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll,ll> pll;
typedef pair<int,int> pii;
typedef queue<int> q_i;
typedef queue<string> q_s;
typedef queue<double> q_d;
typedef queue<ll> q_ll;
typedef queue<char> q_c;
typedef priority_queue<int> pq_i;
typedef priority_queue<string> pq_s;
typedef priority_queue<double> pq_d;
typedef priority_queue<ll> pq_ll;
typedef stack<int> s_i;
typedef stack<string> s_s;
typedef stack<double> s_d;
typedef stack<ll> s_ll;
typedef stack<char> s_c;
typedef map<ll,ll> m_ll_ll;
typedef map<int,ll> m_i_ll;
typedef map<int,int> m_i_i;
typedef map<string,ll> m_s_ll;
typedef map<char,int> m_c_i;
typedef map<char,ll> m_c_ll;
const ll INF=0x3f3f3f3f;
#define rep(i,l,r) for(ll i=l;i<=r;i++)
#define per(i,l,r) for(ll i=r;i>=l;i--)
#define eif else if
#define N 100005
#define mm(dp) memset(dp,0,sizeof(dp))
#define mm1(dp) memset(dp,-1,sizeof(dp))
#define mm2(dp) memset(dp,0x3f,sizeof(dp))
#define IT set<int>::iterator
#define fs(n) fixed<< setprecision(n)
//const double e=2.71828182845;
#define max(a,b) a>b?a:b
#define min(a,b) a<b?a:b
const double pi = acos(-1.0);
const int p=1e9+7;
inline ll read()
{
    ll s=0,w=1;
    char ch=getchar();
    while(ch<'0'||ch>'9')
    {
        if(ch=='-')
            w=-1;
        ch=getchar();
    }
    while(ch>='0'&&ch<='9')
        s=s*10+ch-'0',ch=getchar();
    return s*w;
}
inline void write(ll x)
{
    if(x<0)
        putchar('-'),x=-x;
    if(x>9)
        write(x/10);
    putchar(x%10+'0');
}
float SqrtByCarmack( float number )
{
    int i;
    float x2, y;
    const float threehalfs = 1.5F;
    x2 = number * 0.5F;
    y  = number;
    i  = * ( int * ) &y;
    i  = 0x5f375a86 - ( i >> 1 );
    y  = * ( float * ) &i;
    y  = y * ( threehalfs - ( x2 * y * y ) );
    y  = y * ( threehalfs - ( x2 * y * y ) );
    y  = y * ( threehalfs - ( x2 * y * y ) );
    return number*y;
}
ll qpow(ll a,ll b,ll mod)
{
    ll sum=1;
    while(b)
    {
        if(b%2==1)
        {
            sum=sum*a%mod;
        }
        b/=2;
        a=a*a%mod;
    }
    return sum;
}
int erfen(int *a,int start,int endd,int l)//小于等于l的最大值的角标
{
    int mid=(start+endd)/2;
    if((a[mid]<=l&&a[mid+1]>l)||(mid==endd&&a[mid]<=l))
        return mid;
    else if(a[mid]<=l)
        return erfen(a,mid+1,endd,l);
    else if(a[mid]>l)
    {
        if(start!=mid)
            return erfen(a,start,mid,l);
        else
            return start-1;
    }
}
ll prime[6] = {2, 3, 5, 233, 331};
ll qmul(ll x, ll y, ll mod)
{
    return (x * y - (long long)(x / (long double)mod * y + 1e-3) *mod + mod) % mod;
}
bool Miller_Rabin(ll p)
{
    if(p < 2)
        return 0;
    if(p != 2 && p % 2 == 0)
        return 0;
    ll s = p - 1;
    while(! (s & 1))
        s >>= 1;
    for(int i = 0; i < 5; ++i)
    {
        if(p == prime[i])
            return 1;
        ll t = s, m = qpow(prime[i], s, p);
        while(t != p - 1 && m != 1 && m != p - 1)
        {
            m = qmul(m, m, p);
            t <<= 1;
        }
        if(m != p - 1 && !(t & 1))
            return 0;
    }
    return 1;
}
ll gcd(ll x,ll y)
{
    if(y==0)
        return x;
    else
        return gcd(y,x%y);
}
typedef struct
{
    int a[7][7];
    int nm;
}STU;
typedef struct
{
    int a[7][7];
}STU1;
STU stu;
queue<STU>que;
map<string,int>mapp;
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int cnt=0;
    int u[15];
    mm(u);
    int kk=0;
    rep(i,1,6)
    {
        rep(j,1,6)
        {
            cin>>stu.a[i][j];
            //stu1.a[i][j]=stu.a[i][j];
            u[stu.a[i][j]]++;
            cnt=max(cnt,stu.a[i][j]);
        }
    }
    int flag=0;
    stu.nm=0;
    que.push(stu);
    while(!que.empty())
    {
        STU stu0=que.front();
 
        kk++;
 
//        cout<<endl;
//        rep(i,1,6)
//        {
//            rep(j,1,6)
//            {
//                cout<<stu0.a[i][j]<<" ";
//            }
//            cout<<endl;
//        }
//        cout<<endl;
        que.pop();
        if(stu0.a[3][6]==1&&stu0.a[3][5]==1)
        {
            if(u[1]==2)
                cout<<stu0.nm+2<<'\n';
            else
                cout<<stu0.nm+3<<'\n';
            flag=1;
            break;
        }
        rep(k,1,cnt)
        {
            if(u[k]==2)
            {
                int v=0;
                int flagx1=0;
                int flagy1=0;
                int flagx2=0;
                int flagy2=0;
                rep(i,1,6)
                {
                    rep(j,1,6)
                    {
                        if(stu0.a[i][j]==k&&v==0)
                        {
                            flagx1=i;
                            flagy1=j;
                            v++;
                        }
                        eif(stu0.a[i][j]==k)
                        {
                            flagx2=i;
                            flagy2=j;
                            v++;
                            break;
                        }
                    }
                    if(v==2)
                        break;
                }
                //if(k==2)
                    //cout<<flagy1<<" "<<flagy2<<endl;
                if(flagy1==flagy2)
                {
                    int xx1=min(flagx1,flagx2);
                    int xx2=max(flagx1,flagx2);
                    int yy=flagy1;
                    //cout<<xx1-1<<endl;
                    if(xx1-1>0)
                    {
                        if(stu0.a[xx1-1][yy]==0)
                        {
                            //cout<<xx1-1<<" "<<yy<<" "<<k<<endl;
                            STU stu1;
                            rep(p,1,6)
                            {
                                rep(q,1,6)
                                {
                                    if(q==yy)
                                        continue;
                                    else
                                        stu1.a[p][q]=stu0.a[p][q];
                                }
                            }
                            rep(p,1,6)
                            {
                                if(p==xx1-1||p==xx1)
                                {
                                    stu1.a[p][yy]=stu0.a[p+1][yy];
                                }
                                eif(p==xx2)
                                {
                                    stu1.a[p][yy]=0;
                                }
                                else
                                    stu1.a[p][yy]=stu0.a[p][yy];
                            }
                            stu1.nm=stu0.nm+1;
                            string str="";
                            rep(i,1,6)
                            {
                                rep(j,1,6)
                                {
                                    char ch=(stu1.a[i][j]-'0');
                                    str=str+ch;
                                }
                            }
                            if(mapp[str]==0)
                            {
                                mapp[str]=1;
                                if(u[1]==2)
                                {
                                    if(stu1.nm<=8)
                                        que.push(stu1);
                                }
                                else
                                {
                                    if(stu1.nm<=7)
                                        que.push(stu1);
                                }
 
                            }
 
                        }
                    }
 
                    if(xx2+1<7)
                    {
                        if(stu0.a[xx2+1][yy]==0)
                        {
                            //cout<<xx2+1<<" "<<yy<<" "<<k<<endl;
                            STU stu1;
                            rep(p,1,6)
                            {
                                rep(q,1,6)
                                {
                                    if(q==yy)
                                        continue;
                                    else
                                        stu1.a[p][q]=stu0.a[p][q];
                                }
                            }
                            rep(p,1,6)
                            {
                                if(p==xx2||p==xx2+1)
                                {
                                    stu1.a[p][yy]=stu0.a[p-1][yy];
                                }
                                eif(p==xx1)
                                {
                                    stu1.a[p][yy]=0;
                                }
                                else
                                    stu1.a[p][yy]=stu0.a[p][yy];
                            }
                            stu1.nm=stu0.nm+1;
                            string str="";
                            rep(i,1,6)
                            {
                                rep(j,1,6)
                                {
                                    char ch=(stu1.a[i][j]-'0');
                                    str=str+ch;
                                }
                            }
                            if(mapp[str]==0)
                            {
                                mapp[str]=1;
                                if(u[1]==2)
                                {
                                    if(stu1.nm<=8)
                                        que.push(stu1);
                                }
                                else
                                {
                                    if(stu1.nm<=7)
                                        que.push(stu1);
                                }
 
                            }
 
 
                        }
                    }
                }
                else
                {
                    int yy1=min(flagy1,flagy2);
                    int yy2=max(flagy1,flagy2);
                    int xx=flagx1;
                    if(yy1-1>0)
                    {
                        if(stu0.a[xx][yy1-1]==0)
                        {
                            STU stu1;
                            rep(p,1,6)
                            {
                                rep(q,1,6)
                                {
                                    if(p==xx)
                                        continue;
                                    else
                                        stu1.a[p][q]=stu0.a[p][q];
                                }
                            }
                            rep(p,1,6)
                            {
                                if(p==yy1-1||p==yy1)
                                {
                                    stu1.a[xx][p]=stu0.a[xx][p+1];
                                }
                                eif(p==yy2)
                                {
                                    stu1.a[xx][p]=0;
                                }
                                else
                                    stu1.a[xx][p]=stu0.a[xx][p];
                            }
                            stu1.nm=stu0.nm+1;
                            string str="";
                            rep(i,1,6)
                            {
                                rep(j,1,6)
                                {
                                    char ch=(stu1.a[i][j]-'0');
                                    str=str+ch;
                                }
                            }
                            if(mapp[str]==0)
                            {
                                mapp[str]=1;
                                if(u[1]==2)
                                {
                                    if(stu1.nm<=8)
                                        que.push(stu1);
                                }
                                else
                                {
                                    if(stu1.nm<=7)
                                        que.push(stu1);
                                }
 
                            }
                        }
                    }
                    if(yy2+1<7)
                    {
                        if(stu0.a[xx][yy2+1]==0)
                        {
                            STU stu1;
                            rep(p,1,6)
                            {
                                rep(q,1,6)
                                {
                                    if(p==xx)
                                        continue;
                                    else
                                        stu1.a[p][q]=stu0.a[p][q];
                                }
                            }
                            rep(p,1,6)
                            {
                                if(p==yy2||p==yy2+1)
                                {
                                    stu1.a[xx][p]=k;
                                }
                                eif(p==yy1)
                                {
                                    stu1.a[xx][p]=0;
                                }
                                else
                                    stu1.a[xx][p]=stu0.a[xx][p];
                            }
                            stu1.nm=stu0.nm+1;
                            string str="";
                            rep(i,1,6)
                            {
                                rep(j,1,6)
                                {
                                    char ch=(stu1.a[i][j]-'0');
                                    str=str+ch;
                                }
                            }
                            if(mapp[str]==0)
                            {
                                mapp[str]=1;
                                if(u[1]==2)
                                {
                                    if(stu1.nm<=8)
                                        que.push(stu1);
                                }
                                else
                                {
                                    if(stu1.nm<=7)
                                        que.push(stu1);
                                }
 
                            }
                        }
                    }
                }
            }
 
 
            else
            {
                int v=0;
                int flagx1=0;
                int flagy1=0;
                int flagx2=0;
                int flagy2=0;
                int flagx3=0;
                int flagy3=0;
                rep(i,1,6)
                {
                    rep(j,1,6)
                    {
                        if(stu0.a[i][j]==k&&v==0)
                        {
                            flagx1=i;
                            flagy1=j;
                            v++;
                        }
                        eif(stu0.a[i][j]==k&&v==1)
                        {
                            flagx2=i;
                            flagy2=j;
                            v++;
                        }
                        eif(stu0.a[i][j]==k&&v==2)
                        {
                            flagx3=i;
                            flagy3=j;
                            v++;
                        }
                        if(v==3)
                            break;
                    }
                    if(v==3)
                        break;
                }
                if(flagy1==flagy2)
                {
                    int xx1=min(flagx1,flagx2);
                    xx1=min(xx1,flagx3);
                    int xx2=max(flagx1,flagx2);
                    xx2=max(xx2,flagx3);
                    int yy=flagy1;
 
                    if(xx1-1>0)
                    {
                        if(stu0.a[xx1-1][yy]==0)
                        {
                            //cout<<xx1-1<<" "<<yy<<" "<<k<<endl;
                            STU stu1;
                            rep(p,1,6)
                            {
                                rep(q,1,6)
                                {
                                    if(q==yy)
                                        continue;
                                    else
                                        stu1.a[p][q]=stu0.a[p][q];
                                }
                            }
                            rep(p,1,6)
                            {
                                if(p==xx1-1||p==xx1||p==xx1+1)
                                {
                                    stu1.a[p][yy]=stu0.a[p+1][yy];
                                }
                                eif(p==xx2)
                                {
                                    stu1.a[p][yy]=0;
                                }
                                else
                                    stu1.a[p][yy]=stu0.a[p][yy];
                            }
                            stu1.nm=stu0.nm+1;
                            string str="";
                            rep(i,1,6)
                            {
                                rep(j,1,6)
                                {
                                    char ch=(stu1.a[i][j]-'0');
                                    str=str+ch;
                                }
                            }
                            if(mapp[str]==0)
                            {
                                mapp[str]=1;
                                if(u[1]==2)
                                {
                                    if(stu1.nm<=8)
                                        que.push(stu1);
                                }
                                else
                                {
                                    if(stu1.nm<=7)
                                        que.push(stu1);
                                }
 
                            }
                        }
 
 
 
                    }
                    if(xx2+1<7)
                    {
                        if(stu0.a[xx2+1][yy]==0)
                        {
                            //cout<<xx2+1<<" "<<yy<<" "<<k<<endl;
                            STU stu1;
                            rep(p,1,6)
                            {
                                rep(q,1,6)
                                {
                                    if(q==yy)
                                        continue;
                                    else
                                        stu1.a[p][q]=stu0.a[p][q];
                                }
                            }
                            rep(p,1,6)
                            {
                                if(p==xx2||p==xx2+1||p==xx2-1)
                                {
                                    stu1.a[p][yy]=k;
                                }
                                eif(p==xx1)
                                {
                                    stu1.a[p][yy]=0;
                                }
                                else
                                    stu1.a[p][yy]=stu0.a[p][yy];
                            }
                            stu1.nm=stu0.nm+1;
                            string str="";
                            rep(i,1,6)
                            {
                                rep(j,1,6)
                                {
                                    char ch=(stu1.a[i][j]-'0');
                                    str=str+ch;
                                }
                            }
                            if(mapp[str]==0)
                            {
                                mapp[str]=1;
                                if(u[1]==2)
                                {
                                    if(stu1.nm<=8)
                                        que.push(stu1);
                                }
                                else
                                {
                                    if(stu1.nm<=7)
                                        que.push(stu1);
                                }
 
                            }
 
 
                        }
                    }
                }
                else
                {
                    int yy1=min(flagy1,flagy2);
                    yy1=min(yy1,flagy3);
                    int yy2=max(flagy1,flagy2);
                    yy2=max(yy2,flagy3);
                    int xx=flagx1;
                    if(yy1-1>0)
                    {
                        if(stu0.a[xx][yy1-1]==0)
                        {
                            STU stu1;
                            rep(p,1,6)
                            {
                                rep(q,1,6)
                                {
                                    if(p==xx)
                                        continue;
                                    else
                                        stu1.a[p][q]=stu0.a[p][q];
                                }
                            }
                            rep(p,1,6)
                            {
                                if(p==yy1-1||p==yy1||p==yy1+1)
                                {
                                    stu1.a[xx][p]=k;
                                }
                                eif(p==yy2)
                                {
                                    stu1.a[xx][p]=0;
                                }
                                else
                                    stu1.a[xx][p]=stu0.a[xx][p];
                            }
                            stu1.nm=stu0.nm+1;
                            string str="";
                            rep(i,1,6)
                            {
                                rep(j,1,6)
                                {
                                    char ch=(stu1.a[i][j]-'0');
                                    str=str+ch;
                                }
                            }
                            if(mapp[str]==0)
                            {
                                mapp[str]=1;
                                if(u[1]==2)
                                {
                                    if(stu1.nm<=8)
                                        que.push(stu1);
                                }
                                else
                                {
                                    if(stu1.nm<=7)
                                        que.push(stu1);
                                }
 
                            }
                        }
                    }
                    if(yy2+1<7)
                    {
                        if(stu0.a[xx][yy2+1]==0)
                        {
                            STU stu1;
                            rep(p,1,6)
                            {
                                rep(q,1,6)
                                {
                                    if(p==xx)
                                        continue;
                                    else
                                        stu1.a[p][q]=stu0.a[p][q];
                                }
                            }
                            rep(p,1,6)
                            {
                                if(p==yy2||p==yy2+1||p==yy2-1)
                                {
                                    stu1.a[xx][p]=k;
                                }
                                eif(p==yy1)
                                {
                                    stu1.a[xx][p]=0;
                                }
                                else
                                    stu1.a[xx][p]=stu0.a[xx][p];
                            }
                            stu1.nm=stu0.nm+1;
                            string str="";
                            rep(i,1,6)
                            {
                                rep(j,1,6)
                                {
                                    char ch=(stu1.a[i][j]-'0');
                                    str=str+ch;
                                }
                            }
                            if(mapp[str]==0)
                            {
                                mapp[str]=1;
                                if(u[1]==2)
                                {
                                    if(stu1.nm<=8)
                                        que.push(stu1);
                                }
                                else
                                {
                                    if(stu1.nm<=7)
                                        que.push(stu1);
                                }
 
                            }
                        }
                    }
 
 
 
                }
            }
        }
    }
    if(flag==0)
        cout<<-1<<'\n';
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值