POJ1753

来源:互联网 发布:淘宝店家信用怎么看 编辑:程序博客网 时间:2024/04/30 23:01
Flip Game
Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u


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
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






解题思路:
        题意是给你一个棋盘,上面b代表黑子,w代表白子。每次翻转会使得该棋子和其上下左右的棋子发生翻转,

问最少翻转多少个棋子,可以使棋面全黑或全白,如果不能输出Impossible。

        DFS+枚举。用chess数组存棋盘,b标记为1,w标记为0。然后从(0,0)点开始遍历,对每个点都搜一遍。

DFS中如果deep == step,此时搜索结束,判断是否满足全黑或全白,满足judge返回true,不满足返回false。

如果flag == 1 或者搜索到row == 4(因为row从0开始记)也要终止搜索。接下来就是把当前点(row , col)翻转,

翻转的时候取非即可(注意点本身也要翻转!!!)。下面如果col < 3,即col + 1 < 4 ,那么按列加一搜索,否则

行加一搜索(注意每次行变换时,列的初始值为0)。最后就是回溯,如果翻完之后不符合的话要再翻回来。





完整代码:

#include <functional>#include <algorithm>#include <iostream>#include <fstream>#include <sstream>#include <iomanip>#include <numeric>#include <cstring>#include <climits>#include <cassert>#include <complex>#include <cstdio>#include <string>#include <vector>#include <bitset>#include <queue>#include <stack>#include <cmath>#include <ctime>#include <list>#include <set>#include <map>using namespace std;#define REP(i, n) for (int i=0;i<n;++i)#define FOR(i, a, b) for (int i=a;i<b;++i)#define DWN(i, b, a) for (int i=b-1;i>=a;--i)#define REP_1(i, n) for (LL i=1;i<=n;++i)#define FOR_1(i, a, b) for (int i=a;i<=b;++i)#define DWN_1(i, b, a) for (LL i=b;i>=a;--i)#define REP_C(i, n) for (int n____=n,i=0;i<n____;++i)#define FOR_C(i, a, b) for (int b____=b,i=a;i<b____;++i)#define DWN_C(i, b, a) for (int a____=a,i=b-1;i>=a____;--i)#define REP_N(i, n) for (i=0;i<n;++i)#define FOR_N(i, a, b) for (i=a;i<b;++i)#define DWN_N(i, b, a) for (i=b-1;i>=a;--i)#define REP_1_C(i, n) for (int n____=n,i=1;i<=n____;++i)#define FOR_1_C(i, a, b) for (int b____=b,i=a;i<=b____;++i)#define DWN_1_C(i, b, a) for (int a____=a,i=b;i>=a____;--i)#define REP_1_N(i, n) for (i=1;i<=n;++i)#define FOR_1_N(i, a, b) for (i=a;i<=b;++i)#define DWN_1_N(i, b, a) for (i=b;i>=a;--i)#define REP_C_N(i, n) for (int n____=(i=0,n);i<n____;++i)#define FOR_C_N(i, a, b) for (int b____=(i=0,b);i<b____;++i)#define DWN_C_N(i, b, a) for (int a____=(i=b-1,a);i>=a____;--i)#define REP_1_C_N(i, n) for (int n____=(i=1,n);i<=n____;++i)#define FOR_1_C_N(i, a, b) for (int b____=(i=a,b);i<=b____;++i)#define DWN_1_C_N(i, b, a) for (int a____=(i=b,a);i>=a____;--i)#define ECH(it, A) for (__typeof(A.begin()) it=A.begin(); it != A.end(); ++it)#define REP_S(i, str) for (char*i=str;*i;++i)#define REP_L(i, hd, suc) for (int i=hd;i;i=suc[i])#define REP_G(i, u) REP_L(i,hd[u],suc)#define REP_SS(x, s) for (int x=s;x;x=(x-1)&s)#define DO(n) for ( int ____n = n; ____n-->0; )#define REP_2(i, j, n, m) REP(i, n) REP(j, m)#define REP_2_1(i, j, n, m) REP_1(i, n) REP_1(j, m)#define REP_3(i, j, k, n, m, l) REP(i, n) REP(j, m) REP(k, l)#define REP_3_1(i, j, k, n, m, l) REP_1(i, n) REP_1(j, m) REP_1(k, l)#define REP_4(i, j, k, ii, n, m, l, nn) REP(i, n) REP(j, m) REP(k, l) REP(ii, nn)#define REP_4_1(i, j, k, ii, n, m, l, nn) REP_1(i, n) REP_1(j, m) REP_1(k, l) REP_1(ii, nn)#define ALL(A) A.begin(), A.end()#define LLA(A) A.rbegin(), A.rend()#define CPY(A, B) memcpy(A, B, sizeof(A))#define INS(A, P, B) A.insert(A.begin() + P, B)#define ERS(A, P) A.erase(A.begin() + P)#define LBD(A, x) (lower_bound(ALL(A), x) - A.begin())#define UBD(A, x) (lower_bound(ALL(A), x) - A.begin())#define CTN(T, x) (T.find(x) != T.end())#define SZ(A) int((A).size())typedef long long LL;//typedef long double DB;typedef double DB;typedef unsigned uint;typedef unsigned long long uLL;/** Constant List .. **/ //{const int MOD = int(1e9)+7;const int INF = 0x3f3f3f3f;const LL INFF = 0x3f3f3f3f3f3f3f3fLL;const DB EPS = 1e-9;const DB OO = 1e20;const DB PI = acos(-1.0); //M_PI;int chess[1111][1111];int step;int flag = 0;bool judge(){    for(int i = 0 ; i < 4 ; i ++)    {        for(int j = 0; j < 4 ; j ++)        {            if(chess[i][j] != chess[0][0])                return false;        }    }    return true;}void flip(int row , int col){    chess[row][col] = !chess[row][col];    if(row - 1 >= 0)        chess[row - 1][col] = !chess[row - 1][col];    if(row + 1 < 4)        chess[row + 1][col] = !chess[row + 1][col];    if(col - 1 >= 0)        chess[row][col - 1] = !chess[row][col - 1];    if(col + 1 < 4)        chess[row][col + 1] = !chess[row][col + 1];}void dfs(int row , int col , int deep){    if(deep == step)    {        flag = judge();        return ;    }    if(flag || row == 4)        return ;    flip(row , col);    if(col < 3)        dfs(row , col + 1 , deep + 1);    else        dfs(row + 1 , 0 , deep + 1);    flip(row , col);    if(col < 3)        dfs(row , col + 1 , deep);    else        dfs(row + 1 , 0 , deep);    return ;} char str[100];int main(){    #ifdef DoubleQ    freopen("in.txt","r",stdin);    #endif    memset(chess , 0 , sizeof(chess));    for(int i = 0 ; i < 4; i ++)    {        scanf("%s",str);        for(int j = 0; j < 4 ; j ++)        {            if(str[j] == 'b')                chess[i][j] = 1;        }    }    for(step = 0 ; step < 16 ; step ++)    {        dfs(0 , 0, 0);        if(flag)            break;    }    if(flag)        printf("%d\n",step);    else        printf("Impossible\n");}


0 0