美文网首页
20190908_poj3279

20190908_poj3279

作者: zhaohaoying | 来源:发表于2017-09-09 10:18 被阅读0次
    #include <iostream>
    #include <limits.h>
    #include <algorithm>
    #include <stdlib.h>
    #include <cstring>
    #include <queue>
    using namespace std;
    
    int a[17][17];
    int b[17][17];
    int c[17][17];
    int ans[17][17];
    
    void change(int i, int j) {
        if(b[i][j] == 1)
            b[i][j] = 0;
        else b[i][j] = 1;
    }
    
    void manage(int i, int j) {
        change(i,j);
        change(i-1,j);
        change(i,j-1);
        change(i+1,j);
        change(i,j+1);
        c[i][j] = 1;
    }
    
    int main() {
        int m,n;
        cin >> m;
        cin >> n;
        int max = 1;
        for(int i = 1; i <= m; ++i)
            for(int j = 1; j <= n; ++j)
                scanf("%d", &a[i][j]);
        for(int i = 0; i < n; ++i)
            max *= 2;
        int t = 300;
        for(int i = 0; i < max; ++i) {
            memset(c, 0, sizeof(c));
            int count = 0;
            for(int i = 1; i <= m; ++i)
                for(int j = 1; j <= n; ++j)
                    b[i][j] = a[i][j];
            int cnt = 1;
            int tt = i;
            while(tt >= 1) {
                if((tt & 1) == 1) {
                    manage(1,cnt);
                    count++;
                }
                tt = tt >> 1;
            }
            int flag = 0;
            for(int i = 2; i <= m; ++i)
                for(int j = 1; j <= n; ++j) {
                    if(b[i-1][j] == 1) {
                        manage(i,j);
                        count++;
                    }
                }
            for(int j = 1; j <= n; ++j)
                if(b[m][j] == 1)
                    flag = 1;
            if(t > count && flag == 0) {
                t = count;
                for(int i = 1; i <= m; ++i)
                    for(int j = 1; j <= n; ++j)
                        ans[i][j] = c[i][j];
            }
        }
        if(t == 300)
            cout << "IMPOSSIBLE" << endl;
        else for(int i = 1; i <= m; ++i) {
            for(int j = 1; j <= n; ++j) {
                cout << ans[i][j] << " ";
            }
            cout << endl;
        }
        return 0;
    }
    

    相关文章

      网友评论

          本文标题:20190908_poj3279

          本文链接:https://www.haomeiwen.com/subject/fbyojxtx.html