QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#831757#9926. Flipping Pathsucup-team4479#WA 1ms3620kbC++237.0kb2024-12-25 16:41:122024-12-25 16:41:12

Judging History

你现在查看的是最新测评结果

  • [2024-12-25 16:41:12]
  • 评测
  • 测评结果:WA
  • 用时:1ms
  • 内存:3620kb
  • [2024-12-25 16:41:12]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
const int N = 205;
bool mp[N][N], vis[N][N], rev;
int n, m;
bool work(int goal) {
    vector <string> ans;
    for (int i = 1; i <= n; ++i)
        for (int j = 1; j <= m; ++j)
            vis[i][j] = mp[i][j];
    // cout << goal << ":" << endl;
    for (int d = m - 1; d >= 3 - n; --d) {
        int x = 1, y = x + d;
        if (y < 2 || y > m) {
            y = 2;
            x = y - d;
        }
        string s = "";
        vis[1][1] ^= 1;
        for (int i = 2; i <= x; ++i) {
            vis[i][1] ^= 1;
            s += 'D';
        }
        for (int j = 2; j < y; ++j) {
            vis[x][j] ^= 1;
            s += 'R';
        }
        // cout << x << " " << y << endl;
        while (x < n && y <= m) {
            if (vis[x][y] == goal) {
                s += "DR";
                vis[x + 1][y - 1] ^= 1;
                vis[x + 1][y] ^= 1;
            } else {
                s += "RD";
                vis[x][y] ^= 1;
                vis[x + 1][y] ^= 1;
            }
            x++, y++;
        }
        for (int i = x + 1; i <= n; ++i) {
            s += 'D';
            vis[i][y - 1] ^= 1;
        }
        for (int j = y; j <= m; ++j) {
            s += 'R';
            vis[n][j] ^= 1;
        }
        // cout << s << endl;
        ans.push_back(s);
    }
    // for (auto s : ans) cout << s << endl;
    // for (int i = 1; i <= n; ++i) {
    //     for (int j = 1; j <= m; ++j)
    //         cout << vis[i][j] << " ";
    //     cout << endl;
    // }
    for (int i = 1; i <= n; ++i)
        if (vis[i][1] != vis[1][1]) return false;
    for (int i = 1; i <= m; ++i)
        if (vis[n][i] != vis[1][1]) return false;
    string s = "";
    for (int i = 1; i < n; ++i) s += 'D';
    for (int i = 1; i < m; ++i) s += 'R';
    cout << goal << " " << vis[1][1] << endl;
    if (vis[1][1] != goal) ans.push_back(s);
    /*for (int d = m - 1; d >= 2; --d) {
        string s = "";
        for (int k = 1; k <= d - 1; ++k) {
            s += 'R';
            vis[1][k] ^= 1;
        }
        // cout << s << endl;
        vis[1][d] ^= 1;
        for (int i = 1; i < n; ++i) {
            int j = i + d;
            if (j > m) break;
            if (vis[i][j] == goal) {
                s += "DR";
                vis[i + 1][j - 1] ^= 1;
                vis[i + 1][j] ^= 1;
            } else {
                s += "RD";
                vis[i][j] ^= 1;
                vis[i + 1][j] ^= 1;
            }
        }
        for (int i = m - d + 1; i < n; ++i) {
            s += 'D';
            vis[i + 1][m] ^= 1;
        }
        for (int j = n + d - 1; j < m; ++j) {
            s += 'R';
            vis[n][j + 1] ^= 1;
        }
        // cout << s << endl;
        // for (int i = 1; i <= n; ++i) {
        //     for (int j = 1; j <= m; ++j)
        //         cout << vis[i][j] << " ";
        //     cout << endl;
        // }
        ans.push_back(s);
    }
    for (int d = n - 1; d >= 2; --d) {
        string s = "";
        for (int k = 1; k <= d - 1; ++k) {
            s += 'D';
            vis[k][1] ^= 1;
        }
        vis[d][1] ^= 1;
        for (int i = d + 1; i <= n; ++i) {
            int j = i - d;
            if (j < 1) continue;
            if (vis[i][j] == goal) {
                s += "RD";
                vis[i - 1][j + 1] ^= 1;
                vis[i][j + 1] ^= 1;
            } else {
                s += "DR";
                vis[i][j] ^= 1;
                vis[i][j + 1] ^= 1;
            }
        }
        for (int j = n - d + 1; j < m; ++j) {
            s += 'R';
            vis[n][j + 1] ^= 1;
        }
        // for (int j = m + d - 1; )
        // cout << s << endl;
        // for (int i = 1; i <= n; ++i) {
        //     for (int j = 1; j <= m; ++j)
        //         cout << vis[i][j] << " ";
        //     cout << endl;
        // }
        ans.push_back(s);
    }
    // for (auto s : ans) {
    //     cout << s << endl;
    // }
    // for (int i = 1; i <= n; ++i) {
    //     for (int j = 1; j <= m; ++j)
    //         cout << vis[i][j] << " ";
    //     cout << endl;
    // }
    for (int i = m; i >= n + 1; --i)
        if (vis[n][i] != vis[n][i - 1]) return false;
    int t = (vis[1][2] ^ goal) + (vis[2][1] ^ goal);
    for (int i = 2; i < n; ++i)
        if ((((vis[i][i + 1] ^ goal) + (vis[i + 1][i] ^ goal)) & 1) != (t & 1)) return false;
    for (int i = 1; i < n; ++i)
        if (((t & 1) ^ vis[i][i]) != goal) return false;
    // cout << t << endl;
    if (t) {
        vector <string> s(t, "");
        for (int i = 1; i < n; ++i) {
            if ((vis[i][i + 1] != goal) && (vis[i + 1][i] != goal)) {
                s[0] += "RD";
                s[1] += "DR";
            } else if ((vis[i][i + 1] == goal) && (vis[i + 1][i] == goal)) {
                s[0] += "RD";
                s[1] += "RD";
            } else s[0] += vis[i][i + 1] != goal ? "RD" : "DR";
        }
        for (int i = n; i < m; ++i)
            for (int j = 0; j < s.size(); ++j)
                s[j] += 'R';
        ans.insert(ans.end(), s.begin(), s.end());
    }*/
    cout << "YES" << endl;
    cout << ans.size() << endl;
    for (string s : ans) {
        int x = 1, y = 1;
        mp[x][y] ^= 1;
        for (int i = 0; i < s.length(); ++i) {
            if (rev) cout << ((s[i] == 'R') ? 'D' : 'R');
            else cout << s[i];
            if (s[i] == 'D') x++;
            else y++;
            mp[x][y] ^= 1;
        }
        cout << endl;
        assert(s.length() == n + m - 2);
    }
    for (int i = 1; i <= n; ++i)
        for (int j = 1; j <= m; ++j)
            assert(!(mp[i][j] ^ goal));
    return true;
}
void solve() {
    string s;
    cin >> n >> m;
    for (int i = 1; i <= n; ++i) {
        cin >> s;
        for (int j = 1; j <= m; ++j)
            mp[i][j] = s[j - 1] == 'B';
    }
    rev = 0;
    if (n > m) {
        rev = 1;
        for (int i = 1; i <= n; ++i)
            for (int j = 1; j < min(i, m + 1); ++j)
                swap(mp[i][j], mp[j][i]);
        swap(n, m);
    }
    if (n == 1) {
        for (int i = 2; i <= m; ++i)
            if (mp[1][i] != mp[1][i - 1]) {
                cout << "NO" << endl;
                return;
            }
        cout << "YES" << endl;
        cout << 0 << endl;
        return;
    }
    // for (int i = 1; i <= n; ++i) {
    //     for (int j = 1; j <= m; ++j)
    //         cout << mp[i][j] << " ";
    //     cout << endl;
    // }
    if (work(0) || work(1)) return;
    else {
        cout << "NO" << endl;
        return;
    }
}
int main() {
    cin.tie(nullptr) -> ios::sync_with_stdio(false);
    cout.tie(0);
    int T;
    cin >> T;
    while (T--) solve();
    return 0;
}
/*
8
3 3
WBB
BWB
BBW
1 5
WWWWW
2 2
BB
BB
4 1
W
B
B
W
4 3
WBW
WWW
WBB
BWW
6 4
WWWW
BBWB
BBWB
WWWB
WBBB
BWWW
6 3
WBW
WBB
BWW
BBW
BWW
WBW
6 2
WB
BB
BB
BB
BB
BW
*/

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 0
Wrong Answer
time: 1ms
memory: 3620kb

input:

4
3 3
WBB
BWB
BBW
1 5
WWWWW
2 2
BB
BB
4 1
W
B
B
W

output:

0 1
YES
4
RRDD
DRDR
DRDR
DDRR
YES
0
1 0
YES
2
DR
DR
NO

result:

wrong answer YES or NO expected in answer, but 0 found. (test case 1)