poj1753Flip Game【刷题计划】

Flip Game

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 47766   Accepted: 20383

Description

Flip game is played on a rectangular 4x4 field with two-sided pieces placed on each of its 16 squares. One side of each piece is white and the other one is black and each piece is lying either it‘s black or white side up. Each round you flip 3 to 5 pieces, thus changing the color of their upper side from black to white and vice versa. The pieces to be flipped are chosen every round according to the following rules:

  1. Choose any one of the 16 pieces.
  2. Flip the chosen piece and also all adjacent pieces to the left, to the right, to the top, and to the bottom of the chosen piece (if there are any).

Consider the following position as an example:

bwbw 
wwww 
bbwb 
bwwb 
Here "b" denotes pieces lying their black side up and "w" denotes pieces lying their white side up. If we choose to flip the 1st piece from the 3rd row (this choice is shown at the picture), then the field will become:

bwbw 
bwww 
wwwb 
wwwb 
The goal of the game is to flip either all pieces white side up or all pieces black side up. You are to write a program that will search for the minimum number of rounds needed to achieve this goal.

Input

The input consists of 4 lines with 4 characters "w" or "b" each that denote game field position.

Output

Write to the output file a single integer number - the minimum number of rounds needed to achieve the goal of the game from the given position. If the goal is initially achieved, then write 0. If it‘s impossible to achieve the goal, then write the word "Impossible" (without quotes).

Sample Input

bwwb
bbwb
bwwb
bwww

Sample Output

4

题意:把一个4*4的棋盘翻转为颜色统一,翻转规则是翻转一个棋子时,这个棋子上下左右也要进行翻转。

思路:枚举。递归真是横贯搜索啊 ~~用好了真心感觉神奇,今天又重新写了一下之前写的这道题,发现又有了新的大陆。

#include<stdio.h>
#include<string.h>
#define inf 0x3f3f3f3f
int flag,min,map[4][4];
int CheckMap(int map[][4])
{
    for(int i = 0; i < 4; i ++)
        for(int j = 0; j < 4; j ++)
            if(map[i][j] != map[0][0])
            return 0;
    return 1;
}
void Reverse(int ans)
{
    int k[4][2] = {0,1,0,-1,-1,0,1,0};
    int x = ans/4;
    int y = ans%4;
    map[x][y] ^= 1;
    for(int i = 0; i< 4; i ++)
    {
        x = ans/4 + k[i][0];
        y = ans%4 + k[i][1];
        if(x < 0||y < 0||x > 3||y > 3)
            continue;
        map[x][y] ^= 1;
    }
    return;
}
void dfs(int ans,int step)
{
    if(CheckMap(map))
    {
        flag = 1;
        if(step < min)
            min = step;
        return;
    }
    if(ans == 16)
        return;
    dfs(ans+1,step);//不翻转当前棋子
    Reverse(ans);//翻转当前棋子
    dfs(ans+1,step+1);//翻转当前棋子步数+1,往下搜索
    Reverse(ans);    //复原
}
int main()
{

    char str[4][4];
    flag = 0;
    min = inf;
    for(int i = 0; i < 4; i ++)
    {
        for(int j = 0; j < 4; j ++)
        {
            scanf("%c",&str[i][j]);
            if(str[i][j] == ‘b‘)
                map[i][j] = 1;
            else
                map[i][j] = 0;
        }
        getchar();
    }
    dfs(0,0);
    if(!flag)
        printf("Impossible\n");
    else
        printf("%d\n",min);
    return 0;
}
时间: 2024-08-28 22:07:51

poj1753Flip Game【刷题计划】的相关文章

BZOJ第一页刷题计划

BZOJ第一页刷题计划 已完成:1 / 100 BZOJ1000:A+B

刷题计划

我很后悔这一年来被我浪费过的每分每秒,我已经不想再浪费时间了. 平时不好好刷题还想着打比赛? 今年的目标就是刷完紫书第七章的搜索,第八章的贪心,第九章的dp,然后每次的cf补题尽量补到div2的DE题,如果时间有剩余,就学AC自动机等一些数据结构吧. 第一阶段:紫书ch7-ch9  时间11月末-12月31号 第二阶段  训练指南选择性的补充知识点,同时打一些5个小时比赛+补题  时间1月-2月 第三阶段  区域赛真题组队训练

[填坑][主线任务]历年NOIP刷题计划

今天又是喜闻乐见的非考试日,那么今天做点什么呢== 前些日子的主线任务陆陆续续(接近)完成了,好多蒙蔽的没学好的算法都算是入门补坑了 我听学长说,做题的顺序是:NOIP真题->NOIP模拟题->专项练习->杂题 啊哈!最重要的真题我还没做几道呢...于是这两天填填这个坑吧 [NOIP2016 Day1 T2]天天爱跑步 NOIP2016最难的题没有之一QAQ,原来做过一直没过,今天重新听讲解,总算打了出来 [NOIP2015 Day2 T3]运输计划 压轴题防AK,最后一个点及其凶残,并

HDU5697 刷题计划 dp+最小乘积生成树

分析:就是不断递归寻找靠近边界的最优解 学习博客(必须先看这个): 1:http://www.cnblogs.com/autsky-jadek/p/3959446.html 2:http://blog.csdn.net/u013849646/article/details/51524748 注:这里用的最小乘积生成树的思想,和dp结合 每次找满足条件的最优的点,只不过BZOJ裸题的满足条件是形成一棵树 这个题是大于m,生成树借用最小生成树进行求解最优,大于m用dp进行求解最优 #include

刷题计划(转)

原文链接:http://my.oschina.net/lee24/blog/74949 初级: 一.基本算法:      (1)枚举. (poj1753,poj2965)      (2)贪心(poj1328,poj2109,poj2586)      (3)递归和分治法.      (4)递推.      (5)构造法.(poj3295)      (6)模拟法.(poj1068,poj2632,poj1573,poj2993,poj2996) 二.图算法:      (1)图的深度优先遍历和

poj 2586 Y2K Accounting Bug【贪心】【刷题计划】

Y2K Accounting Bug Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 16154   Accepted: 8111 Description Accounting for Computer Machinists (ACM) has sufferred from the Y2K bug and lost some vital data for preparing annual report for MS Inc

poj1068 Parencodings【模拟】【刷题计划】

Parencodings Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 27375   Accepted: 16094 Description Let S = s1 s2...s2n be a well-formed string of parentheses. S can be encoded in two different ways: q By an integer sequence P = p1 p2...pn

刷题计划(暑假)

暂定每天更新. 7.7 1. P1378 油滴扩展 2. P1306 斐波那契公约数 3. Bugs Integrated, Inc.(加深理解三进制dp) 4. UVa12206 Stammering Aliens(复习字符串hash) 5. P2921 [USACO08DEC]在农场万圣节Trick or Treat on the Farm 原文地址:https://www.cnblogs.com/wsmrxc/p/9275856.html

屯题计划

感觉lxt一天到晚就是在浪啊浪, 毫无斗志, 颓废得不得了, 每天看小说玩手机到三四点然后整个人都是乱七八糟的. 不行不行我要振作起来了! 从最开始学语言到现在都快两年过去了 T T , 代码能力还是渣成这样简直不能看 T T 觉得应该学学zj爷们屯题. 加油! (题目是直接从劼很久以前的几篇屯题计划里边搬过来的我之前做过的题就删掉了 [BZOJ1822][JSOI2010]Frozen Nova 冷冻波 很水的网络流加计算几何, 然而计算几何部分非常莫名奇妙, 题目应该是判断一个圆是否与一个线