zoukankan      html  css  js  c++  java
  • POJ 1753 Flip Game(枚举+DFS)

    Flip Game
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 17012   Accepted: 7377

    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
    

    Source

    Northeastern Europe 2000

    解题报告:利用枚举的方法,因为最多的步骤是16步,每次再进行DFS深搜,若搜到即为最小步骤,否则就不存在使得全部为白或黑,

    代码如下:

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    using namespace std;
    const int N = 10;
    int map[N][N], flag, ans;
    int move[4][2] = {{-1, 0}, {0, -1}, {1, 0}, {0, 1}};
    void Change(int x, int y) //翻转函数
    {
    int p, q, i;
    map[x][y] = ! map[x][y];
    for (i = 0; i < 4; ++i)
    {
    p = x + move[i][0];
    q = y + move[i][1];
    if (q >=1 && p>=1 && p <= 4 && q <=4)
    {
    map[p][q] = ! map[p][q];
    }
    }
    }
    int Judge() //判断函数
    {
    int i, j;
    for(i = 1; i <= 4; ++i)
    {
    for(j = 1; j <= 4; ++j)
    {
    if(map[1][1] != map[i][j])
    {
    return 0;
    }
    }
    }
    return 1;
    }
    void DFS(int x, int y, int step)
    {
    if (ans == step)
    {
    if (Judge())
    {
    flag = 1;
    return;
    }
    }
    if (flag || x >= 5)
    {
    return ;
    }
    Change(x, y);
    if (y < 4)
    {
    DFS(x, y + 1, step + 1);
    }
    else
    {
    DFS(x + 1, 1, step + 1);
    }
    Change(x , y); //若没有得到答案,恢复原来的状态
    if (y < 4)
    {
    DFS(x, y + 1, step);
    }
    else
    {
    DFS(x + 1, 1, step);
    }
    }
    int main()
    {
    char str;
    int i, j;
    memset(map, 0, sizeof(map));
    for (i = 1; i <= 4; ++i)
    {
    for (j = 1; j <= 4; ++j)
    {
    scanf("%c", &str);
    if (str == 'w')
    {
    map[i][j] = 1;
    }
    }
    getchar();//度掉回车
    }
    flag = 0;
    for (i = 0; i <= 16; ++i)//枚举方法
    {
    ans = i;
    DFS(1, 1, 0);
    if (flag)//若找的则退出,即为最小
    {
    break;
    }
    }
    if (flag)
    {
    printf("%d\n", ans);
    }
    else
    {
    printf("Impossible\n");
    }
    return 0;
    }

     

  • 相关阅读:
    POJ 2155:Matrix 二维树状数组
    POJ 2823:Sliding Window 单调队列
    POJ 3007:Organize Your Train part II
    51nod 1208 && POJ 2482:Stars in Your Window
    POJ 3061:Subsequence 查找连续的几个数,使得这几个数的和大于给定的S
    51nod 1206:Picture 求覆盖周长
    POJ 1195:Mobile phones 二维树状数组
    lightoj 1319
    暴力大法好
    Poj1273--Drainage Ditches(最大流)
  • 原文地址:https://www.cnblogs.com/lidaojian/p/2363910.html
Copyright © 2011-2022 走看看