結果

問題 No.179 塗り分け
ユーザー seiichiseiichi
提出日時 2021-01-13 18:53:43
言語 Go
(1.22.1)
結果
AC  
実行時間 131 ms / 3,000 ms
コード長 2,163 bytes
コンパイル時間 12,188 ms
コンパイル使用メモリ 208,804 KB
実行使用メモリ 8,292 KB
最終ジャッジ日時 2023-08-14 16:14:43
合計ジャッジ時間 15,852 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 14 ms
8,256 KB
testcase_06 AC 4 ms
7,948 KB
testcase_07 AC 131 ms
8,288 KB
testcase_08 AC 111 ms
8,292 KB
testcase_09 AC 5 ms
5,672 KB
testcase_10 AC 33 ms
8,288 KB
testcase_11 AC 28 ms
8,272 KB
testcase_12 AC 99 ms
8,216 KB
testcase_13 AC 1 ms
4,376 KB
testcase_14 AC 2 ms
4,380 KB
testcase_15 AC 2 ms
4,376 KB
testcase_16 AC 1 ms
4,376 KB
testcase_17 AC 2 ms
4,380 KB
testcase_18 AC 60 ms
8,292 KB
testcase_19 AC 54 ms
8,288 KB
testcase_20 AC 106 ms
8,292 KB
testcase_21 AC 116 ms
8,288 KB
testcase_22 AC 123 ms
8,284 KB
testcase_23 AC 45 ms
8,288 KB
testcase_24 AC 120 ms
8,272 KB
testcase_25 AC 67 ms
8,284 KB
testcase_26 AC 53 ms
8,292 KB
testcase_27 AC 115 ms
8,288 KB
testcase_28 AC 57 ms
8,288 KB
testcase_29 AC 114 ms
8,292 KB
testcase_30 AC 37 ms
8,288 KB
testcase_31 AC 116 ms
8,288 KB
testcase_32 AC 75 ms
8,288 KB
testcase_33 AC 114 ms
8,288 KB
testcase_34 AC 49 ms
8,284 KB
testcase_35 AC 115 ms
8,288 KB
testcase_36 AC 1 ms
4,376 KB
testcase_37 AC 2 ms
4,376 KB
testcase_38 AC 1 ms
4,376 KB
testcase_39 AC 2 ms
4,376 KB
testcase_40 AC 1 ms
4,380 KB
testcase_41 AC 1 ms
4,376 KB
testcase_42 AC 1 ms
4,380 KB
testcase_43 AC 4 ms
5,700 KB
testcase_44 AC 15 ms
8,280 KB
testcase_45 AC 2 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

package main

import "fmt"

func scan() (int, int, [][]string) {
	var H, W int
	fmt.Scan(&H, &W)
	S := make([][]string, H)
	for i := 0; i < H; i++ {
		S[i] = make([]string, W)
	}

	for i := 0; i < H; i++ {
		var s string
		fmt.Scan(&s)
		for j, c := range s {
			S[i][j] = string(c)
		}
	}
	return H, W, S
}

func check(H int, W int, dy int, dx int, S [][]string) bool {
	_S := make([][]string, len(S))
	for i := 0; i < H; i++ {
		_S[i] = make([]string, len(S[i]))
		// deep copy
		copy(_S[i], S[i])
	}

	res := false
	if dy < 0 {
		for h := H - 1; h > -1; h-- {
			for w := 0; w < W; w++ {
				if _S[h][w] == "#" {
					if 0 <= h+dy && h+dy < H && 0 <= w+dx && w+dx < W && _S[h+dy][w+dx] == "#" {
						_S[h][w] = "r"
						_S[h+dy][w+dx] = "b"
						res = true
					} else {
						res = false
						goto Exit
					}
				}
			}
		}

	} else {
		for h := 0; h < H; h++ {
			for w := 0; w < W; w++ {
				if _S[h][w] == "#" {
					if 0 <= h+dy && h+dy < H && 0 <= w+dx && w+dx < W && _S[h+dy][w+dx] == "#" {
						_S[h][w] = "r"
						_S[h+dy][w+dx] = "b"
						res = true
					} else {
						res = false
						goto Exit
					}
				}
			}
		}
	}
Exit:
	return res
}

func main() {
	H, W, S := scan()
	// H := 3
	// W := 3
	// S := [][]string{
	// 	{"#", "#", "."},
	// 	{"#", "#", "#"},
	// 	{".", "#", "."},
	// }
	// H := 3
	// W := 3
	// S := [][]string{
	// 	{".", ".", "#"},
	// 	{".", ".", "."},
	// 	{"#", ".", "."},
	// }
	// H := 5
	// W := 3
	// S := [][]string{
	// 	{".", "#", "."},
	// 	{".", "#", "."},
	// 	{".", "#", "."},
	// 	{".", "#", "."},
	// 	{".", "#", "."},
	// }
	// H := 3
	// W := 9
	// S := [][]string{
	// 	{"#", "#", "#", "#", "#", "#", ".", ".", "."},
	// 	{"#", ".", "#", "#", "#", "#", ".", "#", "."},
	// 	{"#", "#", "#", "#", "#", "#", ".", ".", "."},
	// }

	res := false
	// 先に移動量だけ決めて探索する
	// 縦軸の移動はマイナスも考慮する
	for h := -(H - 1); h < H; h++ {
		for w := 0; w < W; w++ {
			if h == 0 && w == 0 {
				continue
			}
			dy, dx := h, w
			res = check(H, W, dy, dx, S)
			if res {
				goto Exit
			}
		}
	}
Exit:
	if res {
		fmt.Print("YES")
	} else {
		fmt.Print("NO")
	}
}
0