結果

問題 No.13 囲みたい!
ユーザー mbanmban
提出日時 2017-04-21 04:14:28
言語 C#(csc)
(csc 3.9.0)
結果
AC  
実行時間 64 ms / 5,000 ms
コード長 2,649 bytes
コンパイル時間 3,967 ms
コンパイル使用メモリ 107,420 KB
実行使用メモリ 23,912 KB
最終ジャッジ日時 2023-09-27 07:48:22
合計ジャッジ時間 4,600 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 55 ms
21,596 KB
testcase_01 AC 56 ms
19,644 KB
testcase_02 AC 56 ms
21,744 KB
testcase_03 AC 58 ms
21,928 KB
testcase_04 AC 57 ms
21,920 KB
testcase_05 AC 57 ms
21,844 KB
testcase_06 AC 63 ms
23,912 KB
testcase_07 AC 61 ms
21,844 KB
testcase_08 AC 63 ms
21,928 KB
testcase_09 AC 61 ms
22,000 KB
testcase_10 AC 63 ms
23,760 KB
testcase_11 AC 64 ms
21,808 KB
testcase_12 AC 58 ms
23,668 KB
testcase_13 AC 58 ms
21,728 KB
testcase_14 AC 58 ms
21,720 KB
testcase_15 AC 58 ms
19,604 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Microsoft (R) Visual C# Compiler version 3.9.0-6.21124.20 (db94f4cc)
Copyright (C) Microsoft Corporation. All rights reserved.

ソースコード

diff #

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;
        }
    }
}
0