1 条题解

  • 2
    @ 2026-9-7 17:06:03

    此题与这道题十分相似,只需将输入的灯的状态取反,就可以转换成那道题,只需稍加改动

    ACcode

    #include<bits/stdc++.h>
    using namespace std;
    // #define int long long
    int m, n, anscnt = 1e9;
    int A[20][20], a[20][20];
    void change(int x, int y)
    {
        a[x][y] ^= 1;
        a[x-1][y] ^= 1;
        a[x+1][y] ^= 1;
        a[x][y-1] ^= 1;
        a[x][y+1] ^= 1;
    }
    signed main()
    {
      
        cin >> m >> n;
        for(int i = 1;i <= m;i++)
        {
            for(int j = 1;j <= n;j++)
            {
                cin >> A[i][j];
                // cout << A[i][j] << ' ';
                A[i][j] ^= 1;            
            }
            // cout << endl;
        }
        int anss[20][20] = {0};
        for(int l = 0;l < (1 << m);l++)
        {
            int ans[20][20] = {0};
            memcpy(a, A, sizeof(A));
            int cnt = 0;
            bool bj = 1;
            for(int i = 1;i <= n;i++)
            {
                if(l & (1 << (i - 1)))
                {
                    change(1, i);
                    cnt++;
                    ans[1][i]++;
                }
            }
            for(int i = 2;i <= m;i++)
            {
                for(int j = 1;j <= n;j++)
                {
                    if(!a[i-1][j])
                    {
                        change(i, j);
                        cnt++;
                        ans[i][j]++;
                    }
                }
            }
            for(int i = 1;i <= n;i++)
            {
                bj &= a[m][i];
            }
            if(bj&&cnt < anscnt)
            {
                memcpy(anss, ans, sizeof(ans));
                anscnt = cnt;
            }
        }
        if(anscnt == 1e9)
        {
            cout << "IMPOSSIBLE";
            return 0;
        }
        for(int i = 1;i <= m;i++)
        {
            for(int j = 1;j <= n;j++)
            {
                cout << anss[i][j] << ' ';
            }
            cout << endl;
        }
        return 0;
    }
    

    信息

    ID
    820
    时间
    1000ms
    内存
    256MiB
    难度
    10
    标签
    (无)
    递交数
    5
    已通过
    1
    上传者