Walking on a Grid

You will be given a square grid of size N × N . The top-left square has a coordinate of (1, 1) and that of bottom-right is (N,N). Your job is to walk from (1, 1) to (N,N). Very easy, right? Thats why you have to follow some rules when walking.

  1. You can only move left, right or down.
  2. (i,j−1)isleftof(i,j),(i,j+1)isrightof(i,j)and(i+1,j)isdownof(i,j). 3. You can never move outside the grid.
  3. You can not step on a cell more than once.
  4. Every cell has an integer associated with it.
  5. You have to make sure the sum of integers of the path is maximized.
  6. You can step on at most k negative integers from source to destination. Input Each case will start with two integers N and k. N ≤ 75 and k ≤ 5. Each of the next N lines will contain N integers each given in row major order. That is, the first integer of the first row is (1, 1) and the last integer of last row is (N,N). Input terminates with two zeros on a line. Output For every case output the case number. If it’s not possible to reach the destination meeting the above rules then output ‘impossible’, else print the maximum sum of integers of the path. Sample Input 41 1 2 3 -5 -10 6 0 -1 -10 -10 -10 2 0001 40 1 2 3 -5 -10 6 0 -1 -10 -10 -10 2 0001 00 Sample Output Case 1: 11 Case 2: impossible