結果

問題 No.2946 Puyo
ユーザー tobisatistobisatis
提出日時 2024-10-25 21:47:33
言語 C#
(.NET 8.0.203)
結果
AC  
実行時間 503 ms / 2,000 ms
コード長 4,214 bytes
コンパイル時間 11,268 ms
コンパイル使用メモリ 170,036 KB
実行使用メモリ 318,164 KB
最終ジャッジ日時 2024-10-25 21:48:26
合計ジャッジ時間 22,587 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 57 ms
30,080 KB
testcase_01 AC 56 ms
29,952 KB
testcase_02 AC 56 ms
29,824 KB
testcase_03 AC 142 ms
59,728 KB
testcase_04 AC 142 ms
59,832 KB
testcase_05 AC 144 ms
59,592 KB
testcase_06 AC 143 ms
59,984 KB
testcase_07 AC 144 ms
59,604 KB
testcase_08 AC 66 ms
33,024 KB
testcase_09 AC 88 ms
39,012 KB
testcase_10 AC 63 ms
32,256 KB
testcase_11 AC 101 ms
45,288 KB
testcase_12 AC 64 ms
32,512 KB
testcase_13 AC 57 ms
29,948 KB
testcase_14 AC 67 ms
32,896 KB
testcase_15 AC 64 ms
31,744 KB
testcase_16 AC 72 ms
34,304 KB
testcase_17 AC 126 ms
42,044 KB
testcase_18 AC 65 ms
32,000 KB
testcase_19 AC 100 ms
48,360 KB
testcase_20 AC 110 ms
50,568 KB
testcase_21 AC 141 ms
64,296 KB
testcase_22 AC 72 ms
34,816 KB
testcase_23 AC 136 ms
64,412 KB
testcase_24 AC 230 ms
83,036 KB
testcase_25 AC 235 ms
80,104 KB
testcase_26 AC 227 ms
83,060 KB
testcase_27 AC 61 ms
31,744 KB
testcase_28 AC 345 ms
144,440 KB
testcase_29 AC 503 ms
236,480 KB
testcase_30 AC 279 ms
129,868 KB
testcase_31 AC 377 ms
152,104 KB
testcase_32 AC 352 ms
147,824 KB
testcase_33 AC 311 ms
132,392 KB
testcase_34 AC 379 ms
153,896 KB
testcase_35 AC 311 ms
135,888 KB
testcase_36 AC 332 ms
134,080 KB
testcase_37 AC 294 ms
132,984 KB
testcase_38 AC 197 ms
78,828 KB
testcase_39 AC 181 ms
90,444 KB
testcase_40 AC 123 ms
62,568 KB
testcase_41 AC 215 ms
81,392 KB
testcase_42 AC 190 ms
74,116 KB
testcase_43 AC 330 ms
158,184 KB
testcase_44 AC 383 ms
153,744 KB
testcase_45 AC 357 ms
160,504 KB
testcase_46 AC 236 ms
120,104 KB
testcase_47 AC 309 ms
318,164 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
  復元対象のプロジェクトを決定しています...
  /home/judge/data/code/main.csproj を復元しました (98 ms)。
MSBuild のバージョン 17.9.6+a4ecab324 (.NET)
  main -> /home/judge/data/code/bin/Release/net8.0/main.dll
  main -> /home/judge/data/code/bin/Release/net8.0/publish/

ソースコード

diff #

namespace AtCoder;

#nullable enable

using System.Numerics;

class UnionFind
{
    (int parent, int edgeCount)[] Verticals { get; set; }
    bool EnabledUndo { get; init; }
    Stack<(int v, (int parent, int edges))> Histories { get; init; }

    public UnionFind(int verticals, bool enableUndo = false)
    {
        Verticals = new (int, int)[verticals];
        for (int i = 0; i < verticals; i++) Verticals[i] = (-1, 0);
        Histories = new();
        EnabledUndo = enableUndo;
    }

