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

     

  • 相关阅读:
    asr相关技术总结
    SLURM 使用基础教程
    FSMN 及其变种 cFSMN DFSMN pyramidal-FSMN
    均方根误差(RMSE),平均绝对误差 (MAE),标准差 (Standard Deviation)
    linux文本编码格式转化 字幕处理
    PyTorch-Kaldi 语音识别工具包
    SRILM Ngram 折扣平滑算法
    awk 调用 shell 命令,并传递参数
    用 scikit-learn 和 pandas 学习线性回归
    逻辑回归 及 实例
  • 原文地址:https://www.cnblogs.com/lidaojian/p/2363910.html
Copyright © 2011-2022 走看看