結果

問題 No.34 砂漠の行商人
ユーザー aru aruaru aru
提出日時 2020-07-12 22:51:56
言語 Go
(1.22.1)
結果
AC  
実行時間 5 ms / 5,000 ms
コード長 2,266 bytes
コンパイル時間 15,100 ms
コンパイル使用メモリ 233,276 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-24 00:00:24
合計ジャッジ時間 13,477 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
5,248 KB
testcase_01 AC 1 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 3 ms
5,376 KB
testcase_08 AC 2 ms
5,376 KB
testcase_09 AC 2 ms
5,376 KB
testcase_10 AC 2 ms
5,376 KB
testcase_11 AC 2 ms
5,376 KB
testcase_12 AC 2 ms
5,376 KB
testcase_13 AC 5 ms
5,376 KB
testcase_14 AC 4 ms
5,376 KB
testcase_15 AC 1 ms
5,376 KB
testcase_16 AC 2 ms
5,376 KB
testcase_17 AC 1 ms
5,376 KB
testcase_18 AC 1 ms
5,376 KB
testcase_19 AC 3 ms
5,376 KB
testcase_20 AC 3 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 3 ms
5,376 KB
testcase_25 AC 2 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

package main

import (
	"bufio"
	"fmt"
	"math"
	"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 N, V, sx, sy, gx, gy int
var m [][]int
var dist [][]int
var cnt [][]int

const inf = math.MaxInt64 / 16

type pair struct {
	x, y int
}

func bsf(sx, sy int) {
	dist = make([][]int, N)
	cnt = make([][]int, N)
	for i := 0; i < N; i++ {
		dist[i] = make([]int, N)
		cnt[i] = make([]int, N)
		for j := 0; j < N; j++ {
			dist[i][j] = inf
			cnt[i][j] = inf
		}
	}
	dx := []int{1, -1, 0, 0}
	dy := []int{0, 0, 1, -1}
	dist[sy][sx] = 0
	cnt[sy][sx] = 0
	q := make([]pair, 0)
	q = append(q, pair{sx, sy})
	for len(q) != 0 {
		p := q[0]
		if p.x == gx && p.y == gy {
			break
		}
		q = q[1:]
		for i := 0; i < 4; i++ {
			x := p.x + dx[i]
			y := p.y + dy[i]
			if x < 0 || x >= N || y < 0 || y >= N {
				continue
			}
			if cnt[p.y][p.x]+m[y][x] >= V {
				continue
			}
			if cnt[y][x] > cnt[p.y][p.x]+m[y][x] {
				dist[y][x] = dist[p.y][p.x] + 1
				cnt[y][x] = cnt[p.y][p.x] + m[y][x]
				q = append(q, pair{x, y})
			}
		}
	}

}

func main() {
	sc.Split(bufio.ScanWords)
	N, V, sx, sy, gx, gy = getInt(), getInt(),
		getInt()-1, getInt()-1, getInt()-1, getInt()-1
	m = make([][]int, N)
	for i := 0; i < N; i++ {
		m[i] = getInts(N)
	}
	bsf(sx, sy)
	// for i := 0; i < N; i++ {
	// 	out(cnt[i])
	// }

	if dist[gy][gx] != inf {
		out(dist[gy][gx])
		return
	}
	out(-1)
}
0