Guest User

Highway Bypass

a guest
Feb 11th, 2018
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.12 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4. import java.util.Arrays;
  5.  
  6. /**
  7.  * Created by bugkiller on 11/02/18.
  8.  */
  9. class HighwayBypass {
  10.  
  11.     static int a[][] = new int[300][300];
  12.     static int dp[][] = new int[300][300];
  13.     static final int M = 20011;
  14.     public static void main(String[] args) throws IOException {
  15.         BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  16.         int r, c, d;
  17.         String[] s;
  18.         s = br.readLine().split("\\s");
  19.         r = Integer.parseInt(s[0]);
  20.         c = Integer.parseInt(s[1]);
  21.         d = Integer.parseInt(s[2]);
  22.         for (int i = 0; i < r; i++) {
  23.             s = br.readLine().split("\\s");
  24.             for (int j = 0; j < c; j++) {
  25.                 a[i][j] = Integer.parseInt(s[j]);
  26.             }
  27.         }
  28.         System.out.println(solve(a, r, c, d));
  29.         for (int i = 0; i < r; i++) {
  30.             for (int j = 0; j < c; j++) {
  31.                 System.out.print(dp[i][j]+" ");
  32.             }
  33.             System.out.println();
  34.         }
  35.     }
  36.  
  37.     private static int solve(int[][] a, int r, int c, int d) {
  38.         for (int i = 0; i < r; i++) {
  39.             Arrays.fill(dp[i], -1);
  40.         }
  41.         return totalPathBottomUp(a,r-1,c-1,d);
  42.     }
  43.  
  44.     private static int totalPathBottomUp(int[][] a, int r, int c, int d) {
  45.         for (int i = 0; i <= r; i++) {
  46.             for (int j = 0; j <= c; j++) {
  47.                 if (a[i][j] == 0) {
  48.                     dp[i][j] = 0;
  49.                 } else if (i == 0 && j == 0) {
  50.                     dp[i][j] = 0;
  51.                 } else if (i == 0 ) {
  52.                     if ( j <= d)
  53.                         dp[i][j] = 1;
  54.                     else dp[i][j] = 0;
  55.                 } else if (j == 0) {
  56.                     if (i <= d)
  57.                         dp[i][j] = 1;
  58.                     else dp[i][j] = 0;
  59.                 }  else {
  60.                     dp[i][j] = dp[i - 1][j] + dp[i][j - 1];
  61.                     if (i - d - 1 >= 0) {
  62.                         dp[i][j] -= dp[i - d - 1][j];
  63.                     }
  64.                     if (j - d - 1 >= 0) {
  65.                         dp[i][j] -= dp[i][j - d - 1];
  66.                     }
  67.                 }
  68.             }
  69.         }
  70.         return dp[r][c];
  71.     }
  72.     private static int totalPath(int[][] a, int r, int c, int d) {
  73.         if (r == 0 && c == 0)
  74.             return 0;
  75.         if (r >= 0 && c >= 0) {
  76.             if (a[r][c]==0)
  77.                 return 0;
  78.             if (r == 0) {
  79.                 if (c <= d) return 1;
  80.                 else return 0;
  81.             }
  82.             if (c == 0) {
  83.                 if (r <= d) return 1;
  84.                 else return 0;
  85.             }
  86.  
  87.             if (dp[r][c]!=-1)
  88.                 return dp[r][c];
  89.  
  90.             if (a[r][c] == 1) {
  91.                 dp[r][c] = ((totalPath(a, r - 1, c, d) % M) + (totalPath(a, r, c - 1, d) % M)
  92.                         -(totalPath(a, r - d - 1, c, d) % M) - (totalPath(a, r, c - d - 1, d) % M)) % M;
  93.             } else dp[r][c] = 0;
  94.  
  95.             return dp[r][c];
  96.         }
  97.         return 0;
  98.     }
  99. }
Add Comment
Please, Sign In to add comment