結果
問題 | No.2946 Puyo |
ユーザー | ks2m |
提出日時 | 2024-10-25 22:35:42 |
言語 | Java (openjdk 23) |
結果 |
AC
|
実行時間 | 551 ms / 2,000 ms |
コード長 | 1,644 bytes |
コンパイル時間 | 5,794 ms |
コンパイル使用メモリ | 77,588 KB |
実行使用メモリ | 66,292 KB |
最終ジャッジ日時 | 2024-10-25 22:36:10 |
合計ジャッジ時間 | 25,664 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge1 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 45 |
ソースコード
import java.util.Scanner; public class Main { static int w; public static void main(String[] args) throws Exception { Scanner sc = new Scanner(System.in); int h = sc.nextInt(); w = sc.nextInt(); char[][] g = new char[h][w]; for (int i = 0; i < h; i++) { g[i] = sc.next().toCharArray(); } sc.close(); int hw = h * w; UnionFind uf = new UnionFind(hw); for (int i = 0; i < h; i++) { for (int j = 0; j < w; j++) { if (i > 0) { if (g[i - 1][j] == g[i][j]) { uf.union(toInt(i - 1, j), toInt(i, j)); } } if (j > 0) { if (g[i][j - 1] == g[i][j]) { uf.union(toInt(i, j - 1), toInt(i, j)); } } } } for (int i = 0; i < h; i++) { for (int j = 0; j < w; j++) { if (uf.size(toInt(i, j)) >= 4) { g[i][j] = '.'; } } System.out.println(g[i]); } } static int toInt(int x, int y) { return x * w + y; } static class UnionFind { int[] parent, size; int num = 0; // 連結成分の数 UnionFind(int n) { parent = new int[n]; size = new int[n]; num = n; for (int i = 0; i < n; i++) { parent[i] = i; size[i] = 1; } } void union(int x, int y) { int px = find(x); int py = find(y); if (px != py) { parent[px] = py; size[py] += size[px]; num--; } } int find(int x) { if (parent[x] == x) { return x; } parent[x] = find(parent[x]); return parent[x]; } /** * xとyが同一連結成分か */ boolean same(int x, int y) { return find(x) == find(y); } /** * xを含む連結成分のサイズ */ int size(int x) { return size[find(x)]; } } }