CCF 201512-3 画图_BFS

CCF 201512-3 画图 传送门

模拟, 还带了个BFS, 比较简单. 但是第一次提交只得了90分. 因为在判断交点的时候, 考虑了 -遇到||遇到- 的情况却没考虑|-遇到+的情况, 真是蠢哉, 蠢哉.

#include <algorithm>
#include <iostream>
#include <vector>
#include <queue>
using namespace std;

int move[4][2] = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
int n, m, q, cmd;

void bfs(char map[100][100], int x, int y, char ch)
{
    int vis[100][100] = {};
    queue<int> Qx, Qy;
    Qx.push(x), Qy.push(y);
    vis[x][y] = true;
    map[x][y] = ch;
    while (!Qx.empty()) {
        int u = Qx.front(), v = Qy.front();
        Qx.pop(), Qy.pop();
        for (int i = 0; i < 4; ++i) {
            int uu = u + move[i][0], vv = v + move[i][1];
            if (uu < 0 || uu >= n || vv < 0 || vv >= m) continue;
            if (map[uu][vv] == '-' || map[uu][vv] == '+' || map[uu][vv] == '|') continue;
            if (vis[uu][vv]) continue;
            vis[uu][vv] = true;
            Qx.push(uu), Qy.push(vv);
            map[uu][vv] = ch;
        }
    }
}

int main()
{
    char map[100][100];
    for (int i = 0; i < 100; ++i) {
        for (int j = 0; j < 100; ++j) {
            map[i][j] = '.';
        }
    }
    cin >> n >> m >> q;
    for (int qu = 0; qu < q; ++qu) {
        cin >> cmd;
        if (cmd == 1) {
            int x, y;
            char c;
            cin >> x >> y >> c;
            bfs(map, x, y, c);
        } else if (cmd == 0) {
            int x1, y1, x2, y2;
            cin >> x1 >> y1 >> x2 >> y2;
            if (y1 == y2) { // 横线, 用'-'表示
                for (int i = min(x1, x2); i <= max(x1, x2); ++i) {
                    if (map[i][y1] == '|') map[i][y1] = '+';
                    else if (map[i][y1] == '+') continue; //靠, 漏掉了这个,10分.
                    else map[i][y1] = '-';
                }
            } else { // 竖线, 用'|'表示
                for (int i = min(y1, y2); i <= max(y1, y2); ++i) {
                    if (map[x1][i] == '-') map[x1][i] = '+';
                    else if (map[x1][i] == '+') continue;
                    else map[x1][i] = '|';
                }
            }
        }
    }
    for (int i = m - 1; i >= 0; --i) {
        for (int j = 0; j < n; ++j) {
            cout << map[j][i];
        }
        cout << endl;
    }
}

猜你喜欢

转载自blog.csdn.net/wjh2622075127/article/details/81540046