Computer Science, asked by asingh6be19, 8 months ago

Reverse Spiral Traversal of Sparse Matrix :

Traverse a Sparse matrix in reverse spiral order. The Sparse matrix is represented in the form of a triplet [rowindex,columnindex,value] following row-major order. The starting point for traversal, i.e. the row index and the column index is given as an input. The sequence of directions to be followed from the given starting point is Down (1), Right (2), Up (3), and then Left (4).

The output should be in the form of a quadruplet with rows arranged in the required order of traversal. The first three columns of this quadruplet are similar to those as given in the input triplet. The last column includes the direction information which is followed when the corresponding element is traversed. The allowed values in the last column are:

1: If element is traversed while moving Down.
2: If element is traversed while moving Right.
3: If element is traversed while moving Up.
4: If element is traversed while moving Left.
Input:
Line 1 contains three space separated integers, P, Q, and N, where P is the first and Q is the second dimension of a Sparse matrix. N is the total number of non-zero entries in the Sparse matrix.
Each of the following N lines contains three space separated integers representing a single row of the triplet, i.e. rowindex, columnindex, and value.
Last line contains two space separated integers, I and J, respectively giving the starting row and column indices for reverse spiral traversal of the Sparse matrix.
Output:
Each row of the quadruplet is contained in N separate lines. Each line in the output has four space separated integers in the format as is described above.
Constraints
All integers range in between 1 and 1000.
Sample Input:
4 4 5
0 0 8
1 1 6
1 2 5
2 3 9
3 2 7
1 1
Sample Output:
1 1 6 1
1 2 5 3
0 0 8 4
3 2 7 2
2 3 9 3
EXPLANATION:
We have to start from [1][1] and direction to be followed is Down (1), so the first entry should be 1 1 6 1. Next entries traversed in the reverse spiral order are at indices [2][1] and [2][2], but corresponding triplet entries are missing so no output is generated. Then 5 comes at [1][2] while moving Up (3). Triplet entries for [0][2] and [0][1] are again missing. Then at [0][0] element 8 comes while moving Left (4). Again missing triplet entries for [1][0], [2][0], [3][0], and [3][1] generate no output. Element 7 is visited when moving Right (2) at index [3][2]. Triplet entry for [3][3] is missing. Lastly we found element 9 at [2][3] when moving Up (3). Rest of the triplet entries are missing, i.e. [1][3] and [0][3]. This completes the reverse spiral traversal.

Answers

Answered by geetamourya1998
0

Answer:

Traverse a Sparse matrix in reverse spiral order. The Sparse matrix is represented in the form of a triplet [rocolumindex,valualue] following row-major order. The starting point for traversal, i.e. the row index and the column index is given as an input. The sequence of directions to be followed from the given starting point is Dow (3)(3), and then L (4)(4).

The output should be in the form of a quadruplet with rows arranged in the required order of traversal. The first three columns of this quadruplet are similar to those as given in the input triplet. The last column includes the direction information which is followed when the corresponding element is traversed. The allowed values in the last column are:

1: If element is traversed while moving

2: If element is traversed while moving Rt.

3: If element is traversed while moving U

4: If element is traversed while moving Left.

Input:

Line 1 contains three space separated integers, PP, QQ, and NN, where PP is the first and QQ is the second dimension of a Sparse matrix. NN is the total number of non-zero entries in the Sparse matrix.

Each of the following NN lines contains three space separated integers representing single row of the triplet,x, and v

Last line contains two space separated integers, II and JJ, respectively giving the starting row and column indices for reverse spiral traversal of the Sparse matrix.

Output:

Each row of the quadruplet is contained in NN separate lines. Each line in the output has four space separated integers in the format as is described above.

Constraints

All integers range in between 1 and 1000.  

Sample Input:

4 4 5

0 0 8

1 1 6

1 2 5

2 3 9

3 2 7

1 1

Sample Output:

1 1 6 1

1 2 5 3

0 0 8 4

3 2 7 2

2 3 9 3

EXPLANATION:

We have to start from [1][1][1][1] and direction to be followed is (1)(1), so the first entry should be 11 11 66 11. Next entries traversed in the reverse spiral order are at indices [2][1][2][1] and [2][2][2][2], but corresponding triplet entries are missing so no output is generated. Then 55 comes at [1][2][1][2] while moving (3)(3). Triplet entries for [0][2][0][2] and [0][1][0][1] are again missing. Then at [0][0][0][0] element 88 comes while moving (4)(4). Again missing triplet entries for [1][0][1][0], [2][0][2][0], [3][0][3][0], and [3][1][3][1] generate no output. Element 77 is visited when moving (2)(2) at index [3][2][3][2]. Triplet entry for [3][3][3][3] is missing. Lastly we found element 99 at [2][3][2][3] when moving (3)(3). Rest of the triplet entries are missing, i.e. [1][3][1][3] and [0][3][0][3]. This completes the reverse spiral t

Thank_ you ❤

PLEASE MARK ME AS BRAINLEAST

Similar questions