This page is still under construction.

Parts of this page are still being built. What you see may change.

Cow Travelling

Interview

Time limit1sMemory limit128 MB

Summary
Count the number of walks of exactly T steps on a grid from a start cell to a target cell, where each step moves to a vertically or horizontally adjacent open cell.
Level

Medium5 of 10

Topics
Dynamic programming, Matrix, Implementation, Brute force
Solved
No attempts yet

Problem

Searching for the very best grass, the cows are travelling about the pasture, which is represented as a grid with NN rows and MM columns (2≤N≤1002 \le N \le 100, 2≤M≤1002 \le M \le 100). A keen observer, the farmer, recorded cow Bessie's position as (R1,C1)(R_1, C_1) at a certain time and then as (R2,C2)(R_2, C_2) exactly TT (0<T≤150 < T \le 15) seconds later. He is not sure whether she passed through (R2,C2)(R_2, C_2) before TT seconds, but he knows she is there at time TT.

Every second, a cow must move from its current cell to a vertically or horizontally adjacent cell (the cows never rest). The pasture also contains trees, and no cow can travel through a tree.

Given the pasture map, where '.' marks open pasture and '*' marks a tree, compute the number SS of distinct ways to travel from (R1,C1)(R_1, C_1) to (R2,C2)(R_2, C_2) in exactly TT seconds.

Input

  • Line 1: Three space-separated integers NN, MM, and TT
  • Lines 2..N+1N+1: Each line describes one row of the pasture with exactly MM characters, each of which is '.' or '*'
  • Line N+2N+2: Four space-separated integers R1R_1, C1C_1, R2R_2, and C2C_2

Output

Output the single integer SS described above on one line.

Hint

For example, if the pasture is 4 rows by 5 columns and the cow travels from (row 1, column 3) to (row 1, column 5) in exactly 6 seconds, there is exactly one such way, since the only route travels around the two trees.

Examples3

  1. Example 1

    Input
    4 5 6
    ...*.
    ...*.
    .....
    .....
    1 3 1 5
    
    Expected output
    1
    
  2. Example 2

    Input
    2 2 2
    ..
    ..
    1 1 1 1
    
    Expected output
    2
    
  3. Example 3

    Input
    2 2 2
    ..
    ..
    1 1 2 2
    
    Expected output
    2