結果

問題 No.402 最も海から遠い場所
ユーザー aru aruaru aru
提出日時 2021-01-07 21:14:32
言語 Go
(1.22.1)
結果
TLE  
実行時間 -
コード長 3,198 bytes
コンパイル時間 12,077 ms
コンパイル使用メモリ 224,784 KB
実行使用メモリ 227,332 KB
最終ジャッジ日時 2024-04-26 13:51:15
合計ジャッジ時間 19,068 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
13,884 KB
testcase_01 AC 1 ms
6,816 KB
testcase_02 AC 2 ms
6,812 KB
testcase_03 AC 1 ms
6,940 KB
testcase_04 AC 1 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,940 KB
testcase_08 AC 1 ms
6,940 KB
testcase_09 AC 1 ms
6,944 KB
testcase_10 AC 1 ms
6,940 KB
testcase_11 AC 2 ms
6,940 KB
testcase_12 AC 2 ms
6,940 KB
testcase_13 AC 14 ms
7,660 KB
testcase_14 AC 6 ms
6,944 KB
testcase_15 AC 70 ms
16,204 KB
testcase_16 AC 117 ms
22,424 KB
testcase_17 AC 1,423 ms
227,332 KB
testcase_18 TLE -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

package main

import (
	"bufio"
	"container/heap"
	"fmt"
	"os"
	"sort"
	"strconv"
)

var sc = bufio.NewScanner(os.Stdin)
var wr = bufio.NewWriter(os.Stdout)

func out(x ...interface{}) {
	fmt.Fprintln(wr, x...)
}

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

func getF() float64 {
	sc.Scan()
	i, e := strconv.ParseFloat(sc.Text(), 64)
	if e != nil {
		panic(e)
	}
	return i
}

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

func getS() 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
}

// min for n entry
func nmin(a ...int) int {
	ret := a[0]
	for _, e := range a {
		ret = min(ret, e)
	}
	return ret
}

// max for n entry
func nmax(a ...int) int {
	ret := a[0]
	for _, e := range a {
		ret = max(ret, e)
	}
	return ret
}

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
}

//---------------------------------------------
// priority queue
//---------------------------------------------
type pqi struct {
	x, y int
	a    int32
}

type priorityQueue []pqi

func (pq priorityQueue) Len() int            { return len(pq) }
func (pq priorityQueue) Swap(i, j int)       { pq[i], pq[j] = pq[j], pq[i] }
func (pq priorityQueue) Less(i, j int) bool  { return pq[i].a < pq[j].a }
func (pq *priorityQueue) Push(x interface{}) { *pq = append(*pq, x.(pqi)) }
func (pq *priorityQueue) Pop() interface{} {
	x := (*pq)[len(*pq)-1]
	*pq = (*pq)[0 : len(*pq)-1]
	return x
}

const inf = 100000

func main() {
	defer wr.Flush()
	sc.Split(bufio.ScanWords)
	sc.Buffer([]byte{}, 1000000)
	// this template is new version.
	// use getI(), getS(), getInts(), getF()
	H, W := getI(), getI()
	s := make([]string, H+2)
	a := make([][]int32, H+2)
	for i := 0; i < H+2; i++ {
		a[i] = make([]int32, W+2)
		for j := 0; j < W+2; j++ {
			a[i][j] = inf
		}
	}
	for i := 0; i < W+2; i++ {
		s[0] += "."
		s[H+1] += "."
	}
	for i := 0; i < H; i++ {
		s[i+1] = "." + getS() + "."
	}

	dx := []int{-1, 0, 1, -1, 1, -1, 0, 1}
	dy := []int{-1, -1, -1, 0, 0, 1, 1, 1}
	W += 2
	H += 2

	pq := priorityQueue{}

	for i := 0; i < H; i++ {
		for j := 0; j < W; j++ {
			if s[i][j] == '.' {
				heap.Push(&pq, pqi{j, i, 0})
				a[i][j] = 0
			}
		}
	}

	for len(pq) != 0 {
		e := pq[0]
		heap.Pop(&pq)
		for i := 0; i < 8; i++ {
			px := e.x + dx[i]
			py := e.y + dy[i]
			if px < 0 || px >= W || py < 0 || py >= H {
				continue
			}
			if a[py][px] > a[e.y][e.x]+1 {
				a[py][px] = a[e.y][e.x] + 1
				heap.Push(&pq, pqi{px, py, a[py][px]})
			}
		}
	}

	ans := 0
	for i := 0; i < H; i++ {
		for j := 0; j < W; j++ {
			ans = max(ans, int(a[i][j]))
		}
	}
	// for i := 0; i < H; i++ {
	// 	out(i, a[i])
	// }
	out(ans)
}
0