結果
| 問題 |
No.2946 Puyo
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2024-10-25 21:47:33 |
| 言語 | C# (.NET 8.0.404) |
| 結果 |
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 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 45 |
コンパイルメッセージ
復元対象のプロジェクトを決定しています... /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/
ソースコード
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();
}
}