← Home
For problem statement at 0-999/800-899/810-819/811/problemD.txt this is a correct solution, but verifier at 0-999/800-899/810-819/811/verifierD.go ends with case 1 failed: runtime error: exit status 2
panic: runtime error: index out of range [1] with length 1

goroutine 1 [running]:
main.main()
	/tmp/build-219668688/solution.go:29 +0x1424

input:
5 6 0 0
......
......
..*..F
.*....
...*..
exit status 1 can you fix the verifier? ```go
package main

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

type Pos struct {
	r, c int
}

func main() {
	defer os.Stdout.Sync()
	var reader = bufio.NewReader(os.Stdin)
	var writer = bufio.NewWriter(os.Stdout)
	scanf := func(f string, a ...interface{}) { fmt.Fscanf(reader, f, a...) }
	printf := func(f string, a ...interface{}) { fmt.Fprintf(writer, f, a...) }

	var n, m int
	scanf("%d %d\n", &n, &m)
	grid := make([]string, n)
	for i := 0; i < n; i++ {
		scanf("%s\n", &grid[i])
	}
	var fx, fy int
	for i := 0; i < n; i++ {
		for j := 0; j < m; j++ {
			if grid[i][j] == 'F' {
				fx = i + 1
				fy = j + 1
			}
		}
	}
	x, y := 1, 1
	h_known := false
	v_known := false
	var h_swapped, v_swapped bool

	send := func(move string) (int, int) {
		printf("%s\n", move)
		writer.Flush()
		var nx, ny int
		scanf("%d %d\n", &nx, &ny)
		if nx == fx && ny == fy {
			os.Exit(0)
		}
		return nx, ny
	}

	safe_h_test := func(posx, posy int) bool {
		tl_x, tl_y := posx, posy-1
		tr_x, tr_y := posx, posy+1
		left_safe := true
		right_safe := true
		if tl_x >= 1 && tl_x <= n && tl_y >= 1 && tl_y <= m {
			if grid[tl_x-1][tl_y-1] == '*' {
				left_safe = false
			}
		}
		if tr_x >= 1 && tr_x <= n && tr_y >= 1 && tr_y <= m {
			if grid[tr_x-1][tr_y-1] == '*' {
				right_safe = false
			}
		}
		return left_safe && right_safe
	}

	safe_v_test := func(posx, posy int) bool {
		tu_x, tu_y := posx-1, posy
		td_x, td_y := posx+1, posy
		up_safe := true
		down_safe := true
		if tu_x >= 1 && tu_x <= n && tu_y >= 1 && tu_y <= m {
			if grid[tu_x-1][tu_y-1] == '*' {
				up_safe = false
			}
		}
		if td_x >= 1 && td_x <= n && td_y >= 1 && td_y <= m {
			if grid[td_x-1][td_y-1] == '*' {
				down_safe = false
			}
		}
		return up_safe && down_safe
	}

	discover_h := func(px, py *int) {
		oldx := *px
		oldy := *py
		nx, ny := send("R")
		*px = nx
		*py = ny
		if nx == oldx && ny == oldy {
			left_out := (oldy-1 < 1)
			right_out := (oldy+1 > m)
			if left_out && right_out {
				return
			}
			if !left_out && !right_out {
				return
			}
			if left_out {
				h_swapped = true
			} else {
				h_swapped = false
			}
		} else if nx == oldx && ny == oldy+1 {
			h_swapped = false
		} else if nx == oldx && ny == oldy-1 {
			h_swapped = true
		} else {
			return
		}
		h_known = true
	}

	discover_v := func(px, py *int) {
		oldx := *px
		oldy := *py
		nx, ny := send("D")
		*px = nx
		*py = ny
		if nx == oldx && ny == oldy {
			up_out := (oldx-1 < 1)
			down_out := (oldx+1 > n)
			if up_out && down_out {
				return
			}
			if !up_out && !down_out {
				return
			}
			if up_out {
				v_swapped = true
			} else {
				v_swapped = false
			}
		} else if ny == oldy && nx == oldx+1 {
			v_swapped = false
		} else if ny == oldy && nx == oldx-1 {
			v_swapped = true
		} else {
			return
		}
		v_known = true
	}

	get_button := func(actual string) string {
		switch actual {
		case "L":
			if h_swapped {
				return "R"
			}
			return "L"
		case "R":
			if h_swapped {
				return "L"
			}
			return "R"
		case "U":
			if v_swapped {
				return "D"
			}
			return "U"
		case "D":
			if v_swapped {
				return "U"
			}
			return "D"
		}
		return ""
	}

	findPath := func(s_r, s_c, t_r, t_c int, h_ok, v_ok bool) []string {
		drow := []int{-1, 0, 1, 0}
		dcol := []int{0, -1, 0, 1}
		dname := []string{"U", "L", "D", "R"}
		visited := make([][]bool, n+2)
		for i := 0; i <= n+1; i++ {
			visited[i] = make([]bool, m+2)
		}
		queue := []Pos{}
		queue = append(queue, Pos{s_r, s_c})
		visited[s_r][s_c] = true
		prev := make(map[Pos]string)
		from := make(map[Pos]Pos)
		for len(queue) > 0 {
			cur := queue[0]
			queue = queue[1:]
			if cur.r == t_r && cur.c == t_c {
				break
			}
			for d := 0; d < 4; d++ {
				if (d == 0 || d == 2) && !v_ok {
					continue
				}
				if (d == 1 || d == 3) && !h_ok {
					continue
				}
				nr := cur.r + drow[d]
				nc := cur.c + dcol[d]
				if nr < 1 || nr > n || nc < 1 || nc > m {
					continue
				}
				if grid[nr-1][nc-1] == '*' {
					continue
				}
				if visited[nr][nc] {
					continue
				}
				visited[nr][nc] = true
				queue = append(queue, Pos{nr, nc})
				prev[Pos{nr, nc}] = dname[d]
				from[Pos{nr, nc}] = cur
			}
		}
		if !visited[t_r][t_c] {
			return nil
		}
		var moves []string
		cur := Pos{t_r, t_c}
		for cur.r != s_r || cur.c != s_c {
			move := prev[cur]
			moves = append(moves, move)
			cur = from[cur]
		}
		for i, j := 0, len(moves)-1; i < j; i, j = i+1, j-1 {
			moves[i], moves[j] = moves[j], moves[i]
		}
		return moves
	}

	if !h_known && safe_h_test(x, y) {
		discover_h(&x, &y)
	}
	if !v_known && safe_v_test(x, y) {
		discover_v(&x, &y)
	}

	if h_known && v_known {
		moves := findPath(x, y, fx, fy, true, true)
		for _, act := range moves {
			btn := get_button(act)
			nx, ny := send(btn)
			x, y = nx, ny
		}
		return
	} else if h_known && !v_known {
		dir_right := true
		for {
			if !v_known && safe_v_test(x, y) {
				discover_v(&x, &y)
			}
			if v_known {
				break
			}
			dy := 1
			if !dir_right {
				dy = -1
			}
			ty := y + dy
			if ty < 1 || ty > m || grid[x-1][ty-1] == '*' {
				if !dir_right {
					break
				}
				dir_right = false
				continue
			}
			actual := "R"
			if dy == -1 {
				actual = "L"
			}
			btn := get_button(actual)
			nx, ny := send(btn)
			if nx == x && ny == ty {
				x, y = nx, ny
			} else {
				if !dir_right {
					break
				}
				dir_right = false
				continue
			}
		}
	} else if v_known && !h_known {
		dir_down := true
		for {
			if !h_known && safe_h_test(x, y) {
				discover_h(&x, &y)
			}
			if h_known {
				break
			}
			dr := 1
			if !dir_down {
				dr = -1
			}
			tx := x + dr
			if tx < 1 || tx > n || grid[tx-1][y-1] == '*' {
				if !dir_down {
					break
				}
				dir_down = false
				continue
			}
			actual := "D"
			if dr == -1 {
				actual = "U"
			}
			btn := get_button(actual)
			nx, ny := send(btn)
			if nx == tx && ny == y {
				x, y = nx, ny
			} else {
				if !dir_down {
					break
				}
				dir_down = false
				continue
			}
		}
	}

	moves := findPath(x, y, fx, fy, h_known, v_known)
	for _, act := range moves {
		btn := get_button(act)
		nx, ny := send(btn)
		x, y = nx, ny
	}
}
```