POJ 1753: Flip Game

17 篇文章 0 订阅

http://poj.org/problem?id=1753

题目意思很简单,和POJ1222题一样,前面在POJ1222的解题报告中已经将思路写的很清楚,这里不再赘述。这个题是找出最短的翻转步骤。同样也是遍历解空间。不过需要我们完全遍历,并且分为翻成全白和全黑两种这里都要计算。程序如下:

/*
ID: csuchenan
PROG: POJ 1753 Flip Game
LANG: C++
*/

#include<iostream>
#include<string.h>
#include<stdio.h>
using namespace std ;

int press[6][7] ;
int state[6][7] ;

int comple() ;
void judge(int flag) ;

int nmin ;

int main()
{
	char c ;
	
	int i ;
	int j ;
	
	memset(press , 0 , sizeof(press)) ;
	memset(state , 0 , sizeof(state)) ;
	
	for(i = 1 ; i <= 4 ; i ++)
	{
		for(j = 1 ; j <= 4 ; j ++)
		{
			c = getchar() ;
			
			if(c=='b')
			{
				state[i][j] = 0 ;
			}
			else if(c=='w')
			{
				state[i][j] = 1 ;
			}
		}
		getchar() ;
	}
	
	nmin = -1 ;
	
	comple() ;
	
	if(nmin >= 0)
		printf("%d\n" , nmin) ;
	else
		printf("Impossible\n") ;
	return 0 ;
}

int comple()
{	
	int c ;
	int ncount = 0 ;
	
	while(ncount < 16)
	{
		judge(0) ;
		judge(1) ;
		
		press[1][1] ++ ;
		
		c = 1 ;
		
		if(nmin == 0)//没有比0更小的翻转次数了,有这个判断条件时,16ms ,没有时32ms
			break ;
			
		while(press[1][c] > 1 && c <= 5)
		{
			press[1][c] = 0 ;
			c ++ ;
			press[1][c] ++ ;
		}
		ncount ++ ;
	}
	return 0 ;
}

void judge(int flag) 
{
	int c ;
	int r ;
	
	for(r = 1 ; r < 4 ; r ++)
	{
		for(c = 1 ; c <= 4 ; c ++)
		{
			press[r+1][c] = (state[r][c] + press[r-1][c] + press[r][c-1] + press[r][c+1] + press[r][c] + flag) % 2 ;
		}
	}
	
	for(c = 1 ; c <= 4 ; c ++)
	{
		if((press[r][c] + press[r-1][c] + press[r][c-1] + press[r][c+1] + state[r][c] + flag)%2 != 0 )
			return ;
	}
	
	int nsum ;
	
	nsum = 0 ;
	
	for(r = 1 ; r <= 4 ; r ++)
	{
		for(c = 1 ; c <= 4 ; c ++)
			nsum += press[r][c] ;
	}
	
	if(nmin == -1 || nmin > nsum)
		nmin = nsum ;
}



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值