← Home
For problem statement at 0-999/200-299/200-209/208/problemB.txt this is a correct solution, but verifier at 0-999/200-299/200-209/208/verifierB.go ends with All 100 tests passed can you fix the verifier? package main

import (
	"bufio"
	"fmt"
	"os"
)

func main() {
	in := bufio.NewReader(os.Stdin)

	var n int
	fmt.Fscan(in, &n)

	cards := make([]string, n+1)
	for i := 1; i <= n; i++ {
		fmt.Fscan(in, &cards[i])
	}

	match := make([][]bool, n+1)
	for i := 0; i <= n; i++ {
		match[i] = make([]bool, n+1)
	}
	for i := 1; i <= n; i++ {
		for j := 1; j <= n; j++ {
			match[i][j] = cards[i][0] == cards[j][0] || cards[i][1] == cards[j][1]
		}
	}

	B := n + 1
	memo := make([]uint8, B*B*B*B)

	idx := func(m, a, b, c int) int {
		return (((m*B)+a)*B+b)*B + c
	}

	bufLen := func(a, b, c int) int {
		if a == 0 {
			return 0
		}
		if b == 0 {
			return 1
		}
		if c == 0 {
			return 2
		}
		return 3
	}

	var dfs func(int, int, int, int) bool
	dfs = func(m, a, b, c int) bool {
		if m+bufLen(a, b, c) == 1 {
			return true
		}

		id := idx(m, a, b, c)
		if memo[id] != 0 {
			return memo[id] == 2
		}

		res := false

		if a == 0 {
			if m >= 2 && match[m][m-1] && dfs(m-2, m, 0, 0) {
				res = true
			}
			if !res && m >= 4 && match[m][m-3] && dfs(m-4, m, m-2, m-1) {
				res = true
			}
		} else if b == 0 {
			if m >= 1 && match[a][m] && dfs(m-1, a, 0, 0) {
				res = true
			}
			if !res && m >= 3 && match[a][m-2] && dfs(m-3, a, m-1, m) {
				res = true
			}
		} else if c == 0 {
			if match[b][a] && dfs(m, b, 0, 0) {
				res = true
			}
			if !res && m >= 2 && match[b][m-1] && dfs(m-2, b, m, a) {
				res = true
			}
		} else {
			if match[c][b] && dfs(m, a, c, 0) {
				res = true
			}
			if !res && m >= 1 && match[c][m] && dfs(m-1, c, a, b) {
				res = true
			}
		}

		if res {
			memo[id] = 2
		} else {
			memo[id] = 1
		}
		return res
	}

	if dfs(n, 0, 0, 0) {
		fmt.Println("YES")
	} else {
		fmt.Println("NO")
	}
}