DCEPC701 - Amazing Maze

no tags 

Chintu and his father Nikka, on their vacations, went to The Amazing Maze. But unfortunately, Chintu is has been separated from his father. His father Nikka has to find him as soon as possible in the maze or else Chintu will start crying soon.

The Amazing Maze is actually pretty amazing! The maze is built in a rectangular grid fashion. There are walls (#) and walking cells (.) in the grid. One can only walk through walking cells and so cannot jump off the wall. The amazing part is that each wall becomes a walking cell at some point of time (Neglect the transition time). Alternatively one can say that at some time t1 units, the wall will become walking cell and so one can step on to it at t1 time and at any time after t1. One can only move to adjacent walking cells from a walking cell. Adjacent cells mean the immediate up, down, right and left walking cells. It takes 1 unit of time to go from current cell to any adjacent cell. One can also wait on a particular walking cell for an adjacent wall to become a walking cell.

Nikka has the map of the maze with the information of the time at which the walls becomes walking cells. Also he knows at which walking cell he is situated and at which walking cell Chintu is situated. Help Nikka find out the minimum time required to reach to his son Chintu.

Input

First line contains T, the number of test cases.

Each case begins with two integers, M and N, the dimensions of maze.

Next contains M lines each containing N characters. Each character is either a “#” or a “.” as described above. This maze is the snapshot at time t=0.

Next M lines contain N space separated non negative integers. For each “.”, there will be a 0 and for each “#”, there will be a positive integer which represent the time at which that wall (#) will become a walkable cell.

Next line contains x1 and y1 denoting the position of Nikka in the maze.

Next line contains x2 and y2 denoting the position of Chintu in the maze.

Output

Output T lines each containing an integer, the minimum time required for Nikka to reach Chintu.

Constraints

1<=T<=10
1<=M<=200
1<=N<=200
0<=x1, x2 0<=y1, y2

For all #’s, time “t” will be > 0 and <= 10^4.

Example

Input:
2
1 5
..#..
0 0 1 0 0
0 0
0 4
3 3
.##
##.
##.
0 1 2
3 4 0
6 7 0
0 0
2 2

Output:
4
4

hide comments
Aditya Gourav: 2013-03-25 20:40:39

yipee, finally,
great relief
keyword "time" cost me 4 compilation errors!!

Last edit: 2013-03-26 08:45:20
technophyle: 2013-01-02 16:48:35

WA :( on 5th case

Last edit: 2013-01-02 16:57:43
Archit Mittal: 2012-08-27 16:05:46

good one :)

Last edit: 2012-08-28 11:13:13
Nehal J Wani: 2012-07-18 17:15:29

@DCE Coders: I am too having problem with the 5th test case. Please check submission id 7335889. I can't seem to understand where am I going wrong.

Muralidhar Reddy: 2012-06-17 07:48:10

@DCE Coders: Why am I getting WA. I am using Dijikstra's algo. I got WA at 5th test case. id is 7165743. Oh god what is the fifth test case which I am failing :@

Last edit: 2012-06-17 20:02:36

Added by:dce coders
Date:2012-04-30
Time limit:1s
Source limit:50000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Languages:ASM32-GCC MAWK BC C-CLANG C NCSHARP C++ 4.3.2 CPP CPP14 CPP14-CLANG COBOL COFFEE D-CLANG D-DMD DART ELIXIR FANTOM FORTH GOSU GRV JAVA JS-MONKEY JULIA KTLN NIM NODEJS OBJC OBJC-CLANG OCT PICO PROLOG PYPY PYPY3 PY_NBC R RACKET RUST CHICKEN SQLITE SWIFT UNLAMBDA VB.NET
Resource:Own Problem