Maximum value path in matrix

1.8

19 votes
Basic Programming, Matrix, Probablity, Greedy Algorithms, Recursion, Graph, Recursion and Backtracking
Problem

Note: This is an approximate problem. There is no exact solution. You must find the most optimal solution. Please, take a look at the sample explanation to ensure that you understand the problem correctly.

Problem statement

You are given an integer matrix A of size N×N. You can start at any cell of the matrix. You can walk in four directions (left, right, up, and down). You can not visit the same cell twice. Here, B1,B2,,Bk denote the values of cells of the path in order of bypass. The value of the path is equal to ki=1Bii. Your task is to find the path with maximum value.

Input format

  • First line contains two space-separated integers N (1N1000)
  • Each of the next N lines contains N space-separated integers Ai,j (1Ai,j109)

Output format

X (1XNN) is the length of path that you found. The format of the output is as follows:

  • In the first line, print X denoting the length of path.
  • In each of the X lines, print two numbers denoting the coordinates of cell of the path.

Verdict and scoring

Your score is equal to the sum of the values of all test cases. The value of each test is equal to the value of path that you found.

If at least one of the rules will be violated, then your value for this test will be 0.

Test generation plan

  • In 10% tests: (1N6) denoting the elements of matrix A are generated randomly.
  • In 30% tests: (1N1000) denoting the elements of matrix A are a random permutation of size NN (elements from 1 to NN).
  • In 60% tests: (1N1000) denoting the elements of matrix A are generated randomly.
Time Limit: 5
Memory Limit: 256
Source Limit:
Explanation

The length of optimal path is 9. The value of path will be 314:

A1,31+A1,22+A1,13+A2,14+A3,15+A3,26+A3,37+A2,38+A2,29=314.

Editor Image

?