The Pilots Brothers' refrigerator

本文介绍了一个基于矩阵操作的游戏谜题解决方案。玩家需要通过改变冰箱门上16个把手的状态来打开冰箱,每个把手有两种状态:开或关。文章提供了一段C++代码,实现了使用广度优先搜索算法找到打开冰箱所需的最少操作步骤。

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

The game “The Pilots Brothers: following the stripy elephant” has a quest where a player needs to open a refrigerator.

There are 16 handles on the refrigerator door. Every handle can be in one of two states: open or closed. The refrigerator is open only when all handles are open. The handles are represented as a matrix 4х4. You can change the state of a handle in any location [i, j] (1 ≤ i, j ≤ 4). However, this also changes states of all handles in row i and all handles in column j.

The task is to determine the minimum number of handle switching necessary to open the refrigerator.

Input

The input contains four lines. Each of the four lines contains four characters describing the initial state of appropriate handles. A symbol “+” means that the handle is in closed state, whereas the symbol “−” means “open”. At least one of the handles is initially closed.

Output

The first line of the input contains N – the minimum number of switching. The rest N lines describe switching sequence. Each of the lines contains a row number and a column number of the matrix separated by one or more spaces. If there are several solutions, you may give any one of them.

Sample Input

-+--
----
----
-+--

Sample Output

6
1 1
1 3
1 4
4 1
4 3
4 4
#include<iostream>
#include<cstring>
#include<string>
#include<queue>
using namespace std;
int wei[20]= { 63624, 62532, 61986,61713, 36744,20292,12066,7953,35064, 17652,8946,4593,34959, 17487, 8751, 4383};//16种状态
bool vis[65536+10];
int dis[65536+10];

int num;

struct node
{
    int t;
    int step;
};
void bfs()
{
    queue<node>Q;
    node now,next;
    now.t=num;
    now.step=0;
    Q.push(now);
    while(!Q.empty())
    {
        now=Q.front();
        Q.pop();
        for(int i=0; i<16; i++)
        {
            next.t=now.t^wei[i];//取异或变状态
            if(!vis[next.t])//判断当前状态是否出现
            {
                dis[next.t]=i;
                next.step=now.step+1;
                vis[next.t]=true;
                Q.push(next);
            }
            if(next.t==0)//判断是否达到目标状态
            {
                cout<<now.step+1<<endl;
                int j=next.t;
                while(j!=num)//输出坐标位置
                {
                    cout<<dis[j]/4+1<<" "<<dis[j]%4+1<<endl;
                    j=j^wei[dis[j]];
                }
                return ;
            }
        }
    }
    return ;
}

int main()
{
    char str;
    memset(vis,false,sizeof(vis));
    num=0;
    for(int i=0; i<4; i++)
    {
        for(int j=0; j<4; j++)//状态压缩,变为具体的数
        {
            cin>>str;
            if(str=='+')
                num=(num<<1)+1;
            else num=(num<<1);
        }
    }
    //cout<<num<<endl;
    if(num==0)
    {
        cout<<0<<endl;
        return 0;
    }
    else
    {
        vis[num]=true;
        bfs();
    }
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值