zoukankan      html  css  js  c++  java
  • poj1753 【枚举】

    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
    思路:dfs
    实现代码:
    #include<iostream>
    #include<cstdio>
    #include<string>
    #include<cstring>
    #include<cmath>
    #include<algorithm>
    #include<map>
    #include<queue>
    #include<stack>
    #include<set>
    #include<list>
    using namespace std;
    #define ll long long
    #define sd(x) scanf("%d",&x)
    #define sdd(x,y) scanf("%d%d",&x,&y)
    #define sddd(x,y,z) scanf("%d%d%d",&x,&y,&z)
    #define sf(x) scanf("%s",x)
    #define ff(i,x,y) for(int i = x;i <= y;i ++)
    #define fj(i,x,y) for(int i = x;i >= y;i --)
    #define mem(s,x) memset(s,x,sizeof(s));
    #define pr(x) printf("%d",x);
    const int Mod = 1e9+7;
    const int inf = 1e9;
    const int Max = 1e5+10;
    void exgcd(ll a,ll b,ll& d,ll& x,ll& y){if(!b){d=a;x=1;y=0;}else{exgcd(b,a%b,d,y,x);y-=x*(a/b);}}
    ll inv(ll a,ll n){ll d, x, y;exgcd(a,n,d,x,y);return (x+n)%n;}
    int gcd(int a,int b)  {  return (b>0)?gcd(b,a%b):a;  }
    int lcm(int a, int b)  {  return a*b/gcd(a, b);   }
    //int mod(int x,int y) {return x-x/y*y;}
    char s[10];
    int mp[10][10];
    int ans = inf;
    int check()
    {
        int x = mp[0][0];
        for(int i = 0;i < 4;i ++){
            for(int j = 0;j < 4;j ++){
                if(mp[i][j]!=x)
                    return 0;
            }
        }
        return 1;
    }
    void fun(int x,int y)
    {
        mp[x][y] = !mp[x][y];
        if(x - 1 >= 0) mp[x-1][y] = !mp[x-1][y];
        if(x + 1 <= 3) mp[x+1][y] = !mp[x+1][y];
        if(y - 1 >= 0) mp[x][y-1] = !mp[x][y-1];
        if(y + 1 <= 3) mp[x][y+1] = !mp[x][y+1];
    }
    int dfs(int x,int y,int t)
    {
        if(check()){
            ans = min(ans,t);
            return 0;
        }
        if(x>=4||y>=4)
            return 0;
        int fx = (x+1)%4;
        int fy = y+(x+1)/4;
        dfs(fx,fy,t);
        //cout<<fx<<" "<<fy<<" "<<t<<endl;
        fun(x,y);
    
        dfs(fx,fy,t+1);
        //cout<<fx<<" "<<fy<<" "<<t+1<<endl;
        fun(x,y);
        return 0;
    }
    int main()
    {
        for(int i=0;i<4;i++){
            cin>>s;
            for(int j=0;j<4;j++){
                if(s[j]=='b')
                    mp[i][j] = 0;
                else
                    mp[i][j] = 1;
            }
        }
        dfs(0,0,0);
        if(ans==inf)
            cout<<"Impossible"<<endl;
        else
            cout<<ans<<endl;
        return 0;
    }
  • 相关阅读:
    numpy中linspace用法 (等差数列创建函数)
    Ubuntu环境下 matplotlib 图例中文乱码
    转载: 广义逆矩阵
    matplotlib.pyplot中add_subplot方法参数111的含义
    转载:(论文) 二次指数平滑法中确定初始值的简便方法
    图像处理之 opencv 学习---opencv 中的常用算法
    图像处理之 opencv 学习---矩阵的操作
    编译异常之static和extern---more than one storage class specified
    格式转换至yuv422转 yuv420
    阶段3 3.SpringMVC·_02.参数绑定及自定义类型转换_6 自定义类型转换器代码编写
  • 原文地址:https://www.cnblogs.com/kls123/p/7358718.html
Copyright © 2011-2022 走看看