zoukankan      html  css  js  c++  java
  • 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

    bwwb
    bbwb
    bwwb
    bwww

    Sample Output

    4
    

    Source

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

    #include<stdio.h>
    #include<string.h>
    #include<iostream>
    
    #include<stdlib.h>
    
    
    using namespace std;
    
    #define N  7
    
    bool 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("
    ");
        }
    }
    
    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)
    ", x, dp);
    
        Flip(x);
        //Print();printf("---------
    ");
        if(dp == deep)
        {
            flag = (flag || Result());
            if(flag)
            {
                //printf("OK!!!!!!!!!!!!!!");
                //exit(0);
                goto A;
            }
    
        }
        Dfs(x+1, dp+1);
        Flip(x);
        //Print();printf("---------
    ");
        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;
    }
    /*
    bwwb
    bbwb
    bwwb
    bwww
    
    wwww
    wwww
    wwww
    wwww
    
    bbbw
    wbww
    wwww
    wwww
    
    wwww
    wwww
    wwwb
    wwbb
    
    bbww
    bwww
    wwww
    wwww
    
    wwbw
    bbww
    wwww
    wwww
    */
  • 相关阅读:
    SVN cannot be opened because the project file cannot be parsed.xcode有几种可能错
    Cornerstone是mac操作 如何恢复到某一版本
    这个方法可以留着 + 上传图片
    UIImagePickerController从拍照、图库、相册获取图片
    实现多选相册的图片 demo
    (今天的一个错误)加了全局错误断点之后,依然停在main函数了??
    ios使用支付宝进行支付,注意事项& 集成支付宝钱包支付iOS SDK的方法与经验。
    Http Analyzer 数据抓包
    中文目录文件显示问题
    Python urllib和urllib2模块学习(三)
  • 原文地址:https://www.cnblogs.com/wmxl/p/5860936.html
Copyright © 2011-2022 走看看