POJ1753Flip Game(DFS + 枚举)

Flip Game

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 37050   Accepted: 16122

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

没点思路-_-

转载请注明出处:優YoU  http://user.qzone.qq.com/289065406/blog/1299076400

提示:翻转棋,可以建模为多叉树

本题难点有两个,一个就是不要以全黑(或全白)作为目标进行搜索,而是要把全黑(或全白)作为“根”,去搜索树叶,看看是否有 输入的棋盘状态。

另一个难点需要一点数学功底,就是要知道 树 的最大高度,这是“状态不存在”的判断标准

提示:其实每格棋子最多只可以翻转一次(实际是奇数次,但这没意义),只要其中一格重复翻了2(不论是连续翻动还是不连翻动),那么它以及周边的棋子和没翻动时的状态是一致的,由此就可以确定这个棋盘最多只能走16步,最多只能有翻出2^16种状态

其实也想过dfs,找不到终止状态,对,就是16就ok了,因为如果每一个状态都翻转了一遍,那么你在翻转一个牌,那么就和他之前没翻转是一样的,所以没必要了

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
bool chess[10][10];
int step,flag;
int r[4] = {-1,1,0,0};
int c[4] = {0,0,-1,1};
int judge_all()
{
    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)
{
    chess[row][col] = !chess[row][col];
    for(int i = 0; i < 4; i++)
    {
        int fx = row + r[i];
        int fy = col + c[i];
        if(fx > 0 && fx <= 4 && fy > 0 && fy <= 4)
            chess[fx][fy] = !chess[fx][fy];
    }
}
void dfs(int row, int col, int deep)
{
    if(deep == step)  // 到达了翻转的指定次数,就判断是否都一样,然后返回
    {
        flag = judge_all();
        return;
    }
    if(flag || row > 4)
        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); //回溯时要判断上一步是否已经结束,因此传的是deep,判断当前状态,如果没结束,就按row,col+1变化
    }
    else
    {
        dfs(row + 1, 1, deep);
    }
    return;
}
int main()
{
    char s[10];
    while(scanf("%s", s) != EOF)
    {
        memset(chess, false, sizeof(chess));
        for(int i = 0; i < 4; i++)
        {
            if(s[i] == ‘b‘)
            {
                chess[1][i + 1] = true;
            }
        }
        for(int i = 2; i <= 4; i++)
        {
            scanf("%s", s);
            for(int j = 0; j < 4; j++)
            {
                if(s[j] == ‘b‘)
                    chess[i][j + 1] = true;
            }
        }
        flag = 0;
        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;
}
时间: 2024-11-06 23:34:24

POJ1753Flip Game(DFS + 枚举)的相关文章

POJ1288 Sly Number(高斯消元 dfs枚举)

由于解集只为{0, 1, 2}故消元后需dfs枚举求解 #include<cstdio> #include<iostream> #include<cstdlib> #include<cstring> #include<string> #include<algorithm> #include<map> #include<queue> #include<vector> #include<cmath

HDU 4068 dfs枚举

SanguoSHA Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 944    Accepted Submission(s): 520 Problem Description Sanguosha has a singled version. Two players each select N heroes and start fight

codeforce Pashmak and Buses(dfs枚举)

1 /* 2 题意:n个同学,k个车, 取旅游d天! 3 要求所有的学生没有两个或者两个以上的在同一辆车上共同带d天! 输出可行的方案! 4 5 对于d行n列的矩阵,第i行第j列表示的是第i天第j个同学所在的车号! 6 也就是保证所有行不全相同,即每一列都是不相同的! 7 如果每一列都不相同就是表示第j个同学(第j列)在这d天中不会和其他同学(列)在这d天中 都在同一辆车中! 8 9 思路:对于每一列我们枚举d天该学生所在的车号!它的下一列只保证有一个元素和它不同就行了!依次下去! 10 11

hdu 2489 Minimal Ratio Tree(dfs枚举 + 最小生成树)~~~

题目: 链接:点击打开链接 题意: 输入n个点,要求选m个点满足连接m个点的m-1条边权值和sum与点的权值和ans使得sum/ans最小,并输出所选的m个点,如果有多种情况就选第一个点最小的,如果第一个点也相同就选第二个点最小的........ 思路: 求一个图中的一颗子树,使得Sum(edge weight)/Sum(point weight)最小~ 数据量小,暴力枚举~~~~~dfs暴力枚举C(M,N)种情况. 枚举出这M个点之后,Sum(point weight)固定,进行prim或者K

hdu1045 Fire Net(DFS枚举)

http://acm.hdu.edu.cn/showproblem.php?pid=1045 这是在贪心分类里面的题目,然而我第一眼看到时候还是想到了dfs,毕竟只有4*4……数据小,枚举也能AC 用DFS枚举所有状态…… #include <iostream> #include <cstdio> #include <cstdlib> #include <cstring> #include <algorithm> #include <cma

poj2965The Pilots Brothers&#39; refrigerator DFS+枚举

The Pilots Brothers' refrigerator Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 20858   Accepted: 8048   Special Judge Description The game "The Pilots Brothers: following the stripy elephant" has a quest where a player needs to o

POJ 1753 Flip Game (DFS + 枚举)

题目:http://poj.org/problem?id=1753 这个题在开始接触的训练计划的时候做过,当时用的是DFS遍历,其机制就是把每个棋子翻一遍,然后顺利的过了,所以也就没有深究. 省赛前一次做PC2遇到了几乎一模一样的题,只不过是把棋盘的界限由4X4改为了5X5,然后一直跑不出结果来,但是当时崔老湿那个队过了,在最后总结的时候,崔老湿就说和这个题一样,不过要枚举第一行进行优化. 我以为就是恢复第一行然后第二行以此类推,不过手推一下结果是6不是4,就知道这个有问题. 问了崔老湿,问了+

poj 1753 Flip Game(bfs状态压缩 或 dfs枚举)

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 f

poj1753 Flip Game DFS,枚举

Flip Game Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 34437   Accepted: 15058 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