結果

問題 No.124 門松列(3)
ユーザー aru aruaru aru
提出日時 2020-08-13 17:45:34
言語 Go
(1.22.1)
結果
AC  
実行時間 3 ms / 5,000 ms
コード長 2,334 bytes
コンパイル時間 11,903 ms
コンパイル使用メモリ 222,828 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-18 01:43:10
合計ジャッジ時間 13,388 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 1 ms
5,248 KB
testcase_02 AC 2 ms
5,248 KB
testcase_03 AC 3 ms
5,376 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 1 ms
5,376 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 1 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 1 ms
5,376 KB
testcase_12 AC 1 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 2 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 2 ms
5,376 KB
testcase_22 AC 1 ms
5,376 KB
testcase_23 AC 2 ms
5,376 KB
testcase_24 AC 2 ms
5,376 KB
testcase_25 AC 2 ms
5,376 KB
testcase_26 AC 1 ms
5,376 KB
testcase_27 AC 1 ms
5,376 KB
testcase_28 AC 1 ms
5,376 KB
testcase_29 AC 2 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 m [][]int
var f [][][4]int
var dx = []int{-1, 1, 0, 0}
var dy = []int{0, 0, -1, 1}

const inf = int(1e12)

type pair struct {
	x, y, dir, v int
}

func bsf(sx, sy int) {
	q := make([]pair, 0)
	f[sy][sx][0] = 0
	f[sy+1][sx][3] = 1
	f[sy][sx+1][1] = 1
	q = append(q, pair{sx + 1, sy, 1, m[sy][sx]})
	q = append(q, pair{sx, sy + 1, 3, m[sy][sx]})
	for len(q) > 0 {
		cur := q[0]
		q = q[1:]
		v := m[cur.y][cur.x]
		// out(cur)
		for i := 0; i < 4; i++ {
			nx := cur.x + dx[i]
			ny := cur.y + dy[i]
			if nx < 0 || nx >= w || ny < 0 || ny >= h {
				continue
			}
			if cur.v == m[ny][nx] {
				continue
			}
			// out(cur, f[ny][nx][i], f[cur.y][cur.x][cur.dir])
			if f[ny][nx][i] <= f[cur.y][cur.x][cur.dir]+1 {
				continue
			}
			// out(nx, ny)
			if (v > cur.v && v > m[ny][nx]) || (v < cur.v && v < m[ny][nx]) {
				f[ny][nx][i] = f[cur.y][cur.x][cur.dir] + 1
				q = append(q, pair{nx, ny, i, v})
			}
		}
	}
}

func main() {
	sc.Split(bufio.ScanWords)
	w, h = getInt(), getInt()
	m = make([][]int, h)
	f = make([][][4]int, h)
	for i := 0; i < h; i++ {
		m[i] = getInts(w)
		f[i] = make([][4]int, w)
		for j := 0; j < w; j++ {
			for k := 0; k < 4; k++ {
				f[i][j][k] = inf
			}
		}
	}

	bsf(0, 0)
	ans := inf
	for k := 0; k < 4; k++ {
		ans = min(ans, f[h-1][w-1][k])
	}
	if ans == inf {
		out(-1)
		return
	}
	out(ans)
}
0