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;
    }
  • 相关阅读:
    ASP.NET MVC URL重写与优化(进阶篇)-继承RouteBase玩转URL
    MVC Razor模板引擎 @RenderBody、@RenderPage、@RenderSection及Html.RenderPartial、Html.RenderAction
    二叉树的建立&&前中后遍历(递归实现)&&层次遍历
    实现一个简单的散列表(HashMap)
    单向链表的删除及插入操作(以头插入法建立单向链表)
    单向链表的建立(头插入法)
    单向链表的建立(尾部插入法)
    链式队列(单向列表实现)
    顺序队列(数组实现)
    链式栈(单向链表实现)
  • 原文地址:https://www.cnblogs.com/kls123/p/7358718.html
Copyright © 2011-2022 走看看