POJ_2965_The Pilots Brothers' refrigerator_生生搜索

本来想六点起结果五点半就醒了怎么破?


题意:

一个奇葩冰箱上有4*4方格把锁,没把锁有开(-)关(+)两个状态,当所有锁都为开(-)时可以打开冰箱,可以对任意一把锁进行一种操作,改变其状态,但同时和它同一行、同一列的所有锁都会改变状态,求最小操作步数(题中没说打不开的情况,应该没有打不开的数据或者不可能打不开?),并输出该方案改变的锁的坐标(有多种可能输出其中任意一种)。



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.

思路很简单,用dfs深搜,貌似也可以广搜?不过要记录没想过行不行。

昨天晚上也做了一个这种题,那个题不用输出改变的具体方案,只要数量,只用一个cnt记录就行了,这个题我不知道只回溯一下cnt然后改改记录的矩阵到底行不行,我记得做题的时候貌似意淫出来反例了,所以对于每一次dfs我都建了一个记录操作的矩阵,这样肯定不会造成不同节点的操作互相留下遗留问题,这个办法内存消耗应该太大了,网上应该有不少更好的解法。本来想用引用传递数组的,结果指向数组的指针的引用傻傻的写不清楚啊,就重新改成结构体装数组了。

但是我代码渣啊!!加上这两天天天打代码啊!!!加上今天五点半起啊!!!然后做了半个上午啊!!!我想死啊!!!昨天晚上做那个差不多的题的时候少改了一个坐标也错了好久啊!!!尼煤啊!!!码力呢!!!


代码如下:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<cmath>
using namespace std;
bool a[4][4];
struct item{
	int mark[4][4];
	bool operator == (const item& in)const{
		for(int i=0;i<4;++i)
			for(int j=0;j<4;++j)
				if(mark[i][j]!=in.mark[i][j])
					return false;
		return true;
	}
	item& operator = (item& in){
		for(int i=0;i<4;++i)
			for(int j=0;j<4;++j)
				mark[i][j]=in.mark[i][j];
		return *this;
	}
	item(){memset(mark,0,sizeof(mark));}
};
item inf;
bool check(){
	for(int i=0;i<4;++i)
		for(int j=0;j<4;++j)
			if(a[i][j])	return false;
	return true;
}
void change(int x,int y){
	for(int i=x;i>=0;--i)	a[i][y]=1-a[i][y];
	for(int i=x+1;i<4;++i)	a[i][y]=1-a[i][y];
	for(int i=y-1;i>=0;--i)	a[x][i]=1-a[x][i];
	for(int i=y+1;i<4;++i)	a[x][i]=1-a[x][i];
}
int cnt(item in){
	if(in.mark[0][0]==-1)	return 100;
	int ret=0;
	for(int i=0;i<4;++i)
		for(int j=0;j<4;++j)
			if(in.mark[i][j]==1)	++ret;
	return ret;
}
item dfs(int x,int y,item flag){
	item tem=flag;
	if(x==4&&!y)
		if(check())
			return flag;
		else	return inf;
	int nxt_x=x, nxt_y=y+1;
	if(y==3)	nxt_x=x+1,nxt_y=0;
	item ret=dfs(nxt_x,nxt_y,flag);
	change(x,y);
	tem.mark[x][y]=1;
	item temp=dfs(nxt_x,nxt_y,tem);
	change(x,y);
	if(cnt(ret)<cnt(temp))	return ret;
	return temp;
}
int main(){
	for(int i=0;i<4;++i)
		for(int j=0;j<4;++j)	inf.mark[i][j]=-1;
	memset(a,0,sizeof(a));
	for(int i=0;i<4;++i)
		for(int j=0;j<4;++j){
			char tem;
			cin>>tem;
			if(tem=='+')	a[i][j]=1;
		}
	item in;
	item ans=dfs(0,0,in);
	if(ans==inf){	puts("ntmzdw");	return 0;}
	int num=cnt(ans);
	printf("%d\n",num);
	for(int i=0;i<4;++i)
		for(int j=0;j<4;++j)
			if(ans.mark[i][j])
				printf("%d %d\n",i+1,j+1);
	return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值