結果

問題 No.157 2つの空洞
ユーザー aru aruaru aru
提出日時 2020-08-25 22:36:05
言語 Go
(1.22.1)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 2,233 bytes
コンパイル時間 12,717 ms
コンパイル使用メモリ 222,608 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-24 04:56:29
合計ジャッジ時間 13,477 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

package main

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

func out(x ...interface{}) {
	fmt.Println(x...)
}

var sc = bufio.NewScanner(os.Stdin)

func getInt() int {
	sc.Scan()
	i, e := strconv.Atoi(sc.Text())
	if e != nil {
		panic(e)
	}
	return i
}

func getInts(N int) []int {
	ret := make([]int, N)
	for i := 0; i < N; i++ {
		ret[i] = getInt()
	}
	return ret
}

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

// min, max, asub, absなど基本関数
func max(a, b int) int {
	if a > b {
		return a
	}
	return b
}

func min(a, b int) int {
	if a < b {
		return a
	}
	return b
}

func asub(a, b int) int {
	if a > b {
		return a - b
	}
	return b - a
}

func abs(a int) int {
	if a >= 0 {
		return a
	}
	return -a
}

func lowerBound(a []int, x int) int {
	idx := sort.Search(len(a), func(i int) bool {
		return a[i] >= x
	})
	return idx
}

func upperBound(a []int, x int) int {
	idx := sort.Search(len(a), func(i int) bool {
		return a[i] > x
	})
	return idx
}

var W, H int
var s []string
var n [][]int

type pair struct {
	x, y int
}

var dx = []int{-1, 1, 0, 0}
var dy = []int{0, 0, -1, 1}

func bsf(sx, sy, c int) int {
	if s[sy][sx] == '#' || n[sy][sx] != 0 {
		return c
	}
	q := make([]pair, 0)
	q = append(q, pair{sx, sy})
	n[sy][sx] = c
	for len(q) != 0 {
		cx := q[0].x
		cy := q[0].y
		q = q[1:]
		for i := 0; i < 4; i++ {
			x := cx + dx[i]
			y := cy + dy[i]
			if x < 0 || x >= W || y < 0 || y >= H {
				continue
			}
			if s[y][x] == '#' {
				continue
			}
			if n[y][x] == 0 {
				n[y][x] = c
				q = append(q, pair{x, y})
			}
		}
	}
	return c + 1
}

func main() {
	sc.Split(bufio.ScanWords)
	W, H = getInt(), getInt()
	s = make([]string, H)
	n = make([][]int, H)
	for i := 0; i < H; i++ {
		s[i] = getString()
		n[i] = make([]int, W)
	}

	cnt := 1
	for i := 0; i < H; i++ {
		for j := 0; j < W; j++ {
			cnt = bsf(j, i, cnt)
		}
	}

	a0 := make([]pair, 0)
	a1 := make([]pair, 0)
	for i := 0; i < H; i++ {
		for j := 0; j < W; j++ {
			switch n[i][j] {
			case 1:
				a0 = append(a0, pair{j, i})
			case 2:
				a1 = append(a1, pair{j, i})
			}
		}
	}

	ans := int(1e10)
	for _, v0 := range a0 {
		for _, v1 := range a1 {
			ans = min(ans, abs(v0.x-v1.x)+abs(v0.y-v1.y))
		}
	}
	out(ans - 1)
}
0