首页 > 其他分享 >POJ 1753 Flip Game(枚举+递归)

POJ 1753 Flip Game(枚举+递归)

时间:2023-05-26 15:03:44浏览次数:72  
标签:right 格子 16 int Flip up Game bool POJ


传送门

思路是别人的,自己理解了半天,真是渣渣。对于自己,路还长,年轻人。

  • 对任意一个格子来说,翻动偶数次等于没翻,翻动奇数次等于翻一次,所以只需考虑翻一次的情况。
  • 一共16个格子,每个格子只有翻和不翻,所以最多16步,最少0步,题目要求最少的步数,所以0——>16枚举,看哪一步先成功就是最优解。
  • 使用dfs,要考虑到树根时如果还没成功,要回溯;回溯时要把棋子再翻回来,再搜。 
#include<iostream>
#include<cstdio>
using namespace std;
bool chess[6][6]={false};
int step;
bool flag;
int r[5]={-1,1,0,0,0};//row up,down,left,right,o
int c[5]={0,0,-1,1,0};//col up,down,left,right,o
bool isOver()
{
	for(int i = 1;i <= 4; i++)
	{
		for(int j = 1;j <= 4;j++)
		{
			if(chess[i][j] != chess[1][1])
			{
				return false;
			}
		}
	}
	return true;
}
void flip(int row,int col)
{
	int i;
	for(i = 0;i < 5;i++)
	{
		chess[row + r[i]][col + c[i]] = !chess[row + r[i]][col + c[i]];
	}
}

void dfs(int row,int col,int deep)
{
	if(deep == step)
	{
		flag = isOver();
		return;
	}
	if(flag || row == 5)
		return;
	flip(row,col);
	if(col < 4)
		dfs(row,col+1,deep+1);
	else
		dfs(row+1,1,deep+1);
	flip(row,col);
	if(col < 4)
		dfs(row,col + 1,deep);
	else
		dfs(row + 1,1,deep);
	return;
}
int main()
{
	char temp;
	for(int i = 1;i <= 4; i++)
	{
		for(int j = 1;j <= 4; j++)
		{
			scanf("%c",&temp);
			if('b' == temp)
			{
				chess[i][j] = true;
			}
		}
		getchar();
	}
	for(step = 0;step <= 16; step++)
	{
		dfs(1,1,0);
		if(flag)
			break;
	}
	if(flag)
        printf("%d\n",step);
	else
		printf("Impossible\n");
	return 0;
}

 

标签:right,格子,16,int,Flip,up,Game,bool,POJ
From: https://blog.51cto.com/u_16131191/6356119

相关文章

  • POJ 3069 Saruman's Army(贪心)
    传送门这个题是说给你n个点,然后让你标记其中尽可能少的点,使得n个点都处于被标记点左右不超过R的区间内我们使用的是贪心算法,也就是我们要将被标记的点尽可能的朝右边去即可,首先我们将他们从左到右进行排序,第一个我们所选取的被标记的点应该是能够包含掉左边的点的最靠右的点。。。......
  • POJ 1182 食物链
    解题思路:并查集经典中的经典题,在网上看了很多大牛的思路,大部分是增加一个结构体存动物间的关系,结合并查集判断,但是关系域的更新比较复杂,一下子不太容易理解。所以就有人另开思路,这里介绍一个十分巧妙的思路。一般我们都会把一个动物当成一个节点,然后去执行并查集等操作。但是有位大......
  • POJ 2251 Dungeon Master(三维BFS)
    题目看起来很厉害,实际上看懂了并不难,开一个三维的数组,这里需要注意的是第一维是高度,然后就是简单的BFS了,还有不同就是三维的时候有六个方向可以走,在前后左右的基础上多了一个向上和向下的走法,还有一个问题就是多个输入样例要注意每次都要初始化,我做的时候就因为这个WA了好几次,最后......
  • POJ 2080 Calendar
    题目链接:http://poj.org/problem?id=2080题目不是很难,也没什么说的,直接看代码吧:#include<iostream>#include<stdio.h>usingnamespacestd;intyear(intm){ if(m%4==0&&m%100!=0||m%400==0) return1; else return0;}intmain(){ intn,i,j......
  • 区分PO、VO、 BO、 DTO、 POJO
     分层领域模型规约:DO(DataObject):此结构与数据库表结构一一对应,通过DTO向上传输数据源对象。DTO(DataTransferObject):数据传输对象,Service或Manager向外传输的对象。BO(BusinessObject):业务对象,由Service层输出的封装业务逻辑的对象。AO(ApplicationObject):应用......
  • Tallest Cow(最高的牛)poj3263
    题目描述:FJ'sN(1≤N≤10,000)cowsconvenientlyindexed1..Narestandinginaline.Eachcowhasapositiveintegerheight(whichisabitofsecret).YouaretoldonlytheheightH(1≤H≤1,000,000)ofthetallestcowalongwiththeindexIoftha......
  • Game on Paper 题解
    题目传送门一道模拟题。如果每涂一个格子就判断整个矩阵,那时间复杂度显然会炸。我们每涂一个格子,影响的应该只是以这个格子为中心的\(3\times3\)矩阵,判断以这些点为中心的话会不会涂出\(3\times3\)的矩阵即可。Code#include<bits/stdc++.h>#definelllonglong#d......
  • poj-1519
    //132K0MSC++#include<cstdio>#include<cstring>usingnamespacestd;longlonggetDigitSum(longlongval){longlongdigitSum=0;if(val<10){returnval;}else{while(val){digitSum+=......
  • poj-2362
    //132K141MSC++withbeginSearchPos#include<cstdio>#include<cstring>#include<cstdlib>usingnamespacestd;intcmp(constvoid*a,constvoid*b)//降序{return*(longlong*)b-*(longlong*)a;}longlongfourEdgeLeng......
  • poj-2371
    //524K63MSC++#include<cstdio>#include<cstring>#include<cstdlib>intcmp(constvoid*a,constvoid*b){return*((int*)a)-*((int*)b);}usingnamespacestd;constintMAX=100001;intarray[MAX];intdataBaseSiz......