• poj 1753【枚举+dfs(位向量法)】


    Flip Game
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 46519   Accepted: 19915

    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

    题意:给你一个4*4的棋盘,黑棋是‘b’,白棋是‘w’,问最少翻转多少次,可以使棋盘颜色统一。棋子有黑白两面可以翻转,翻转一个棋子,周围的棋子都要翻转。
    思路:《算法竞赛入门经典》上提到枚举子集的方法之一---位向量法。这种方法就是把棋盘所有可能的翻转子集情况枚举一遍,如果在翻转过程中,棋盘颜色得以统一,就将现在的步数与最小值进行比较,如果小于最小值,就更新它。
    #include<stdio.h>
    #define inf 0x3f3f3f3f
    int map[5][5],min,n;
    
    int Check(int map[][5])//检查棋盘颜色是否统一 
    {
        int i,j;
        for(i = 0; i < 4; i ++)
            for(j = 0; j < 4; j ++)
                if(map[0][0] != map[i][j])
                    return 0;
        return 1;
    }
    
    void Reverse(int num)//翻转棋子 
    {
        int x,y;
        x = num/4;
        y = num%4;
        map[x][y]=!map[x][y];
        if(x>0)    map[x-1][y]=!map[x-1][y];
        if(x<3)    map[x+1][y]=!map[x+1][y];
        if(y>0)    map[x][y-1]=!map[x][y-1];
        if(y<3)    map[x][y+1]=!map[x][y+1];
        return;
    }
    
    void dfs(int num,int step)
    {
        if(Check(map))
        {
            if(step < min)
                min = step;
            return;
        }
        if(num >= n)
            return;
        
        dfs(num+1,step); 
        Reverse(num);//翻转棋子 
        
        dfs(num+1,step+1);//step记录已经翻转过的棋子 
        Reverse(num);//回溯 
    }
    
    int main()
    {
        int i,j;
        char ch;
        for(i = 0; i < 4; i ++)
        {
            for(j = 0; j < 4; j ++)
            {
                scanf("%c",&ch);
                if(ch == 'b')//构建棋盘 
                    map[i][j] = 0;
                else
                    map[i][j] = 1;
            }
            getchar();
        }
        n = 16;
        min = inf;
        dfs(0,0);
        if(min == inf)
            printf("Impossible
    ");
        else
            printf("%d
    ",min);
        return 0;
    }
  • 相关阅读:
    好兴奋,我的博客终于上线了
    学技术,该怎么看书
    PHP程序员求职经验总结
    Jquery中extend使用技巧
    JavaScript(JS)实现省市联动选择下拉列表
    银行家算法之JavaScript实现
    JS中的两种刷新方法以及区别和适用范围
    一段上传图片预览JS脚本,Input file图片预览的实现
    本博客模板说明
    深圳,想说爱你不容易!
  • 原文地址:https://www.cnblogs.com/hellocheng/p/7452056.html
Copyright © 2020-2023  润新知