B - The Pilots Brothers' refrigerator

B - The Pilots Brothers’ refrigerator

题目描述:

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:

题目大意:

就是有一个特别大的冰箱,有16个把手,每个把手有2种状态,+与-,如果全为-的时候,就可以打开冰箱。4v4中任何一个地方,改变一个地方,就会改变当前位置与所在到行与列的状态。输出最少次数打开冰箱,以及路径。

思路分析:

这道题和flip game 的思路差不多,只是多了一个数组记录状态就行了,也是通过一个一个去搜,就行了,可以用BFS找出最短路径去写,也可以用DFS去压栈,在输出即可,不过自己还是要发一下牢骚,这道题 因为错将==写成=导致不知道哪里出问题,卡了整整3个小时,心态炸了。

Sample Input:

在这里插入图片描述

Sample Output:

在这里插入图片描述

代码块:

	#include<stdio.h>
	char c1[4][4];//把手状态
	int x2[1000][16];//记录路径x轴
	int y2[1000][16];//记录路径y轴
	int min=22,n1=0,n2;//min只要大于16就行了,随便设。
	void lemone3(int x,int y,int num);//num记录步数
	int len()//判断是否全为'-'号
	{
		int b,c;
		for(b=0;b<4;b++)
		{
			for(c=0;c<4;c++)
			{
				if(c1[b][c]=='+')
				{
					return 0;
				}
			}
		}
		return 1;
	}
	void Lemonscanf()//记录冰箱把手状态
	{
		int a,b;
		for(b=0;b<4;b++)
		{
			gets(c1[b]);
		 } 
	}
	void LemonDFS(int x,int y,int num)//深搜开始,num记录步数
	{
		int c,x1,y1;
		if(x>=4)//如果x>=4,说明已经翻转完了
		{
			if(len())
			{
				if(num<min)//比较步数
				{
					min=num;
					for(c=0;c<min;c++)//这一步类似走迷宫,上一次
					//步数的路径复制给下一次步数的路径。
					{
						y2[c][0]=x2[c][0];
						y2[c][1]=x2[c][1];
					}
				}
			}
			return;
	}
	//
		x1=x+(y+1)/4;
		y1=(y+1)%4;
		//这2步主要是类似在4V4棋盘中一个一个的翻。
		lemone3(x,y,num);
		x2[num][0]=x;//记录路径x轴
		x2[num][1]=y;//记录路径y轴
		LemonDFS(x1,y1,num+1);
		// 这一步是退到上一步,回到上一个节点。
		lemone3(x,y,num);
		LemonDFS(x1,y1,num);
		
	}
	void lemone3(int x,int y,int num)//改变状态开始
	{
		int b,c;
		if(x>=4)
		{
			return;
	}
		for(b=0;b<4;b++)
		{
			if(c1[b][y]=='-')
		{
			c1[b][y]='+';
		}
		else
		{
			c1[b][y]='-';
		}
		}
		for(b=0;b<4;b++)
		{
			if(b==y)continue;
			if(c1[x][b]=='-')
		{
			c1[x][b]='+';
		}
		else
		{
			c1[x][b]='-';
		}
		}
	}
	void Lemonprintf()//输出步数与路径
	{
		int b;
		printf("%d\n",min);
		for(b=0;b<min;b++)
		{
			printf("%d %d\n",y2[b][0]+1,y2[b][1]+1);
		}
		
	}
	int main()
	{
		Lemonscanf();//记录冰箱状态
		LemonDFS(0,0,0);//开始搜索
		Lemonprintf();//输出最少步数与路径
	}
发布了49 篇原创文章 · 获赞 6 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/xiaosuC/article/details/104078087