    public int Union(int x, int y)
    {
        (x, y) = (Find(x), Find(y));
        var (sx, sy) = (Size(x), Size(y));
        if (sx < sy) (x, y, sy) = (y, x, sx);
        var (nx, ny) = (Verticals[x], Verticals[y]);
        Histories.Push((x, nx));
        Histories.Push((y, ny));
        if (x == y) nx.edgeCount += 1;
        else
        {
            ny.parent = x;
            nx.parent -= sy;
            nx.edgeCount += ny.edgeCount + 1;
        }
        (Verticals[y], Verticals[x]) = (ny, nx);
        return x;
    }

    public int Find(int x)
    {
        var pid = Verticals[x].parent;
        if (pid < 0) return x;
        var aid = Find(pid);
        if (!EnabledUndo) Verticals[x].parent = aid;
        return aid;
    }
    public int Size(int x) => -Verticals[Find(x)].parent;
    public int EdgeCount(int x) => Verticals[Find(x)].edgeCount;

    public bool Undo()
    {
        if (!EnabledUndo || Histories.Count == 0) return false;
        for (var i = 0; i < 2; i++)
        {
            var (v, h) = Histories.Pop();
            Verticals[v] = h;
        }
        return true;
    }
}

static class Extensions
{
    public static T[] Repeat<T>(this int time, Func<T> F) => Enumerable.Range(0, time).Select(_ => F()).ToArray();

}

class AtCoder
{
    object? Solve()
    {
        var h = Int();
        var w = Int();
        var g = new char[h, w];
        for (var i = 0; i < h; i++)
        {
            var str = String().AsSpan();
            for (var j = 0; j < w; j++) g[i, j] = str[j];
        }

        var s = h * w;
        var uf = new UnionFind(s);
        int ID(int x, int y) => y + x * w;
        var moves = new[]{ (-1, 0), (0, -1), (0, 1), (1, 0) };
        bool Inside(int x, int y) => 0 <= x && x < h && 0 <= y && y < w;
        for (var i = 0; i < h; i++) for (var j = 0; j < w; j++)
        {
            var id = ID(i, j);
            var c = g[i, j];
            if (c == '.') continue;
            foreach (var (dx, dy) in moves)
            {
                var ni = i + dx;
                var nj = j + dy;
                if (!Inside(ni, nj) || g[ni, nj] != c) continue;
                var nid = ID(ni, nj);
                uf.Union(id, nid);
            }
        }
        for (var i = 0; i < h; i++) 
        {
            var row = new char[w];
            for (var j = 0; j < w; j++)
            {
                var id = ID(i, j);
                if (uf.Size(id) >= 4) row[j] = '.';
                else row[j] = g[i, j];
            }
            Out(new string(row));
        }
        return null;
    }

    public static void Main() => new AtCoder().Run();
    public void Run()
    {
        var res = Solve();
        if (res != null)
        {
            if (res is bool yes) res = yes ? "Yes" : "No";
            sw.WriteLine(res);
        }
        sw.Flush();
    }

    string[] input = Array.Empty<string>();
    int iter = 0;
    readonly StreamWriter sw = new(Console.OpenStandardOutput()) { AutoFlush = false };

    string String()
    {
        while (iter >= input.Length) (input, iter) = (Console.ReadLine()!.Split(' '), 0);
        return input[iter++];
    }
    T Input<T>() where T : IParsable<T> => T.Parse(String(), null);
    int Int() => Input<int>();
    void Out(object? x, string? separator = null)
    {
        separator ??= Environment.NewLine;
        if (x is System.Collections.IEnumerable obj and not string)
        {
            var firstLine = true;
            foreach (var item in obj)
            {
                if (!firstLine) sw.Write(separator);
                firstLine = false;
                sw.Write(item);
            }
        }
        else sw.Write(x);
        sw.WriteLine();
    }
}
0