zoukankan      html  css  js  c++  java
  • POJ3276——暴力——Fliptile

    http://poj.org/problem?id=3279

    /*
    枚举第一行所有的状态,接下来的行都已经确定了,字典序最小输出
     */
    /************************************************
    * Author        :Powatr
    * Created Time  :2015-8-9 13:18:38
    * File Name     :D.cpp
     ************************************************/
    
    #include <cstdio>
    #include <algorithm>
    #include <iostream>
    #include <sstream>
    #include <cstring>
    #include <cmath>
    #include <string>
    #include <vector>
    #include <queue>
    #include <deque>
    #include <stack>
    #include <list>
    #include <map>
    #include <set>
    #include <bitset>
    #include <cstdlib>
    #include <ctime>
    using namespace std;
    
    #define lson l, mid, rt << 1
    #define rson mid + 1, r, rt << 1 | 1
    typedef long long ll;
    const int MAXN = 1e5 + 10;
    const int INF = 0x3f3f3f3f;
    const int MOD = 1e9 + 7;
    
    int n, m;
    char s[400];
    char ss[400];
    int  maze[20][20];
    int mp[20][20];
    int path[20][20];
    int f_path[20][20];
    
    int solve(int cnt)
    {
        int  ans = 0;
        for(int i = 1; i <= n; i++)
            for(int j = 1; j <= m; j++)
                mp[i][j] = maze[i][j];
        for(int i = 1; i <= m; i++){
            if(cnt&(1<<(i-1))){
                ans++;
                path[1][i] ^= 1;
                mp[1][i] ^= 1;
                mp[1][i-1] ^= 1;
                mp[1][i+1] ^= 1;
                mp[2][i] ^= 1;
            }
        } 
        for(int i = 2; i <= n;i++){
            for(int j = 1; j <= m; j++){
                if(mp[i-1][j] == 1){
                    ans++;
                    path[i][j] ^= 1;
                    mp[i-1][j] ^= 1;
                    mp[i][j] ^= 1;
                    mp[i][j-1] ^= 1;
                    mp[i][j+1] ^= 1;
                    mp[i+1][j] ^= 1;
                }
            }
        }
           for(int j = 1; j <= m; j++)
               if(mp[n][j])
                   return -1;
       return ans;
    }
    
    int main(){
        while(~scanf("%d%d", &n, &m)){
            for(int i = 1; i <= n; i++){
                for(int j = 1; j <= m; j++)
                    scanf("%d", &maze[i][j]);
                }
            memset(f_path, 0 , sizeof(f_path));
            for(int i = 1; i <= n*m ; i++)
                ss[i] = '1';
            int min1 = n*m + 1;
            for(int i = 0; i <= (1<<n) - 1; i++){
                int res = 0;
                memset(path, 0, sizeof(path));
                memset(mp, 0, sizeof(mp));
                int temp = solve(i);
                if(temp == -1) continue;
                if(temp <= min1) {
                    min1 = temp;
                    for(int i = 1; i <= n; i++){
                        for(int j = 1; j <= m; j++){
                            s[++res] = (char)(path[i][j] + '0');
                        }
                    }
                    if(strcmp(s+1, ss+1) < 0){
                        strcpy(ss+1, s+1);
                    }
                }
            }
            if(min1 == n*m + 1) 
                printf("IMPOSSIBLE
    ");
            else {
                for(int i = 1; i <= n*m; i++){
                    printf("%c%c", ss[i], i % m == 0 ? '
    ' : ' ');
                }
            }
        }
        return 0;
    }
    

      

  • 相关阅读:
    JAVA内存模型(Java Memory Model ,JMM)
    多线程(一)多线程实现、线程状态、线程调度、线程同步、线程数据传递
    Java中如何实现代理机制(JDK动态代理和cglib动态代理)
    HashMap与HashTable
    SSH远程会话管理工具
    nginx 无法启动:bind() to 0.0.0.0:443 failed
    nginx 提示the "ssl" directive is deprecated, use the "listen ... ssl" directive instead
    php Allowed memory size of 134217728 bytes exhausted
    nginx 部署php项目 404
    RabbitMQ+PHP教程
  • 原文地址:https://www.cnblogs.com/zero-begin/p/4715224.html
Copyright © 2011-2022 走看看