結果
| 問題 |
No.157 2つの空洞
|
| コンテスト | |
| ユーザー |
femto
|
| 提出日時 | 2015-04-10 09:51:23 |
| 言語 | C#(csc) (csc 3.9.0) |
| 結果 |
MLE
|
| 実行時間 | - |
| コード長 | 9,513 bytes |
| コンパイル時間 | 2,869 ms |
| コンパイル使用メモリ | 118,284 KB |
| 実行使用メモリ | 534,856 KB |
| 最終ジャッジ日時 | 2024-07-04 13:29:57 |
| 合計ジャッジ時間 | 7,818 ms |
|
ジャッジサーバーID (参考情報) |
judge5 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 4 |
| other | AC * 13 MLE * 1 -- * 2 |
コンパイルメッセージ
Microsoft (R) Visual C# Compiler version 3.9.0-6.21124.20 (db94f4cc) Copyright (C) Microsoft Corporation. All rights reserved.
ソースコード
using System;
using System.Diagnostics;
using System.Collections.Generic;
using System.Linq;
using Enu = System.Linq.Enumerable;
class Solution {
int[] dx = new int[] { 1, 0, -1, 0 }, dy = new int[] { 0, 1, 0, -1 };
int W, H;
int[,] field;
bool[,] visited;
Queue<point>[] hole;
void calc() {
rio(out W, out H);
field = new int[H, W];
for (int i = 0; i < H; i++) {
var s = Console.ReadLine();
for (int j = 0; j < W; j++) {
field[i, j] = s[j] == '.' ? 0 : 1;
}
}
visited = new bool[H, W];
hole = new Queue<point>[2];
for (int i = 0; i < 2; i++) hole[i] = new Queue<point>();
dfs(0);
dfs(1);
var dijkstra = new Algorithm.Dijkstra(H * W);
for (int i = 0; i < H; i++) {
for (int j = 0; j < W; j++) {
for (int k = 0; k < 4; k++) {
int ni = i + dy[k], nj = j + dx[k];
if (ni < 0 || H <= ni || nj < 0 || W <= nj) continue;
dijkstra.addDirEdge(i * W + j, ni * W + nj, field[ni, nj]);
}
}
}
var start = hole[0].First();
dijkstra.calcShortestPath(start.y * W + start.x);
var goal = hole[1].First();
var ans = dijkstra.d[goal.y * W + goal.x];
Console.WriteLine(ans);
}
void dfs(int n) {
for (int i = 0; i < H; i++) {
for (int j = 0; j < W; j++) {
if (field[i, j] == 1 || visited[i, j]) continue;
var search = new Queue<point>();
search.Enqueue(new point(j, i));
while (search.Count != 0) {
var p = search.Dequeue();
visited[p.y, p.x] = true;
hole[n].Enqueue(new point(p.x, p.y));
for (int k = 0; k < dx.Length; k++) {
var np = new point(p.x + dx[k], p.y + dy[k]);
if (np.x < 0 || W <= np.x || np.y < 0 || H <= np.y) continue;
if (field[np.y, np.x] == 0 && !visited[np.y, np.x])
search.Enqueue(np);
}
}
return;
}
}
return;
}
static void Main(string[] args) {
new Solution().calc();
}
#region
static int ri() { return int.Parse(Console.ReadLine()); }
static int[] ria(int n) {
if (n <= 0) { Console.ReadLine(); return new int[0]; }
else return Console.ReadLine().Trim().Split(' ').Select(int.Parse).ToArray();
}
static void rio(out int p1) { p1 = ri(); }
static void rio(out int p1, out int p2) { var r = ria(2); p1 = r[0]; p2 = r[1]; }
static void rio(out int p1, out int p2, out int p3) { var r = ria(3); p1 = r[0]; p2 = r[1]; p3 = r[2]; }
static void rio(out int p1, out int p2, out int p3, out int p4) { var r = ria(4); p1 = r[0]; p2 = r[1]; p3 = r[2]; p4 = r[3]; }
static void rio(out int p1, out int p2, out int p3, out int p4, out int p5) { var r = ria(5); p1 = r[0]; p2 = r[1]; p3 = r[2]; p4 = r[3]; p5 = r[4]; }
static long rl() { return long.Parse(Console.ReadLine()); }
static long[] rla(int n) {
if (n <= 0) { Console.ReadLine(); return new long[0]; }
else return Console.ReadLine().Trim().Split(' ').Select(long.Parse).ToArray();
}
static void rlo(out long p1) { p1 = rl(); }
static void rlo(out long p1, out long p2) { var r = rla(2); p1 = r[0]; p2 = r[1]; }
static void rlo(out long p1, out long p2, out long p3) { var r = rla(3); p1 = r[0]; p2 = r[1]; p3 = r[2]; }
static void rlo(out long p1, out long p2, out long p3, out long p4) { var r = rla(4); p1 = r[0]; p2 = r[1]; p3 = r[2]; p4 = r[3]; }
static void rlo(out long p1, out long p2, out long p3, out long p4, out long p5) { var r = rla(5); p1 = r[0]; p2 = r[1]; p3 = r[2]; p4 = r[3]; p5 = r[4]; }
static double rd() { return double.Parse(Console.ReadLine()); }
static double[] rda(int n) {
if (n <= 0) { Console.ReadLine(); return new double[0]; }
else return Console.ReadLine().Trim().Split(' ').Select(double.Parse).ToArray();
}
static void rdo(out double p1) { p1 = rd(); }
static void rdo(out double p1, out double p2) { var r = rda(2); p1 = r[0]; p2 = r[1]; }
static void rdo(out double p1, out double p2, out double p3) { var r = rda(3); p1 = r[0]; p2 = r[1]; p3 = r[2]; }
static void rdo(out double p1, out double p2, out double p3, out double p4) { var r = rda(4); p1 = r[0]; p2 = r[1]; p3 = r[2]; p4 = r[3]; }
static void rdo(out double p1, out double p2, out double p3, out double p4, out double p5) { var r = rda(5); p1 = r[0]; p2 = r[1]; p3 = r[2]; p4 = r[3]; p5 = r[4]; }
static void swap<T>(ref T x, ref T y) { T temp = x; x = y; y = temp; }
static void wa1<T>(T[] a) { Debug.WriteLine(string.Join(" ", a)); }
static void wa2<T>(T[][] a) {
foreach (var row in a) {
Debug.WriteLine(String.Join(" ", row));
}
}
[DebuggerDisplay("{x} , {y}")]
class point {
public int x, y;
public point(int x, int y) {
this.x = x; this.y = y;
}
}
#endregion
}
static class Extention {
public static T[][] ToJagArray<T>(this T[,] a) {
int n = a.GetLength(0), m = a.GetLength(1);
var ret = new T[n][];
for (int i = 0; i < n; i++) {
ret[i] = new T[m];
for (int j = 0; j < m; j++) {
ret[i][j] = a[i, j];
}
}
return ret;
}
}
namespace Algorithm {
using T = System.Int32;
// require PriorityQueue
public class Dijkstra {
public T INF = (T)1e9;
[DebuggerDisplay("from : {from}, to : {to}, cost : {cost}")]
private class edge {
public int from, to;
public T cost;
public edge(int from, int to, T cost) {
this.from = from; this.to = to; this.cost = cost;
}
}
[DebuggerDisplay("v : {v}, cost : {cost}")]
private class state {
public int v;
public T cost;
public state(int v, T cost) {
this.v = v; this.cost = cost;
}
}
private int vnum;
private List<edge>[] edgelists;
public T[] d;
private PriorityQueue<state> que;
public Dijkstra(int vnum) {
this.vnum = vnum;
edgelists = new List<edge>[vnum + 1];
for (int i = 0; i < edgelists.Length; i++)
edgelists[i] = new List<edge>();
d = new T[vnum + 1];
}
public void addDirEdge(int v1, int v2, T cost) {
edgelists[v1].Add(new edge(v1, v2, cost));
}
public void addUnDirEdge(int v1, int v2, T cost) {
edgelists[v1].Add(new edge(v1, v2, cost));
edgelists[v2].Add(new edge(v2, v1, cost));
}
public void calcShortestPath(int s) {
for (int i = 0; i < d.Length; i++)
d[i] = INF;
que = new PriorityQueue<state>((s1, s2) => s2.cost.CompareTo(s1.cost));
d[s] = 0;
que.Push(new state(s, 0));
while (que.Count() != 0) {
var t = que.Pop();
int v = t.v;
if (d[v] < t.cost) continue;
foreach (var e in edgelists[v]) {
if (d[e.to] > d[v] + e.cost) {
d[e.to] = d[v] + e.cost;
que.Push(new state(e.to, d[e.to]));
}
}
}
}
}
public class PriorityQueue<T> {
private List<T> buffer;
private Comparison<T> comp;
// 昇順
// (e1, e2) => e2.CompareTo(e1)
// 降順
// (e1, e2) => e1.CompareTo(e2)
public PriorityQueue(Comparison<T> comp) {
buffer = new List<T>();
this.comp = comp;
}
public PriorityQueue(Comparison<T> comp, IEnumerable<T> data)
: this(comp) {
foreach (T e in data) Push(e);
}
public void Push(T elem) { PushHeap(this.buffer, elem); }
public T Pop() { return PopHeap(this.buffer); }
public int Count() { return buffer.Count; }
public T Top() { return buffer[0]; }
private void PushHeap(List<T> array, T elem) {
int n = array.Count;
array.Add(elem);
while (n != 0) {
int i = (n - 1) / 2;
// 親と値を入れ替え
if (comp(array[n], array[i]) > 0) {
T tmp = array[n]; array[n] = array[i]; array[i] = tmp;
}
n = i;
}
}
private T PopHeap(List<T> array) {
int n = array.Count - 1;
T returnelem = array[0];
array[0] = array[n];
array.RemoveAt(n);
for (int i = 0, j; (j = 2 * i + 1) < n; ) {
// 値の大きい方の子を選ぶ
if ((j != n - 1) && (comp(array[j], array[j + 1]) < 0))
j++;
// 子と値を入れ替え
if (comp(array[i], array[j]) < 0) {
T tmp = array[j]; array[j] = array[i]; array[i] = tmp;
}
i = j;
}
return returnelem;
}
}
}
femto