• POJ1753 Flip Game 高斯消元


    这题我们可以参考开关那题,只不过这里是求最少的操作次数,那么我们需要对变元进行枚举,算出所有的情况下,最少需要改变的次数。

    代码如下:

    #include <cstdlib>
    #include <cstring>
    #include <cstdio>
    #include <algorithm>
    #define TO(x, y) (x-1)*4+y
    using namespace std;
    
    char G[6][6];
    
    int dir[4][2] = {1, 0, -1, 0, 0, 1, 0, -1};
    
    inline bool judge(int x, int y)
    {
        if (x < 1 || x > 4 || y < 1 || y > 4) {
            return false;
        }   
        return true;
    }
    
    void swap(int &a, int &b) 
    {
        int t = a;
        a = b;
        b = t;
    }
    
    struct Matrix
    {
        int a[20][20];
        void init() {
            int xx, yy, k, pos;
            memset(a, 0, sizeof (a));
            for (int i = 1; i <= 4; ++i) {
                for (int j = 1; j <= 4; ++j) {
                    k = TO(i, j);
                    a[k][k] = 1;
                    for (int d = 0; d < 4; ++d) {
                        xx = i + dir[d][0], yy = j + dir[d][1];
                        if (judge(xx, yy)) {
                            pos = TO(xx, yy);
                            a[pos][k] = 1;
                        }
                    }
                }
            }
        }
        void rswap(int x, int y, int s) {
            for (int j = s; j <= 18; ++j) {
                swap(a[x][j], a[y][j]);
            }
        }
        void relax(int x, int y, int s) {
            for (int j = s; j <= 18; ++j) {
                a[y][j] ^= a[x][j];
            }
        }
    }M;
    
    void solve(int R)
    {
        int f1 = 0, f2 = 0, x1[20], x2[20], t1, t2, ans1 = 0x3fffffff, ans2 = 0x3fffffff;
        for (int i = R + 1; i <= 16; ++i) {
            if (M.a[i][17]) f1 = 1;
            if (M.a[i][18]) f2 = 1;
        }
        if (f1 && f2) {
            puts("Impossible");
            return;
        }
        for (int s = 0; s < 16; ++s) {
            t1 = t2 = 0;
            memset(x1, 0, sizeof (x1));
            memset(x2, 0, sizeof (x2));
            for (int i = 0; i < 4; ++i) {
                x1[R+i+1] = s & (1 << i) ? 1 : 0;
                x2[R+i+1] = x1[R+i+1];
                if (x1[R+i+1]) ++t1;
                if (x2[R+i+1]) ++t2;
            }
            for (int i = R; i >= 1; --i) {
                for (int j = i + 1; j <= 16; ++j) {
                    if (!f1) x1[i] ^= (x1[j] * M.a[i][j]);
                    if (!f2) x2[i] ^= (x2[j] * M.a[i][j]);
                }
                if (!f1) {
                    x1[i] ^= M.a[i][17];
                    if (x1[i]) ++t1;
                }
                if (!f2) {
                    x2[i] ^= M.a[i][18];
                    if (x2[i]) ++t2;
                }
            }
            if (!f1) ans1 = min(ans1, t1);
            if (!f2) ans2 = min(ans2, t2);
        }
        printf("%d\n", min(ans1, ans2));
    }
    
    void Gauss()
    {
        int i = 1, k;
        for (int j = 1; j <= 16; ++j) { // 枚举上三角矩阵的对角线
            for (k = i; k <= 16; ++k) {
                if (M.a[k][j])  break;
            }
            if (k > 16) continue;
            if (k != i) {  // 如果该行就是第首行,那么我们直接进行消元,否则交换行,使得第一行为单位1
                M.rswap(k, i, j);
            }
            for (k = i + 1; k <= 16; ++k) {  // 从下一行开始进行消元
                if (M.a[k][j]) {
                    M.relax(i, k, j);
                }
            } 
            ++i;
        }
        solve(i - 1);
    }
    
    int main()
    {
        M.init();
        for (int i = 1; i <= 4; ++i) {
            scanf("%s", G[i] + 1);
        }
        for (int i = 1; i <= 4; ++i) {
            for (int j = 1; j <= 4; ++j) {
                M.a[TO(i, j)][17] = G[i][j] == 'w';
                M.a[TO(i, j)][18] = G[i][j] == 'b';
            }
        }
        Gauss();
        return 0;
    }
  • 相关阅读:
    运维岗春招--part2
    python 题库|刷题
    leetcode刷题
    运维面经汇总
    python自动化运维阅读笔记
    Python编程汇总
    old_boy 运维学习-第一期
    团队博客作业-Week3
    个人对final发布产品的排名
    各组对final发布产品的排名
  • 原文地址:https://www.cnblogs.com/Lyush/p/2610759.html
Copyright © 2020-2023  润新知