Advertisement
Guest User

Untitled

a guest
Jan 30th, 2015
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.22 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4. import java.io.OutputStreamWriter;
  5. import java.io.PrintWriter;
  6.  
  7. public class a {
  8.  
  9. private static class Pair implements Comparable<Pair> {
  10. int f;
  11. int s;
  12.  
  13. Pair(int f, int s) {
  14.  
  15. this.f = f;
  16. this.s = s;
  17. }
  18.  
  19. @Override
  20. public int compareTo(Pair o) {
  21. return (f - o.f);
  22. }
  23.  
  24. }
  25. public static int n;
  26. public static int m;
  27. public static boolean visited[][];
  28. public static long dp[][];
  29.  
  30. public static long sol(int i,int j){
  31. if(i>n||j>m){
  32. return 0;
  33. }else if(i==n&&j==m){
  34. return 1;
  35. }else if(dp[i][j]!=-1){
  36. return dp[i][j];
  37. }else{
  38. long choose1=0;
  39. long choose2=0;
  40. if((visited[i][j]&&visited[i+1][j])==false){
  41. choose1=sol(i+1,j);
  42. }
  43. if((visited[i][j]&&visited[i][j+1])==false){
  44. choose2=sol(i,j+1);
  45. }
  46. return dp[i][j]=choose1+choose2;
  47. }
  48.  
  49. }
  50. public static long numWays(int width, int height, String[] bad){
  51. n=height;
  52. m=width;
  53. visited=new boolean[n+2][m+2];
  54. for (int i = 0; i < bad.length; i++) {
  55. String s[]=bad[i].split(" ");
  56. visited[Integer.parseInt(s[1])][Integer.parseInt(s[0])]=true;
  57. visited[Integer.parseInt(s[3])][Integer.parseInt(s[2])]=true;
  58.  
  59. }
  60. dp = new long[n+5][m+5];
  61. for (int i = 0; i < n+5; i++) {
  62. for (int j = 0; j < m+5; j++) {
  63. dp[i][j]=-1;
  64. }
  65. }
  66. return sol(0,0);
  67.  
  68.  
  69. }
  70.  
  71. public static void main(String[] args) throws IOException {
  72. // BufferedReader in = new BufferedReader(new
  73. // InputStreamReader(System.in));
  74. // PrintWriter out = new PrintWriter(new
  75. // OutputStreamWriter(System.out));
  76. // String s[] = in.readLine().split(" ");
  77. // int[] sequence = new int[s.length];
  78. // for (int i = 0; i < s.length; i++) {
  79. // sequence[i] = Integer.parseInt(s[i]);
  80. // }
  81. // out.println(longestZigZag(sequence));
  82. // out.close();
  83.  
  84. String t[]={"0 0 0 1","6 6 5 6"};
  85. System.out.println(numWays(6, 6, t));
  86. String a[] = { };
  87. System.out.println(numWays(1, 1, a));
  88. String b[] = { };
  89. System.out.println(numWays(35, 31, b));
  90. String c[] = {"0 0 1 0", "1 2 2 2", "1 1 2 1"};
  91. System.out.println(numWays(2, 2, c));
  92.  
  93.  
  94.  
  95. }
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement