Block Drop

All submissions for this problem are available.

There is a square pool divided into NxM cells. In some cells of the pool there stone islands. Each island consists of some number of stones. Let's call this number the height of the island. You can jump from the island with coordinated (x,y) to any island with coordinates (x+1,y), (x+2,y), (x-1,y), (x-2,y), (x,y+1), (x,y+2), (x,y-1), (x,y-2). When you jump off the island its height goes down by one. If the height of any island becomes 0 it goes under water and you can't jump on it any more. You start on island with coordinates (sx, sy). The goal is to make all the islands (except the final one) go under water and finish on the island with coordinates (fx, fy) which should have height equal to 1 when you finish on it. You task is to count the number of different ways to achieve the goal.

Input

The first line of input file contains number t - the number of test cases (no more than 5 for each file). Then the description of each test case follows. The first line of each test case contains numbers N and M. The next line contains two coordinates sx and sy of the start cell. After that there are two coordinates fx and fy of the finishing cell. Then N lines follow each consisting of M integers denoting the heights of the islands in each cell of the pool. The height 0 means that there is no island in the cell. Note also that each test case in the official tests will be generated by the following procedure. The dimensions of the pool will be chosen randomly and uniformly: N and M will be from 3 to 8 inclusive. Then the final cell will be chosen randomly: fx will be from 1 to N and fy will be from 1 to M. The height of island in the cell (fx, fy) will be set to 1. Then the number of jumps will chosen randomly from 15 to 25. Then this many random valid jumps will be performed adding one stone to the cell the jumps lands on. The cell on which we land after performing all the jumps will be proclaimed as the initial cell: (sx, sy).

Output

For each test case print the total number of different ways to solve the task.

There is an error in the input statement it says "The first line of each test case contains number N" .It should be"The first line of each test case contains numbers N and M".
Then "Then N lines follow each consisting of N integers denoting the heights of the islands in each cell of the pool." should be"Then N*M lines follow each consisting of N*M integers denoting the heights of the islands in each cell of the pool."

Please clarify ..
1
3 3
3 1
3 1
1 0 0
3 1 1
3 1 1
if (3,1) height is only one and it is start and finish. A single jump will make the end point to have 0 height. In the input
1 0 0
3 1 1
3 1 1 where is the origin located??

@maverick_dp I believe you are confusing (x, y) with the x / y - axes. Please consider (x, y) to be the co-ordinate at the x'th row and y'th column! So number if stones at (3, 1) in the above case is 3.

taking bottom-left corner to be origin. Isn't number of stones at (3,1) one(1). Also, it clearly states in the question that. height (Fx,Fy) already set to 1. But the problem is since Sx,Sy is also (3,1). Wouldn't it make it 0 on the first jump.

@pr0ton probably your definition of naive and my definition differ as I don't see any possible optimizations for my naive solution so that it doesn't get TLEd. Let me give it a couple of more attempts and not give up yet.

@Shilp
i m getting answers correctly... i hav even tested test case.. i gives right output.. i hav reduced the complexity of the porgram... but it's still showing time limit exceeded... enlighten me... :)

CodeChef was created as a platform to help programmers make it big in the world of algorithms, computer programming and programming contests. At CodeChef we work hard to revive the geek in you by hosting a programming contest at the start of the month and another smaller programming challenge in the middle of the month. We also aim to have training sessions and discussions related to algorithms, binary search, technicalities like array size and the likes. Apart from providing a platform for programming competitions, CodeChef also has various algorithm tutorials and forum discussions to help those who are new to the world of computer programming.

Try your hand at one of our many practice problems and submit your solution in a language of your choice. Our programming contest judge accepts solutions in over 35+ programming languages. Preparing for coding contests were never this much fun! Receive points, and move up through the CodeChef ranks. Use our practice section to better prepare yourself for the multiple programming challenges that take place through-out the month on CodeChef.

Here is where you can show off your computer programming skills. Take part in our 10 day long monthly coding contest and the shorter format Cook-off coding contest. Put yourself up for recognition and win great prizes. Our programming contests have prizes worth up to INR 20,000 (for Indian Community), $700 (for Global Community) and lots more CodeChef goodies up for grabs.