package main import ( "bufio" "fmt" "os" "strconv" ) func configure(scanner *bufio.Scanner) { scanner.Split(bufio.ScanWords) scanner.Buffer(make([]byte, 1000005), 1000005) } func getNextString(scanner *bufio.Scanner) string { scanned := scanner.Scan() if !scanned { panic("scan failed") } return scanner.Text() } func getNextInt(scanner *bufio.Scanner) int { i, _ := strconv.Atoi(getNextString(scanner)) return i } func getNextInt64(scanner *bufio.Scanner) int64 { i, _ := strconv.ParseInt(getNextString(scanner), 10, 64) return i } func getNextFloat64(scanner *bufio.Scanner) float64 { i, _ := strconv.ParseFloat(getNextString(scanner), 64) return i } func main() { fp := os.Stdin wfp := os.Stdout extra := 0 if os.Getenv("I") == "IronMan" { fp, _ = os.Open(os.Getenv("END_GAME")) extra = 100 } scanner := bufio.NewScanner(fp) configure(scanner) writer := bufio.NewWriter(wfp) defer func() { r := recover() if r != nil { fmt.Fprintln(writer, r) } writer.Flush() }() solve(scanner, writer) for i := 0; i < extra; i++ { fmt.Fprintln(writer, "-----------------------------------") solve(scanner, writer) } } func solve(scanner *bufio.Scanner, writer *bufio.Writer) { r := getNextInt(scanner) c := getNextInt(scanner) t := getNextInt(scanner) sy := getNextInt(scanner) sx := getNextInt(scanner) gy := getNextInt(scanner) gx := getNextInt(scanner) bb := make([][]int, r) for i := 0; i < r; i++ { bb[i] = make([]int, c) } index := 0 for i := 0; i < r; i++ { b := getNextString(scanner) for j := 0; j < c; j++ { if b[j] == '#' { bb[i][j] = -1 continue } bb[i][j] = index index++ } } if index == 1 { fmt.Fprintln(writer, 1) return } ff := makeGrid(1, index) dbl := makeGrid(index, index) ff[0][bb[sy][sx]] = 1 dy := [4]int{-1, 0, 0, 1} dx := [4]int{0, -1, 1, 0} for i := 1; i < r-1; i++ { for j := 1; j < c-1; j++ { if bb[i][j] == -1 { continue } s := 0 for k := 0; k < 4; k++ { y := i + dy[k] x := j + dx[k] if bb[y][x] == -1 { continue } s++ } for k := 0; k < 4; k++ { y := i + dy[k] x := j + dx[k] if bb[y][x] == -1 { continue } dbl[bb[i][j]][bb[y][x]] = 1.0 / float64(s) } } } for t > 0 { if t%2 == 1 { ff = mul(ff, dbl) } dbl = mul(dbl, dbl) t >>= 1 } fmt.Fprintln(writer, ff[0][bb[gy][gx]]) } func dump(a [][]float64) { for i := 0; i < len(a); i++ { fmt.Printf("%v\n", a[i]) } } func mul(dbl, dbl2 [][]float64) [][]float64 { r := len(dbl) c := len(dbl[0]) c2 := len(dbl2[0]) result := makeGrid(r, c2) for i := 0; i < r; i++ { for j := 0; j < c2; j++ { for k := 0; k < c; k++ { result[i][j] += dbl[i][k] * dbl2[k][j] } } } return result } func makeGrid(h, w int) [][]float64 { index := make([][]float64, h, h) data := make([]float64, h*w, h*w) for i := 0; i < h; i++ { index[i] = data[i*w : (i+1)*w] } return index }