• POJ1753 搜索


    Flip Game

    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
    

     

     
     
    思路:用数字来记录棋盘的状态,1代表黑子,0代表白子,可用16位的二进制数表示棋盘状态。则对每个位置的翻子操作结果可以用翻子周围数字之和去异或原数字来表示。
    eg:原数字为32如下:  翻子(3,1)即32^(2^3+2^6+2^7+2^11)           结果:
    wwww                                        wwww
    wwww                                        bwww wwbw                                        bbbw wwww                                        bwww
    目标数字为0或65535.
    #include "cstdio"
    #include "algorithm"
    #include "queue"
    #include "cmath"
    #include "cstring"
    
    #define  inf 0x3f3f3f
    using namespace std;
    int g[16];
    char s[4][4];
    bool use[65536];
    typedef  struct {
        int st,s;
    }N;
    int bfs(int s){
        queue<N>q;
        N no,ne;
        memset(use, false, sizeof(use));
        no.st=s,no.s=0;
        q.push(no);
        use[s]=true;
        while (q.size()){
            no=q.front();
            q.pop();
            if(no.st==0||no.st==65535){
                return no.s;
            }
            for(int i=0;i<16;i++){
                ne.st=no.st^g[i];
                ne.s=no.s+1;
                if(use[ne.st]){
                    continue;
                }
                if(ne.st==0||ne.st==65535){
                    return  ne.s;
                }
                use[ne.st]= true;
                q.push(ne);
            }
        }
        return -1;
    }
    int main()
    {
        memset(g,0, sizeof(g));
        for(int i=0;i<16;i++){//存储16个翻子操作
            int n=15-i;
            g[i]=pow(2,n);
            if(n+1<=n/4*4+3){
                g[i]+=pow(2,n+1);
            }
            if(n-1>=n/4*4){
                g[i]+=pow(2,n-1);
            }
            if(n+4<=15){
                g[i]+=pow(2,n+4);
            }
            if(n-4>=0){
                g[i]+=pow(2,n-4);
            }
        }
        while (scanf("%s",s[0])!=EOF){
            for(int i=1;i<4;i++){
                scanf("%s",s[i]);
            }
            int st=0;
            for(int i=0;i<4;i++){//将初始状态转为数字
                for(int j=0;j<4;j++){
                    st<<=1;
                    if(s[i][j]=='b'){
                        st++;
                    }
                }
            }
            int x=bfs(st);
            if(x==-1){
                printf("Impossible
    ");
            }else{
                printf("%d
    ",x);
            }
        }
        return 0;
    }
  • 相关阅读:
    数据可视化之DAX篇(二十五)PowerBI常用的度量值:累计至今
    数据可视化之DAX篇(二十四)Power BI应用技巧:在总计行实现条件格式
    Type-generic math (Numerics) – C 中文开发手册
    Bootstrap4 卡片
    Linux let 命令
    PHP ftp_fput() 函数
    如何从SQL Server DateTime数据类型返回日期
    ID选择器 | ID selectors (Selectors) – CSS 中文开发手册
    git write-tree (Plumbing Commands) – Git 中文开发手册
    C 库函数 – strtod()
  • 原文地址:https://www.cnblogs.com/mj-liylho/p/6354281.html
Copyright © 2020-2023  润新知