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

    The game “The Pilots Brothers: following the stripy elephant” has a quest where a player needs to open a refrigerator.

    There are 16 handles on the refrigerator door. Every handle can be in one of two states: open or closed. The refrigerator is open only when all handles are open. The handles are represented as a matrix 4х4. You can change the state of a handle in any location [i, j] (1 ≤ i, j ≤ 4). However, this also changes states of all handles in row i and all handles in column j.

    The task is to determine the minimum number of handle switching necessary to open the refrigerator.

    Input

    The input contains four lines. Each of the four lines contains four characters describing the initial state of appropriate handles. A symbol “+” means that the handle is in closed state, whereas the symbol “−” means “open”. At least one of the handles is initially closed.

    Output

    The first line of the input contains N – the minimum number of switching. The rest N lines describe switching sequence. Each of the lines contains a row number and a column number of the matrix separated by one or more spaces. If there are several solutions, you may give any one of them.

    Sample Input

    -+--
    ----
    ----
    -+--

    Sample Output

    6
    1 1
    1 3
    1 4
    4 1
    4 3
    4 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,i,j;
    int rankx[100],ranky[100],fx[100],fy[100];
    int check()
    {
        for(i=0;i<4;i++){
            for(j=0;j<4;j++){
                if(mp[i][j]!=1)
                    return 0;
            }
        }
        return 1;
    }
    
    void fan(int x,int y){
        for(i=0;i<4;i++){
            mp[x][i] = !mp[x][i];
            mp[i][y] = !mp[i][y];
        }
        mp[x][y] = !mp[x][y];
    }
    
    int dfs(int x,int y,int t)
    {
        if(check()){
            if(ans>t){
                ans = t;
                for(i=0;i<t;i++){
                    rankx[i] = fx[i];
                    ranky[i] = fy[i];
                }
            }
            return 0;
        }
        if(x>=4||y>=4)
            return 0;
        int nx = (x+1)%4;
        int ny = y+(x+1)/4;
        dfs(nx,ny,t);
        fan(x,y);
    
        fx[t] = x+1;
        fy[t] = y+1;
    
        dfs(nx,ny,t+1);
        fan(x,y);
    
        return 0;
    }
    int main()
    {
        for(i=0;i<4;i++){
            cin>>s;
            for(j=0;j<4;j++){
                if(s[j]=='+')
                    mp[i][j] = 0;
                else
                    mp[i][j] = 1;
            }
        }
        dfs(0,0,0);
        cout<<ans<<endl;
        for(i=0;i<ans;i++){
            cout<<rankx[i]<<" "<<ranky[i]<<endl;
        }
        return 0;
    }
  • 相关阅读:
    利用jmeter进行数据库测试
    oracle创建/删除表空间、创建/删除用户并赋予权限
    在linux环境下安装JDK并配置环境变量
    本地与在线图片转Base64及图片预览
    html标签页图标
    Eclipse启动时卡死解决方法
    Java创建目录 mkdir与mkdirs的区别
    Java 获取距离最近一段时间的时间点
    data URI
    JavaScript input file上传前获取文件名、文件类型、文件大小等信息
  • 原文地址:https://www.cnblogs.com/kls123/p/7358733.html
Copyright © 2011-2022 走看看