DCEPC706 - Meeting For Party

no tags 

Ankur, Anuja and Jyoti are planning to have party at some place. Their houses are located at different points in a rectangular grid of size M*N. They want to meet in minimum time and then party.

The M rows and N columns rectangular grid contains some impassable points (denoted by a #), however. So none of them wants to step over these points. They can only step over passable points (denoted by a .). They can also meet at some point outside the grid. You can assume that the points outside the grid are all passable. They cannot party on an impassable point and they have their house only on passable point. They can move either to North, South, East or West passable point from the current passable point and it takes 1 unit time to do so. Also they can wait at a passable point if they want to.

Find the minimum time of meeting.

Note: Assume that they will always meet at some point.

Input

First line gives T, the number of test cases.

Each test case has two space separated integers M and N, the dimensions of the grid.

Next M lines contain N characters per line (no spaces). Characters can be either “#” (impassable) or “.” (passable) or “1” (Ankur’s house) or “2” (Anuja’s House) or “3” (Jyoti’s house). Each test case will have exactly 1 “1”, exactly 1 “2” and exactly 1 “3”.

Output

Output T lines, each containing the required answer.

Constraints

1<=T<=10
1<=M<=200
1<=N<=200

Example

Input:
1
4 4
#...
.2#.
..#3
1..#

Output:
4

hide comments
Ankit Jain: 2013-09-19 16:20:09

My 5th test case is failing....can anybody give some hint regarding that

Animesh Sinha: 2013-08-18 00:16:38

What is the meaning of the input at M=1 and N=1 ?

(Tjandra Satria Gunawan)(曾毅昆): 2013-01-17 01:45:41

phew... finally in first position :-)

npsabari: 2012-10-06 09:57:26

@dce coders: My algo runs in linear time, O(N*M).. I checked with many test cases and runs pretty fast in my comp.. but TLE here! is there an algo with better complexity?
EDIT: made constant optimization.. AC!

Last edit: 2012-10-06 22:23:05
♘Prabhat: 2012-09-18 17:24:54

nice one !!!! enjoyed while solving it....

Rishi Mukherje: 2012-06-21 12:59:41

allow for python please.

Sourabh Singh: 2012-06-06 16:55:03

nice one. thinking case's a bit tricky. coding easy :-)

Last edit: 2012-06-06 16:55:46
.:Ishant Shanu:.: 2012-05-19 18:18:47

@vimal poonia sir
yes answer is 5 for your test case :)

Last edit: 2012-05-22 04:12:29
Vimal poonia: 2012-05-17 20:23:25

@Author,
Please tell me case for WA;
Also is this a correct answer?
4 3
1##
#2.
###
##3

answer=5

Last edit: 2012-05-18 13:34:43

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