QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#853099#9926. Flipping Pathsruoye123456WA 0ms3664kbC++204.2kb2025-01-11 15:44:092025-01-11 15:44:10

Judging History

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

  • [2025-01-11 15:44:10]
  • 评测
  • 测评结果:WA
  • 用时:0ms
  • 内存:3664kb
  • [2025-01-11 15:44:09]
  • 提交

answer

#include <iostream>
#include <vector>
#include <string>
using namespace std;

const int MAX_SIZE = 205;

class GridSolver {
private:
    int n, m;  // 网格大小
    bool grid[MAX_SIZE][MAX_SIZE];  // 当前网格状态
    bool original[MAX_SIZE][MAX_SIZE];  // 原始网格状态
    int boundary[MAX_SIZE];  // 每列的处理边界
    vector<string> paths;  // 存储路径
    
    // 更新每列的处理边界,保持单调性
    void updateBoundaries() {
        for (int col = 1; col < m; col++) {
            while (grid[boundary[col]][col] && boundary[col] > boundary[col-1]) {
                boundary[col]--;
            }
        }
    }
    
    // 检查网格是否全部为相同颜色
    bool isUniform() const {
        bool target = grid[1][1];
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= m; j++) {
                if (grid[i][j] != target) return false;
            }
        }
        return true;
    }
    
    // 构造从(1,1)到(n,m)的路径并翻转格子
    void constructPath() {
        string current_path;
        bool firstCell = grid[1][1];
        grid[1][1] = !firstCell;
        
        // 按列处理
        for (int col = 1, pos = 0; col <= m; col++) {
            // 向下移动
            for (int row = boundary[col-1] + 1; row <= boundary[col]; row++) {
                grid[row][col] = !grid[row][col];
                current_path += 'D';
            }
            if (col < m) {
                current_path += 'R';
                grid[boundary[col]][col+1] = !grid[boundary[col]][col+1];
            }
        }
        paths.push_back(current_path);
    }
    
    // 尝试构造解
    bool solve() {
        boundary[0] = 1;
        paths.clear();
        
        // 初始化边界
        for (int i = 1; i <= m; i++) {
            boundary[i] = n;
        }
        
        // 第一阶段:按列处理
        do {
            updateBoundaries();
            constructPath();
            if (boundary[m-1] == 1) break;
        } while (paths.size() < 400);
        
        if (isUniform()) return true;
        
        // 第二阶段:处理最后一行和列
        string final_path;
        grid[1][1] = !grid[1][1];
        
        // 向右移动处理第一行
        for (int j = 2; j <= m; j++) {
            final_path += 'R';
            grid[1][j] = !grid[1][j];
        }
        
        // 向下移动处理最后一列
        for (int i = 2; i <= n; i++) {
            final_path += 'D';
            grid[i][m] = !grid[i][m];
        }
        
        paths.push_back(final_path);
        return isUniform();
    }

public:
    bool solvePuzzle(int rows, int cols) {
        n = rows;
        m = cols;
        
        // 如果已经是统一颜色
        if (isUniform()) {
            cout << "YES\n0\n";
            return true;
        }
        
        // 尝试将所有格子变成1
        if (solve()) {
            printSolution();
            return true;
        }
        
        // 尝试将所有格子变成0
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= m; j++) {
                grid[i][j] = !original[i][j];
            }
        }
        
        if (solve()) {
            printSolution();
            return true;
        }
        
        cout << "NO\n";
        return false;
    }
    
    // 读取输入
    void readGrid() {
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= m; j++) {
                char c;
                cin >> c;
                grid[i][j] = original[i][j] = (c == 'B');
            }
        }
    }
    
    // 输出解
    void printSolution() {
        cout << "YES\n" << paths.size() << '\n';
        for (const string& path : paths) {
            cout << path << '\n';
        }
    }
};

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    
    int testCases;
    cin >> testCases;
    
    while (testCases--) {
        int n, m;
        cin >> n >> m;
        
        GridSolver solver;
        solver.readGrid();
        solver.solvePuzzle(n, m);
    }
    
    return 0;
}

詳細信息

Test #1:

score: 0
Wrong Answer
time: 0ms
memory: 3664kb

input:

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

output:

YES
0
YES
0
YES
0
YES
0

result:

wrong answer cell (1,2) contains different color with (1,1) after all ops. (test case 1)