結果

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

テストケース

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

ソースコード

diff #

package main

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

var sc = bufio.NewScanner(os.Stdin)

func main() {
	sc.Split(bufio.ScanWords)
	w, h := nextInt(), nextInt()
	cave := make([][]byte, h)

	for i := range cave {
		cave[i] = []byte(nextLine())
	}
	var sPos [2]int

	for j, l := range cave {
		for i, b := range l {
			if b == '.' {
				sPos = [2]int{i, j}
				cave[j][i] = 0
				break
			}
		}
		if sPos[0] != 0 && sPos[1] != 0 {
			break
		}
	}

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

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

	for i := byte(0); i < 200; i++ {
		for j, l := range cave[1 : h-1] {
			for k, v := range l[1 : w-1] {
				if v != i {
					continue
				}

				for _, m := range move {
					nextI, nextJ := k+1+m[0], j+1+m[1]
					nextV := cave[nextJ][nextI]
					if nextV == '.' {
						fmt.Println(i)
						return
					}

					if nextV == '#' {
						cave[nextJ][nextI] = i + 1
					}
				}
			}
		}
	}
}
func nextLine() string {
	sc.Scan()
	return sc.Text()
}

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