-4

Plz help me with this question i am unable to do it

Given a 5 x 5 Grid comprising of tiles numbered from 1 to 25 and a set of 5 start-end point pairs. For each pair,find a path from the start point to the end point. The paths should meet the below conditions:
a) Only Horizontal and Vertical moves allowed.
b) No two paths should overlap.
c) Paths should cover the entire grid

Input:
Input consist of 5 lines. Each line contains two space-separated integers,Starting and Ending point.

Output:
Print 5 lines. Each line consisting of space-separated integers,the path for the corresponding start-end pair. Assume that such a path Always exists. In case of Multiple Solution,print any one of them.

Sample Input

1 22 
4 17 
5 18 
9 13 
20 23 

Sample Output

1 6 11 16 21 22 
4 3 2 7 12 17 
5 10 15 14 19 18 
9 8 13 
20 25 24 23
Pshemo
  • 122,468
  • 25
  • 185
  • 269
DiscoveringNew
  • 73
  • 2
  • 13
  • uhm... can you show us a drawing of whats supposed to happen? – Tschallacka Jun 29 '15 at 11:30
  • 1
    duplicate of [on hold] thread http://stackoverflow.com/questions/31096566/all-paths-between-two-nodes-of-an-grid-structure-using-java – Sharon Ben Asher Jun 29 '15 at 11:31
  • 1
    and of http://stackoverflow.com/questions/31082704/java-non-overlapping-paths-between-n-start-and-n-end-points – Sharon Ben Asher Jun 29 '15 at 11:32
  • 1
    and of http://stackoverflow.com/questions/31098661/2d-matrix-statement-for-5-x-5-grid – Sharon Ben Asher Jun 29 '15 at 11:32
  • Your question is very broad and unclear. You need to describe in more detail what kind of help you need, what part of this assignment is unclear and [what have you tried to solve it](http://mattgemmell.com/what-have-you-tried/). – Pshemo Jun 29 '15 at 11:37

1 Answers1

0

I have the logic of the solution. You will need to do the coding yourself (or at least TRY to do it, I can further help if you get stuck, but I will not post complete code!)

The trick is to find a start-end pair where both are on "edges" of the matrix.
For example, the pairs 1 22 and 20 23 are such pairs. Once you find such a pair (any one will do) you plot the path along the edge/s.

Then, the edges of the matrix need to be re-calculated to reflect the plotted path so once you plotted 1 22 numbers 7,12,17 becomes edge numbers (since numbers 6,11,16 were removed)

After that, you will see that the pair 4 17 becomes "on edge" and the process can repeat until all pairs are accounted for.

EDIT: After dry running on the pair 5 18 I see that "being on edge" includes diagonal position:

xxxxX
xxxxx
xxxAx
xxxx*
xx***

This is the matrix status after plotting pair 20 23. Position A is "on the edge" because its south-east (bottom right) corner is now outside the matrix.

Sharon Ben Asher
  • 13,849
  • 5
  • 33
  • 47
  • but the matrix won't be regular everytime?? – DiscoveringNew Jun 29 '15 at 13:00
  • yes sorry i mean it will be jagged, and i have to find edges in it – DiscoveringNew Jun 29 '15 at 13:06
  • yes, it wil be jagged, your "find edge" algorithm will have to be updated as you plot pairs. Also, dry run on the pair `5 18` and you will see that being on edge includes diagonal position (see edited answer) – Sharon Ben Asher Jun 29 '15 at 13:17
  • just stumbled upon yet another duplicate, this one contains multiple answers and much more info http://stackoverflow.com/questions/31080969/approximation-algorithm-for-non-intersecting-paths-in-a-grid – Sharon Ben Asher Jun 29 '15 at 14:01