結果

問題 No.2946 Puyo
ユーザー mataneko
提出日時 2024-10-25 21:52:48
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 126 ms / 2,000 ms
コード長 2,374 bytes
コンパイル時間 4,722 ms
コンパイル使用メモリ 267,332 KB
最終ジャッジ日時 2025-02-24 23:00:57
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 45
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define ALL(a)  (a).begin(),(a).end()
#include <atcoder/all>
using namespace atcoder;
#define int long long
using ll = long long;
using P = pair<int,int>;
using mint = modint998244353;
int INF=1e18+1;

template <typename T> inline bool chmin(T& a, const T& b) {bool compare = a > b; if (a > b) a = b; return compare;}
template <typename T> inline bool chmax(T& a, const T& b) {bool compare = a < b; if (a < b) a = b; return compare;}
template <typename T> inline T lcm(T a, T b) {return (a * b) / gcd(a, b);}
template <typename T> inline T ceil(T a,T b) {return (a+(b-1))/b;}
template<class... T> inline void input(T&... a) { ((cin >> a), ...); }

int H, W;
vector<vector<char>> grid;
vector<vector<bool>> visited;

struct Position {
    int x, y;
};

const vector<Position> directions = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};

bool is_in_grid(int x, int y) {
    return x >= 0 && x < H && y >= 0 && y < W;
}

vector<Position> dfs(int x, int y, char target_char) {
    vector<Position> component;
    stack<Position> stk;
    stk.push({x, y});
    visited[x][y] = true;

    while (!stk.empty()) {
        Position pos = stk.top();
        stk.pop();
        component.push_back(pos);

        for (const Position &dir : directions) {
            int nx = pos.x + dir.x;
            int ny = pos.y + dir.y;
            if (is_in_grid(nx, ny) && !visited[nx][ny] && grid[nx][ny] == target_char) {
                visited[nx][ny] = true;
                stk.push({nx, ny});
            }
        }
    }

    return component;
}

signed main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    input(H, W);
    grid.resize(H, vector<char>(W));
    visited.resize(H, vector<bool>(W, false));

    rep(i, H) {
        rep(j, W) {
            input(grid[i][j]);
        }
    }

    rep(i, H) {
        rep(j, W) {
            if (grid[i][j] != '.' && !visited[i][j]) {
                vector<Position> component = dfs(i, j, grid[i][j]);
                if (component.size() >= 4) {
                    for (const Position &pos : component) {
                        grid[pos.x][pos.y] = '.';
                    }
                }
            }
        }
    }

    rep(i, H) {
        rep(j, W) {
            cout << grid[i][j];
        }
        cout << endl;
    }
}
0