#P1374. 进攻幽暗城

进攻幽暗城

Description

The map of the Undercity is an n×mn \times m grid of 0/10/1. If ai,j=1a_{i, j} = 1, then position (i,j)(i, j) is an obstacle and cannot be traversed; if ai,j=0a_{i, j} = 0, then position (i,j)(i, j) is passable.

Xiao A and Thrall start at (x1,y1)(x_1, y_1), and the Dreadlord starts at (x2,y2)(x_2, y_2). Xiao A’s goal is to reach the Dreadlord’s position.

Thrall moves according to a specific route, which is a digit string composed of digits 00 to 44.

At second ii:

  • If the ii-th digit (thinking of the digit string repeated infinitely) is 00, Thrall stays still this second.
  • If it is 11, Thrall moves up by one cell this second.
  • If it is 22, Thrall moves down by one cell this second.
  • If it is 33, Thrall moves left by one cell this second.
  • If it is 44, Thrall moves right by one cell this second.

If Thrall’s next position would be a wall or outside the maze, Thrall stays in place. The Dreadlord follows the same rule. (In other words, you can think of the digit string as repeating infinitely; at second ii, take the ii-th character of this infinite string.)

Each second, Xiao A may choose one of the four directions (up, down, left, right) and move one cell in that direction, provided the destination is not a wall and not out of bounds; or Xiao A may stay in place. However, due to the strange environment in the Undercity, Xiao A can remain outside Thrall’s “aura” for at most ss consecutive seconds; if this limit is exceeded, Xiao A dies. The aura has effective radius dd, meaning Xiao A receives Thrall’s aura effect only when the distance (xxx)2+(yyy)2\sqrt{(x-xx)^2+(y-yy)^2} between Xiao A and Thrall is less than or equal to dd. If Xiao A leaves Thrall’s aura and then re-enters the effective area of the aura, the counter resets to zero the next time Xiao A leaves the aura.

Xiao A wants to know the minimum number of seconds needed to reach the Dreadlord’s position.

Input Format

The first line contains four integers n,m,s,dn, m, s, d.

The next nn lines each contain mm digits (00 or 11) without spaces, representing the map of the Undercity.

The next line contains four integers x1,y1,x2,y2x_1, y_1, x_2, y_2 (1x1,x2n1 \le x_1, x_2 \le n, 1y1,y2m1 \le y_1, y_2 \le m).

The next line is a digit string describing Thrall’s movement.

The next line is a digit string describing the Dreadlord’s movement.

Output Format

Output a single integer on one line, the minimum time required.

3 4 7 3
1010
0000
0101
1 2 3 3
0132401
12131

3

Hint

Source: kiro, original.

Constraints: For 100%100\% of the testdata, 1n,m501 \le n, m \le 50, 0s10000 \le s \le 1000, 0d1000 \le d \le 100, 11 \le length of the digit string 100\le 100, 00 \le answer 100\le 100.

Translated by ChatGPT 5