結果
| 問題 |
No.13 囲みたい!
|
| コンテスト | |
| ユーザー |
mban
|
| 提出日時 | 2017-04-21 04:14:28 |
| 言語 | C#(csc) (csc 3.9.0) |
| 結果 |
AC
|
| 実行時間 | 38 ms / 5,000 ms |
| コード長 | 2,649 bytes |
| コンパイル時間 | 2,305 ms |
| コンパイル使用メモリ | 107,264 KB |
| 実行使用メモリ | 19,840 KB |
| 最終ジャッジ日時 | 2024-07-20 01:56:34 |
| 合計ジャッジ時間 | 3,605 ms |
|
ジャッジサーバーID (参考情報) |
judge1 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 16 |
コンパイルメッセージ
Microsoft (R) Visual C# Compiler version 3.9.0-6.21124.20 (db94f4cc) Copyright (C) Microsoft Corporation. All rights reserved.
ソースコード
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Text;
using System.Text.RegularExpressions;
using System.Linq;
using System.IO;
class Program
{
static int W, H;
static int[] M;
static void Main(string[] args)
{
string[] line = Console.ReadLine().Split(' ');
W = int.Parse(line[0]);
H = int.Parse(line[1]);
M = new int[W * H];
for (int i = 0; i < H; i++)
{
line = Console.ReadLine().Split(' ');
for (int j = 0; j < W; j++)
{
M[P(i, j)] = int.Parse(line[j]);
}
}
UnionFind uni = new UnionFind(W * H);
for (int i = 0; i < H; i++)
{
for (int j = 0; j < W; j++)
{
int p = P(i, j);
if (i + 1 < H)
{
int q = P(i + 1, j);
if (M[p] == M[q])
{
if (uni.Union(p, q))
{
Console.WriteLine("possible");
return;
}
}
}
if (j + 1 < W)
{
int q = P(i, j + 1);
if (M[p] == M[q])
{
if (uni.Union(p, q))
{
Console.WriteLine("possible");
return;
}
}
}
}
}
Console.WriteLine("impossible");
}
static int P(int i, int j)
{
return i * W + j;
}
}
public class UnionFind
{
private int[] Par;
private int[] Rank;
public UnionFind(int n)
{
Par = (new int[n]).Select((i, index) => index).ToArray();
Rank = new int[n];
}
public int Root(int a)
{
if (Par[a] == a) return a;
else return Root(Par[a]);
}
public bool Same(int a, int b)
{
return Root(a) == Root(b);
}
public bool Union(int a, int b)
{
a = Root(a);
b = Root(b);
if (a != b)
{
if (Rank[a] < Rank[b])
{
Par[a] = b;
}
else
{
Par[b] = a;
if (Rank[a] == Rank[b])
{
Rank[a]++;
}
}
return false;
}
else
{
return true;
}
}
}
mban