結果

問題 No.971 いたずらっ子
ユーザー aru aruaru aru
提出日時 2020-11-02 21:13:31
言語 Go
(1.22.1)
結果
AC  
実行時間 1,503 ms / 2,000 ms
コード長 2,875 bytes
コンパイル時間 12,565 ms
コンパイル使用メモリ 222,976 KB
実行使用メモリ 81,664 KB
最終ジャッジ日時 2024-04-25 02:16:24
合計ジャッジ時間 24,052 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1,503 ms
80,128 KB
testcase_01 AC 1,498 ms
79,232 KB
testcase_02 AC 1,127 ms
60,928 KB
testcase_03 AC 1,118 ms
81,664 KB
testcase_04 AC 1,069 ms
76,800 KB
testcase_05 AC 1,127 ms
81,152 KB
testcase_06 AC 854 ms
63,488 KB
testcase_07 AC 10 ms
5,376 KB
testcase_08 AC 272 ms
25,728 KB
testcase_09 AC 261 ms
24,704 KB
testcase_10 AC 8 ms
5,376 KB
testcase_11 AC 1 ms
5,376 KB
testcase_12 AC 1 ms
5,376 KB
testcase_13 AC 1 ms
5,376 KB
testcase_14 AC 2 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
testcase_20 AC 1 ms
5,376 KB
testcase_21 AC 1 ms
5,376 KB
testcase_22 AC 1 ms
5,376 KB
testcase_23 AC 1 ms
5,376 KB
testcase_24 AC 1 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

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
}

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 H, W int
var s []string

var dist [][]int

//---------------------------------------------
// priority queue
//---------------------------------------------
type pqi struct{ d, cnt, x, y int }

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].d < pq[j].d }
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
}

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

func dijkstra(sx, sy int) {
	pq := priorityQueue{}
	heap.Push(&pq, pqi{0, 0, sx, sy})
	dist[sx][sy] = 0
	for len(pq) != 0 {
		cnt, cx, cy := pq[0].cnt, pq[0].x, pq[0].y
		heap.Pop(&pq)
		cnt++
		for i := 0; i < 2; i++ {
			px := cx + dx[i]
			py := cy + dy[i]
			if px >= W || py >= H {
				continue
			}
			d := 1
			if s[py][px] == 'k' {
				d += cnt
			}
			// out("px, py", px, py, string(s[py][px]), d)
			if dist[py][px] > dist[cy][cx]+d {
				dist[py][px] = dist[cy][cx] + d
				heap.Push(&pq, pqi{dist[py][px], cnt, px, py})
			}

		}
	}
}

const inf = int(1e15)

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)
	for i := 0; i < H; i++ {
		s[i] = getS()
	}

	dist = make([][]int, H)
	for i := 0; i < H; i++ {
		dist[i] = make([]int, W)
		for j := 0; j < W; j++ {
			dist[i][j] = inf
		}
	}

	dijkstra(0, 0)
	// for i := 0; i < H; i++ {
	// 	out(dist[i])
	// }
	out(dist[H-1][W-1])
}
0