답안 #246098

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
246098 2020-07-08T07:26:38 Z NONAME Clickbait (COCI18_clickbait) C++14
0 / 120
30 ms 12408 KB
#include <bits/stdc++.h>
#define dbg(x) cerr << #x << " = " << x << "\n"
#define fast_io ios_base::sync_with_stdio(0); cin.tie(0); cout.tie()
using namespace std;
using ll = long long;
using ld = long double;

const int N = 2e3;

int n, m, mk[N][N], mxx[N], mxy[N], mnx[N], mny[N];
bool used[N][N];
char c[N][N];
vector <int> order;

int f(int x, int y) {
    string s = "";
    while (y < m && (c[x][y] >= '0' && c[x][y] <= '9'))
        s += c[x][y++];

    return atoi(s.c_str());
}

void fill_all(int num, int sx, int sy) {
    queue <pair <int, int> > q;

    q.push(make_pair(sx, sy));
    used[sx][sy] = 1;

    mxx[num] = mxy[num] = 0;
    mnx[num] = mny[num] = N;

    while (!q.empty()) {
        auto p = q.front();
        q.pop();

        mk[p.first][p.second] = num;
        mxx[num] = max(mxx[num], p.first);
        mnx[num] = min(mnx[num], p.first);
        mxy[num] = max(mxy[num], p.second);
        mny[num] = min(mny[num], p.second);

        if (c[p.first][p.second] == '-' || c[p.first][p.second] == '|')
            continue;

        int steps[4][2] = {{1, 0}, {0, 1}, {0, -1}, {-1, 0}};

        for (int i = 0; i < 4; ++i) {
            int cx = p.first + steps[i][0];
            int cy = p.second + steps[i][1];

            if (cx >= 0 && cx < n && cy >= 0 && cy < m && !used[cx][cy]) {
                used[cx][cy] = 1;
                q.push(make_pair(cx, cy));
            }
        }
    }
}

int nxt(int x, int y) {
    int num = 0;
    queue <pair <int, int> > q;
    q.push(make_pair(x, y));
    used[x][y] = 1;

    while (!q.empty()) {
        x = q.front().first;
        y = q.front().second;
        q.pop();
        if (mk[x][y] != -1) {
            num = mk[x][y];
            break;
        }

        if (c[x][y] == '-') {
            if (y + 1 < m && c[x][y + 1] != '.' && !used[x][y + 1]) {
                used[x][y + 1] = 1;
                q.push(make_pair(x, y + 1));
            }

            if (y - 1 >= 0 && c[x][y - 1] != '.' && !used[x][y - 1]) {
                used[x][y - 1] = 1;
                q.push(make_pair(x, y - 1));
            }
        }

        if (x + 1 < n && c[x][y] == '|' && !used[x + 1][y]) {
            used[x + 1][y] = 1;
            q.push(make_pair(x + 1, y));
        }

        if (c[x][y] == '+') {
            if (x + 1 < n && c[x + 1][y] != '.' && !used[x + 1][y]) {
                used[x + 1][y] = 1;
                q.push(make_pair(x + 1, y));
            }

            if (y + 1 < m && c[x][y + 1] != '.' && !used[x][y + 1]) {
                used[x][y + 1] = 1;
                q.push(make_pair(x, y + 1));
            }

            if (y - 1 >= 0 && c[x][y - 1] != '.' && !used[x][y - 1]) {
                used[x][y - 1] = 1;
                q.push(make_pair(x, y - 1));
            }
        }
    }

    return num;
}

void dfs(int v) {
    while (mxx[v] > mnx[v]) {
        if (mny[v] - 1 >= 0 && c[mxx[v]][mny[v] - 1] == '-')
            dfs(nxt(mxx[v], mny[v] - 1));

        if (mxy[v] + 1 < m && c[mxx[v]][mxy[v] + 1] == '-')
            dfs(nxt(mxx[v], mxy[v] + 1));

        --mxx[v];
    }

    order.push_back(v);
}

int main() {
    fast_io;

    cin >> n >> m;

    for (int i = 0; i < n; ++i)
    for (int j = 0; j < m; ++j)
        cin >> c[i][j], mk[i][j] = -1;

    for (int i = 0; i < n; ++i)
    for (int j = 0; j < m; ++j) {
        if (used[i][j])
            continue;

        if (!(c[i][j] >= '0' && c[i][j] <= '9'))
            continue;

        int num = f(i, j);

        fill_all(num, i, j);
    }

    dfs(1);

    for (int i : order)
        cout << i << "\n";
}
# 결과 실행 시간 메모리 Grader output
1 Incorrect 5 ms 1152 KB Output isn't correct
2 Incorrect 5 ms 768 KB Output isn't correct
3 Incorrect 5 ms 896 KB Output isn't correct
4 Incorrect 5 ms 512 KB Output isn't correct
5 Incorrect 5 ms 1152 KB Output isn't correct
6 Incorrect 5 ms 1024 KB Output isn't correct
7 Incorrect 5 ms 1024 KB Output isn't correct
8 Incorrect 7 ms 2944 KB Output isn't correct
9 Incorrect 8 ms 7680 KB Output isn't correct
10 Incorrect 30 ms 12408 KB Output isn't correct