Submission #1789667


Source Code Expand

using System;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using System.Text;
using System.Globalization;
using System.Diagnostics;
using static System.Console;
//using System.Numerics;
//using static System.Math;

class Program
{
    static void Main()
    {
        //SetOut(new StreamWriter(OpenStandardOutput()) { AutoFlush = false });
        new Program().solve();
        Out.Flush();
    }
    Scanner cin = new Scanner();
    readonly int[] dd = { 0, 1, 0, -1, 0 }; //→↓←↑
    readonly int mod = 1000000007;

    int H, W;
    void solve()
    {
        H = cin.nextint;
        W = cin.nextint;
        var S = new char[H][];
        for (int i = 0; i < H; i++)
        {
            S[i] = cin.next.ToCharArray();
        }
        int T = 2 * H * W + 1;
        var D = new Dinic(T + 1);

        
        for (int i = 0; i < H; i++)
        {
            for (int j = 0; j < W; j++)
            {
                if (i == 0 || i == H - 1 || j == 0 || j == W - 1)
                {
                    if (S[i][j] == 'X')
                    {
                        WriteLine(-1);
                        return;
                    }
                    D.add_edge(0, IN(i, j), Dinic.INF);
                }
                if (S[i][j] == 'X')
                {
                    D.add_edge(IN(i, j), OUT(i, j), Dinic.INF);
                    D.add_edge(OUT(i, j), T, Dinic.INF);
                }
                else
                {
                    D.add_edge(IN(i, j), OUT(i, j), 1);
                    for (int r = 0; r < 4; r++)
                    {
                        int y = i + dd[r];
                        int x = j + dd[r + 1];
                        if (y < 0 || y >= H || x < 0 || x >= W) continue;

                        D.add_edge(OUT(i, j), IN(y, x), Dinic.INF);
                    }
                }
            }
        }

        WriteLine(D.max_flow(0, T));
    }
    int IN(int y, int x)
    {
        return y * W + x + 1;
    }
    int OUT(int y, int x)
    {
        return y * W + x + 1 + H * W;
    }


}

class Dinic
{
    // 辺を表すクラス(行き先、容量、逆辺)
    class Edge_
    {
        public int to;
        public long cap;
        public int rev;
        public Edge_(int to, long cap, int rev)
        {
            this.to = to;
            this.cap = cap;
            this.rev = rev;
        }
    }
    public const long INF = long.MaxValue / 3;
    List<Edge_>[] G; // グラフの隣接リスト表現
    int[] level;    // sからの距離
    int[] iter;     // どこまで調べ終わったか
    public Dinic(int V)
    {
        G = new List<Edge_>[V];
        for (int i = 0; i < V; i++) G[i] = new List<Edge_>();
        level = new int[V];
        iter = new int[V];
    }
    // fromからtoまでの最短距離をBFSで計算する
    public void add_edge(int from, int to, long cap)
    {
        G[from].Add(new Edge_(to, cap, G[to].Count));
        G[to].Add(new Edge_(from, 0, G[from].Count - 1));
    }
    // sからの最短距離をBFSで計算する
    void bfs(int s)
    {
        for (int i = 0; i < level.Length; i++) level[i] = -1;
        var que = new Queue<int>();
        level[s] = 0;
        que.Enqueue(s);
        while (que.Any())
        {
            int v = que.Dequeue();
            foreach (Edge_ e in G[v])
            {
                if (e.cap > 0 && level[e.to] < 0)
                {
                    level[e.to] = level[v] + 1;
                    que.Enqueue(e.to);
                }
            }
        }
    }
    //増加パスをDFSで探す
    long dfs(int v, int t, long f)
    {
        if (v == t) return f;
        for (int i = iter[v]; i < G[v].Count; i++)
        {
            Edge_ e = G[v][i];
            if (e.cap > 0 && level[v] < level[e.to])
            {
                long d = dfs(e.to, t, Math.Min(f, e.cap));
                if (d > 0)
                {
                    e.cap -= d;
                    G[e.to][e.rev].cap += d;
                    return d;
                }
            }
        }
        return 0;
    }
    // sからtへの最大流を求める
    public long max_flow(int s, int t)
    {
        long flow = 0;
        while (true)
        {
            bfs(s);
            if (level[t] < 0) return flow;
            for (int i = 0; i < iter.Length; i++) iter[i] = 0;
            while (true)
            {
                long f = dfs(s, t, INF);
                if (f <= 0) break;
                flow += f;
            }
        }
    }
}

class Scanner
{
    string[] s; int i;
    char[] cs = new char[] { ' ' };
    public Scanner() { s = new string[0]; i = 0; }
    public string[] scan { get { return ReadLine().Split(); } }
    public int[] scanint { get { return Array.ConvertAll(scan, int.Parse); } }
    public long[] scanlong { get { return Array.ConvertAll(scan, long.Parse); } }
    public double[] scandouble { get { return Array.ConvertAll(scan, double.Parse); } }
    public string next
    {
        get
        {
            if (i < s.Length) return s[i++];
            string st = ReadLine();
            while (st == "") st = ReadLine();
            s = st.Split(cs, StringSplitOptions.RemoveEmptyEntries);
            i = 0;
            return next;
        }
    }
    public int nextint { get { return int.Parse(next); } }
    public long nextlong { get { return long.Parse(next); } }
    public double nextdouble { get { return double.Parse(next); } }
}

Submission Info

Submission Time
Task E - Fences
User claw88
Language C# (Mono 4.6.2.0)
Score 0
Code Size 5721 Byte
Status TLE
Exec Time 2108 ms
Memory 23368 KB

Judge Result

Set Name All
Score / Max Score 0 / 150
Status
AC × 14
TLE × 5
Set Name Test Cases
All 00_sample.txt, 01_sample.txt, 02_sample.txt, 10_rand_00.txt, 10_rand_01.txt, 10_rand_02.txt, 10_rand_03.txt, 10_rand_04.txt, 10_rand_05.txt, 10_rand_06.txt, 10_rand_07.txt, 10_rand_08.txt, 11_hashi_00.txt, 11_hashi_01.txt, 11_hashi_02.txt, 12_rect_00.txt, 12_rect_01.txt, 12_rect_02.txt, 99_all_one.txt
Case Name Status Exec Time Memory
00_sample.txt AC 27 ms 11476 KB
01_sample.txt AC 23 ms 9172 KB
02_sample.txt AC 27 ms 11476 KB
10_rand_00.txt AC 27 ms 11476 KB
10_rand_01.txt AC 30 ms 11348 KB
10_rand_02.txt AC 1837 ms 21848 KB
10_rand_03.txt TLE 2108 ms 17376 KB
10_rand_04.txt AC 37 ms 11616 KB
10_rand_05.txt TLE 2108 ms 17456 KB
10_rand_06.txt AC 59 ms 17756 KB
10_rand_07.txt TLE 2108 ms 19100 KB
10_rand_08.txt TLE 2108 ms 23368 KB
11_hashi_00.txt AC 23 ms 9276 KB
11_hashi_01.txt AC 26 ms 11276 KB
11_hashi_02.txt AC 24 ms 9260 KB
12_rect_00.txt TLE 2108 ms 18648 KB
12_rect_01.txt AC 395 ms 18952 KB
12_rect_02.txt AC 46 ms 13664 KB
99_all_one.txt AC 24 ms 9312 KB