結果

問題 No.157 2つの空洞
ユーザー yukirinyukirin
提出日時 2016-03-03 19:18:37
言語 Go
(1.22.1)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 1,295 bytes
コンパイル時間 11,785 ms
コンパイル使用メモリ 230,280 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-04-19 06:48:09
合計ジャッジ時間 11,889 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,812 KB
testcase_01 AC 1 ms
6,816 KB
testcase_02 AC 1 ms
6,816 KB
testcase_03 AC 1 ms
6,940 KB
testcase_04 AC 2 ms
6,940 KB
testcase_05 AC 2 ms
6,944 KB
testcase_06 AC 1 ms
6,944 KB
testcase_07 AC 1 ms
6,944 KB
testcase_08 AC 1 ms
6,944 KB
testcase_09 AC 1 ms
6,940 KB
testcase_10 AC 2 ms
6,940 KB
testcase_11 AC 2 ms
6,940 KB
testcase_12 AC 2 ms
6,940 KB
testcase_13 AC 2 ms
6,944 KB
testcase_14 AC 1 ms
6,940 KB
testcase_15 AC 2 ms
6,940 KB
testcase_16 AC 1 ms
6,940 KB
testcase_17 AC 2 ms
6,944 KB
testcase_18 AC 1 ms
6,944 KB
testcase_19 AC 1 ms
6,940 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

package main

import (
	"bufio"
	"bytes"
	"fmt"
	"math"
	"os"
	"strconv"
)

var sc = bufio.NewScanner(os.Stdin)

func main() {
	sc.Split(bufio.ScanWords)
	w, h := nextInt(), nextInt()
	cave := make([][]byte, h)
	first, second := make([][2]int, 0, w*h), make([][2]int, 0, w*h)
	var sPos [2]int

	for i := range cave {
		b := []byte(nextLine())
		if j := bytes.IndexByte(b, '.'); j != -1 {
			sPos = [2]int{j, i}
		}
		cave[i] = b
	}

	cave[sPos[1]][sPos[0]] = 0
	move, q := [][2]int{{1, 0}, {-1, 0}, {0, 1}, {0, -1}}, make([][2]int, 0, 400)
	q = append(q, sPos)
	for len(q) > 0 {
		pos := q[0]
		q = q[1:]

		first = append(first, pos)
		for _, m := range move {
			nextI, nextJ := pos[0]+m[0], pos[1]+m[1]
			if cave[nextJ][nextI] != '.' {
				continue
			}
			cave[nextJ][nextI] = 0
			q = append(q, [2]int{nextI, nextJ})
		}
	}

	for j, l := range cave {
		for i, v := range l {
			if v != '.' {
				continue
			}
			second = append(second, [2]int{i, j})
		}
	}

	min := 100
	for _, f := range first {
		for _, s := range second {
			l := int(math.Abs(float64(f[0]-s[0])) + math.Abs(float64(f[1]-s[1])))
			if l < min {
				min = l
			}
		}
	}
	fmt.Println(min - 1)
}

func nextLine() string {
	sc.Scan()
	return sc.Text()
}

func nextInt() int {
	i, _ := strconv.Atoi(nextLine())
	return i
}
0