結果

問題 No.2946 Puyo
ユーザー tenten
提出日時 2024-10-28 09:23:58
言語 Java
(openjdk 23)
結果
AC  
実行時間 354 ms / 2,000 ms
コード長 2,901 bytes
コンパイル時間 2,542 ms
コンパイル使用メモリ 78,992 KB
実行使用メモリ 66,448 KB
最終ジャッジ日時 2024-10-28 09:24:17
合計ジャッジ時間 16,812 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 45
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.*;
import java.util.*;
import java.util.function.*;
import java.util.stream.*;

public class Main {
    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner();
        int h = sc.nextInt();
        int w = sc.nextInt();
        char[][] field = new char[h][];
        UnionFindTree uft = new UnionFindTree(h * w);
        for (int i = 0; i < h; i++) {
            field[i] = sc.next().toCharArray();
            for (int j = 0; j < w; j++) {
                if (i > 0) {
                    if (field[i - 1][j] == field[i][j]) {
                        uft.unite((i - 1) * w + j, i * w + j);
                    }
                }
                if (j > 0) {
                    if (field[i][j - 1] == field[i][j]) {
                        uft.unite(i * w + j - 1, i * w + j);
                    }
                }
            }
        }
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < h; i++) {
            for (int j = 0; j < w; j++) {
                if (uft.getCount(i * w + j) >= 4) {
                    field[i][j] = '.';
                }
            }
            sb.append(new String(field[i])).append("\n");
        }
        System.out.print(sb);
    }
    
    static class UnionFindTree {
        int[] parents;
        int[] counts;
        
        public UnionFindTree(int size) {
            parents = IntStream.range(0, size).toArray();
            counts = new int[size];
            Arrays.fill(counts, 1);
        }
        
        public int find(int x) {
            return x == parents[x] ? x : (parents[x] = find(parents[x]));
        }
        
        public boolean same(int x, int y) {
            return find(x) == find(y);
        }
        
        public void unite(int x, int y) {
            int xx = find(x);
            int yy = find(y);
            if (xx != yy) {
                parents[xx] = yy;
                counts[yy] += counts[xx];
            }
        }
        
        public int getCount(int x) {
            return counts[find(x)];
        }
    }
}

class Scanner {
    BufferedReader br;
    StringTokenizer st = new StringTokenizer("");

    public Scanner() {
        try {
            br = new BufferedReader(new InputStreamReader(System.in));
        } catch (Exception e) {
            
        }
    }
    
    public int nextInt() {
        return Integer.parseInt(next());
    }
    
    public long nextLong() {
        return Long.parseLong(next());
    }
    
    public double nextDouble() {
        return Double.parseDouble(next());
    }
    
    public String next() {
        try {
            while (!st.hasMoreTokens()) {
                st = new StringTokenizer(br.readLine());
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            return st.nextToken();
        }
    }
    
}


0