結果

問題 No.226 0-1パズル
ユーザー aru aruaru aru
提出日時 2020-09-19 10:33:56
言語 Go
(1.22.1)
結果
AC  
実行時間 2 ms / 5,000 ms
コード長 2,180 bytes
コンパイル時間 18,089 ms
コンパイル使用メモリ 211,724 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-10-04 12:08:37
合計ジャッジ時間 18,412 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 1 ms
4,376 KB
testcase_11 AC 1 ms
4,380 KB
testcase_12 AC 1 ms
4,376 KB
testcase_13 AC 1 ms
4,380 KB
testcase_14 AC 1 ms
4,380 KB
testcase_15 AC 1 ms
4,380 KB
testcase_16 AC 1 ms
4,380 KB
testcase_17 AC 1 ms
4,384 KB
testcase_18 AC 2 ms
4,376 KB
testcase_19 AC 1 ms
4,376 KB
testcase_20 AC 2 ms
4,376 KB
testcase_21 AC 2 ms
4,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 H, W int
var s [][]byte

const mod = int(1e9 + 7)

var dp [][2]int

func pat() int {
	dp := make([][2]int, 101)
	dp[0][0] = 1
	for x := 0; x < W; x++ {
		for i := 0; i < 2; i++ {
			for j := 0; j < 2; j++ {
				ng := 0
				for y := 0; y < H; y++ {
					if s[y][x] != '?' && s[y][x] != byte('0'+(i+j+y)%2) {
						ng++
					}
				}
				if ng == 0 {
					dp[x+1][(i+j)%2] += dp[x][i]
					dp[x+1][(i+j)%2] %= mod
				}
			}
		}
	}
	return dp[W][0] + dp[W][1]
}

func ichimatu() int {
	ok1, ok2 := 1, 1
	for y := 0; y < H; y++ {
		for x := 0; x < W; x++ {
			if s[y][x] != '?' {
				if int(s[y][x]-'0') != ((y + x) % 2) {
					ok1 = 0
				}
				if int(s[y][x]-'0') == ((y + x) % 2) {
					ok2 = 0
				}
			}
		}
	}
	return ok1 + ok2
}

func main() {
	sc.Split(bufio.ScanWords)
	sc.Buffer([]byte{}, 1000000)
	H, W = getInt(), getInt()
	s = make([][]byte, H)
	for i := 0; i < H; i++ {
		s[i] = []byte(getString())
	}

	ret := mod - ichimatu()

	for i := 0; i < 2; i++ {
		ret += pat()
		t := make([][]byte, W)
		for i := 0; i < W; i++ {
			t[i] = make([]byte, H)
		}
		for x := 0; x < W; x++ {
			for y := 0; y < H; y++ {
				t[x][y] += s[y][x]
			}
		}
		s = t
		W, H = H, W
	}
	out(ret % mod)
}
0