POJ 1753 Flip Game(枚举+回溯法)

来源:互联网 发布:莫言生死疲劳淘宝 编辑:程序博客网 时间:2024/06/05 14:21

Time Limit: 1000MS Memory Limit: 65536K

Problem 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:
Choose any one of the 16 pieces.
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
POJ1753
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


问题分析

这道题的大意就是给你一个4X4的矩阵,每个格子都被标记为白色或者黑色,然后你可以对某一个格子进行flip操作,这项操作会使格子本身以及它的上下左右共5个格子的颜色发生变化(从题干中可以很清楚地看出),问最少需要多少步可以使16个格子全部变为一种颜色。
解题的主要思想是枚举和递归。对于一个格子而言,它只有被操作和没被操作两个状态,由于总共只有16个格子,所以从给定的某一个状态出发,最多只有216种不同的状态,利用递归一一枚举判断就可得到答案。

AC代码

#include<iostream>#include<cstdio>#include<cstdlib>#include<algorithm>#include<cmath>#define ll long long#define ull unsigned long long#define rep(i,a,b) for (int i=(a),_ed=(b);i<=_ed;i++)#define rrep(i,a,b) for(int i=(a),_ed=(b);i>=_ed;i--)#define fil(a,b) memset((a),(b),sizeof(a))#define cl(a) fil(a,0)#define PI 3.1415927#define inf 0x3f3f3f3fusing namespace std;char ini[6][6];//防止数组越界int maxstep;int flag = 0;int judge()//判断16个格子是否颜色相同{    char std = ini[1][1];    rep(i, 1, 4)    {        rep(j, 1, 4)        {            if (std != ini[i][j]) return 0;        }    }    return 1;}void tran(int i, int j)//翻转函数{    if (ini[i][j] == 'w') ini[i][j] = 'b';    else ini[i][j] = 'w';}void flip(int i, int j){    tran(i, j);tran(i-1, j);tran(i+1, j);tran(i, j-1);tran(i, j+1);}void dfs(int r, int c, int step){    if (step == maxstep) //step表示已经走的步数,maxstep表示在当前循环中允许走的最大步数,所以当步数走满就要进行判断    {        flag = judge();        return;    }    if (flag||r == 5) return;//如果已经找到答案或者行数已经到了不可能到达的第5层,就退出    flip(r, c);    if (c < 4) dfs(r, c + 1, step + 1);//翻转过后,开始对下一个格子进行操作    else dfs(r + 1, 1, step + 1);    flip(r, c);//将格子翻回来,体现了回溯法的思想(因为每个格子有翻和不翻两种状态)    if(c<4) dfs(r, c + 1, step );    else dfs(r + 1, 1, step );}int main(void){    rep(i, 1, 4)    {        rep(j, 1, 4) cin >> ini[i][j];    }    for(maxstep=0;maxstep<=16;++maxstep)//可能的答案就在0步到16步之间    {        dfs(1, 1, 0);        if (flag) break;    }    if (flag) cout << maxstep << endl;    else cout << "Impossible\n";    return 0;}

思路来源于http://blog.sina.com.cn/s/blog_6635898a0100ivzv.html

2016 年 09月 15日

0 0