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

    Flip Game
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 26492   Accepted: 11422

    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


    题意:
    有一个4*4的正方形,每个格子上的棋子(即块状物)有正反两面,一面黑色,一面白色,当一个格子里的棋子被翻转时,其周围的四个棋子(如果存在的话)也被翻转,问至少翻转几个格子可以使4*4的正方形里的棋子变为全白或全黑?
    思路:
    一开始凭感觉吧,同个棋子被翻转2次的话好像作用和未翻转一样,那题目就成了一个棋子是否被翻转的问题了(要么被翻转1次,要么不被翻转)。
    那就想到枚举每个棋子的情况,最多枚举 2 ^ (4 * 4) = 2 ^ 16 次,应该可以过。
    然后又想到以前书上看过类似的一题,枚举第一排就能推出之后的几排的情况,然后翻书,继续想想,就想通了。确实当第一排被确定时可以推出下面几排的情况。而且只需要枚举 2 ^ 4 * 2次(乘以2是考虑到翻转成全白或者全黑两种情况)即可。





    /*************************************************************************
    	> File Name: POJ1753.c
    	> Author: BSlin
    	> Mail:  
    	> Created Time: 2013年10月30日 星期三 19时44分55秒
     ************************************************************************/
    
    #include <stdio.h>
    #include <string.h>
    #define INF 20
    
    int map1[5][5],map2[5][5];
    
    int min(int a, int b) {
        return a > b ? b : a;
    }
    
    int inmap(int x, int y) {
        if(x >= 0 && x < 4 && y >= 0 && y < 4) 
            return 1;
        return 0;
    }
    
    void change(int x, int y) {
        if(inmap(x,y)) {
            map2[x][y] = 1 - map2[x][y];
        }
        if(inmap(x-1,y)) {
            map2[x-1][y] = 1 - map2[x-1][y];
        }
        if(inmap(x,y+1)) {
            map2[x][y+1] = 1 - map2[x][y+1];
        }
        if(inmap(x+1,y)) {
            map2[x+1][y] = 1 - map2[x+1][y];
        }
        if(inmap(x,y-1)) {
            map2[x][y-1] = 1 - map2[x][y-1];
        }
    }
    
    int check(int s, int num) {
        int i, j, cnt, x, y;
        for(i=0; i<4; i++) {
            for(j=0; j<4; j++) {
                map2[i][j] = map1[i][j];
            }
        }
        cnt = 0;
        for(j=0; j<4; j++) {
            if(s & (1 << j)) {
                cnt ++;
                change(0,j);
            }
        }
        //if(s == 1) {
            //for(x=0; x<4; x++) {
                //for(y=0; y<4; y++) {
                    //printf("%d",map2[x][y]);
                //}
                //printf("
    ");
            //}
            //printf("
    ");
        //}
        for(i=0; i<3; i++) {
            for(j=0; j<4; j++) {
                if(map2[i][j] != num) {
                    cnt ++;
                    change(i+1,j);
        //if(s == 1) {
            //for(x=0; x<4; x++) {
                //for(y=0; y<4; y++) {
                    //printf("%d",map2[x][y]);
                //}
                //printf("
    ");
            //}
            //printf("
    ");
        //}
                }
            }
        }
        for(j=0; j<4; j++) {
            if(map2[3][j] != num) 
                return INF;
        }
        return cnt;
    }
    
    int main() {
        freopen("in.txt","r",stdin);
        int i,j,ans;
        char ch;
        for(i=0; i<4; i++) {
            for(j=0; j<4; j++) {
                scanf("%c",&ch);
                if(ch == 'b') {
                    map1[i][j] = 1;
                } else if(ch == 'w') {
                    map1[i][j] = 0;
                }
            }
            getchar();
        }
        ans = INF;
        for(i=0; i<16; i++) {
            ans = min(ans, check(i,0));
        }
        for(i=0; i<16; i++) {
            ans = min(ans, check(i,1));
        }
        if(ans == INF) {
            printf("Impossible
    ");
        } else {
            printf("%d
    ",ans);
        }
    }
    



  • 相关阅读:
    20155311《信息安全系统设计基础》课程总结
    20155311《信息安全系统设计基础》第14周学习总结
    20155311《信息安全系统设计基础》第十三周学习总结
    2017-2018-1 《信息安全系统设计基础》实验五 通信协议设计
    补交课下测试(ch12并发编程) 08.第八周
    2017-2018-1 20155311 实验四 外设驱动程序设计
    2017-2018-1 学号20155311 《信息安全系统设计基础》第11周学习总结
    PWD的编译及调试
    2017-2018-1 学号20155311 《信息安全系统设计基础》第9周学习总结
    2017-2018-1 20155311 实验三 实时系统
  • 原文地址:https://www.cnblogs.com/pangblog/p/3400235.html
Copyright © 2011-2022 走看看