首页 > 代码库 > POJ 1753 (枚举+DFS)

POJ 1753 (枚举+DFS)

Flip Game
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 40632 Accepted: 17647

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

bwwbbbwbbwwbbwww

Sample Output

4

Source

Northeastern Europe 2000

 

题目地址:http://poj.org/problem?id=1753

#include<stdio.h>#include<string.h>#include<iostream>#include<stdlib.h>using namespace std;#define N  7bool mat[N][N], flag;int deep;int dx[] = {1, -1, 0, 0, 0};int dy[] = {0, 0, 1, -1, 0};void Init(){    char c;    for(int i = 1; i <= 4; i++)    {        for(int j = 1; j <= 4; j++)        {            scanf(" %c",&c);            if(c == b)            {                mat[i][j] = 1;            }            else            {                mat[i][j] = 0;            }        }    }}void Print(){    for(int i = 1; i <= 4; i++)    {        for(int j = 1; j <= 4; j++)            printf("%d", mat[i][j]);        printf("\n");    }}void Change(int x, int y){    int next_x, next_y;    for(int k = 0; k < 5; k++)    {        next_x = x + dx[k];        next_y = y + dy[k];        //if(next_x >= 1 && next_x <=4 && next_y >= 1 && next_y <=4)        //{            mat[next_x][next_y] = !mat[next_x][next_y];        //}    }}void Flip(int x){    switch(x)   //1~16种case代表4*4的16个格子    {        case 1: Change(1,1); break;        case 2: Change(1,2); break;        case 3: Change(1,3); break;        case 4: Change(1,4); break;        case 5: Change(2,1); break;        case 6: Change(2,2); break;        case 7: Change(2,3); break;        case 8: Change(2,4); break;        case 9:  Change(3,1); break;        case 10: Change(3,2); break;        case 11: Change(3,3); break;        case 12: Change(3,4); break;        case 13: Change(4,1); break;        case 14: Change(4,2); break;        case 15: Change(4,3); break;        case 16: Change(4,4); break;    }}bool Result(){    for(int i = 1; i <= 4; i++)    {        for(int j = 1; j <= 4; j++)            if(mat[i][j] != mat[1][1])                return false;    }    return true;}void Dfs(int x, int dp){    if(flag || dp > deep || x>16) return;    //printf("DFS(%d, %d)\n", x, dp);    Flip(x);    //Print();printf("---------\n");    if(dp == deep)    {        flag = (flag || Result());        if(flag)        {            //printf("OK!!!!!!!!!!!!!!");            //exit(0);            goto A;        }    }    Dfs(x+1, dp+1);    Flip(x);    //Print();printf("---------\n");    Dfs(x+1, dp);    A: return;}int main(){//每个棋子最多翻一次(其实是奇数次,但是没意义),翻偶数次和没翻一样,所以每个棋子就两种状态,翻或者不翻//所以一共就有2^16次方种可能,枚举这些可能就行了,DFS//从0到16,如果16还找不到那就是Impossible    int a, b;    Init();    //Print();    flag = false;    if(Result())    {        cout<<0<<endl;        return 0;    }    for(deep = 1; deep <= 16; deep++)    {        //cout<<"deep = "<<deep<<endl;        Dfs(1, 1);        if(flag) break;    }    if(flag) cout<<deep<<endl;    else cout<<"Impossible"<<endl;//    while(cin>>a)//    {//        Flip(a);//        Print();//    }    return 0;}/*bwwbbbwbbwwbbwwwwwwwwwwwwwwwwwwwbbbwwbwwwwwwwwwwwwwwwwwwwwwbwwbbbbwwbwwwwwwwwwwwwwbwbbwwwwwwwwww*/

 

POJ 1753 (枚举+DFS)