POJ1753 Flip Game(暴力枚举)

127 篇文章 0 订阅
19 篇文章 0 订阅

Flip Game

题解:因为数据只有 4 ⋅ 4 4\cdot 4 44 d f s dfs dfs暴力枚举每种情况即可。翻硬币次数最多不会超过 16 16 16次,因为翻两次相当于没有翻。

代码

#include<iostream>

using namespace std;
bool a[6][6];

bool check()
{
	char pre = a[0][0];
	for(int i = 0; i <= 3; ++i){
		for(int j = 0; j <= 3; ++j){
			if(a[i][j] != pre)
				return 0;
		}
	}
	return 1;
}

int change_row[] = {0,-1,0,1,0};
int change_col[] = {0,0,1,0,-1};

void flip(int row , int col){
	for(int i = 0 ; i < 5 ; i++)
		a[row+change_row[i]][col+change_col[i]] = !a[row+change_row[i]][col+change_col[i]];	
}

int f = 0,step = 0;
void dfs(int x,int y,int deep)
{
	if(step == deep){
		f = check();
		return;
	}
	if(f || x > 3 || y > 3 || x < 0 || y < 0) return;
	flip(x,y);
	if(x < 3)
		dfs(x + 1, y, deep + 1);
	else
		dfs(0, y + 1, deep + 1);
	flip(x,y);
	if(x < 3)
		dfs(x + 1, y, deep);
	else
		dfs(0, y + 1, deep);
	return;
}
int main()
{
#ifndef ONLINE_JUDGE
    freopen("input.in","r",stdin);
#endif
	char x;
	for(int i = 0; i <= 3; ++i){
		for(int j = 0; j <= 3; ++j){
			cin>>x;
			a[i][j] = ((x == 'w') ? 1 : 0);
		}
	}
	for(step = 0; step <= 16; ++step){
		dfs(0,0,0);
		if(f) break;
	}
	if(f)
		cout<<step<<endl;
	else
		cout<<"Impossible"<<endl;
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值