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
题意:每次把“+”或者“-”所对应的行,列全变成另一个。看最终全变成“-”需要多少步,输出路径。
思路:深搜,搜索下,每次两种情况翻或者不翻,记录下zu最短路径就好了。
代码:
#include<stdio.h>
#include<string.h>
#define INF 0x3f3f3f3f
char str[10];
int mp[10][10],vis[105][5],v[105][5];
int step;
int check()
{
for(int i=0; i<4; i++)
for(int j=0; j<4; j++)
{
if(mp[i][j]!=1) return 0;
}
return 1; //判断是否达到目标状态
}
void fun(int x,int y)
{
mp[x][y]=!mp[x][y];
for(int i=0; i<4; i++)
{
mp[x][i]=!mp[x][i];
mp[i][y]=!mp[i][y]; //改变状态
}
}
void dfs(int x,int y,int t)
{
if(check())
{
if(step>t)
{
step=t;
for(int i=0;i<t;i++)
{
v[i][0]=vis[i][0];
v[i][1]=vis[i][1]; //记录最短路径
}
return ;
}
}
if(x>=4||y>=4)
return ;
int dx=(x+1)%4;
int dy=y+(x+1)/4;
dfs(dx,dy,t);
vis[t][0]=x;
vis[t][1]=y; //当前位置
fun(x,y);
dfs(dx,dy,t+1);
fun(x,y); //每次两种情况,翻与不翻
return ;
}
int main()
{
for(int i=0; i<4; i++)
{
scanf("%s",str);
for(int j=0; j<4; j++)
{
if(str[j]=='+')
mp[i][j]=0;
else
mp[i][j]=1; //标记“+”或者“-”号,后面容易改变状态
}
}
step=INF;
dfs(0,0,0);
printf("%d\n",step);
for(int i=0; i<step; i++)
printf("%d %d\n",v[i][0]+1,v[i][1]+1);
}
本文介绍了一款游戏中的谜题解决方法,玩家需通过最少的步骤将初始状态为闭合或开启的冰箱门全部打开。采用深度优先搜索算法记录最优路径,并详细展示了算法实现的代码。
1267

被折叠的 条评论
为什么被折叠?



