Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.io.OutputStreamWriter;
- import java.io.PrintWriter;
- public class a {
- private static class Pair implements Comparable<Pair> {
- int f;
- int s;
- Pair(int f, int s) {
- this.f = f;
- this.s = s;
- }
- @Override
- public int compareTo(Pair o) {
- return (f - o.f);
- }
- }
- public static int n;
- public static int m;
- public static boolean visited[][];
- public static long dp[][];
- public static long sol(int i,int j){
- if(i>n||j>m){
- return 0;
- }else if(i==n&&j==m){
- return 1;
- }else if(dp[i][j]!=-1){
- return dp[i][j];
- }else{
- long choose1=0;
- long choose2=0;
- if((visited[i][j]&&visited[i+1][j])==false){
- choose1=sol(i+1,j);
- }
- if((visited[i][j]&&visited[i][j+1])==false){
- choose2=sol(i,j+1);
- }
- return dp[i][j]=choose1+choose2;
- }
- }
- public static long numWays(int width, int height, String[] bad){
- n=height;
- m=width;
- visited=new boolean[n+2][m+2];
- for (int i = 0; i < bad.length; i++) {
- String s[]=bad[i].split(" ");
- visited[Integer.parseInt(s[1])][Integer.parseInt(s[0])]=true;
- visited[Integer.parseInt(s[3])][Integer.parseInt(s[2])]=true;
- }
- dp = new long[n+5][m+5];
- for (int i = 0; i < n+5; i++) {
- for (int j = 0; j < m+5; j++) {
- dp[i][j]=-1;
- }
- }
- return sol(0,0);
- }
- public static void main(String[] args) throws IOException {
- // BufferedReader in = new BufferedReader(new
- // InputStreamReader(System.in));
- // PrintWriter out = new PrintWriter(new
- // OutputStreamWriter(System.out));
- // String s[] = in.readLine().split(" ");
- // int[] sequence = new int[s.length];
- // for (int i = 0; i < s.length; i++) {
- // sequence[i] = Integer.parseInt(s[i]);
- // }
- // out.println(longestZigZag(sequence));
- // out.close();
- String t[]={"0 0 0 1","6 6 5 6"};
- System.out.println(numWays(6, 6, t));
- String a[] = { };
- System.out.println(numWays(1, 1, a));
- String b[] = { };
- System.out.println(numWays(35, 31, b));
- String c[] = {"0 0 1 0", "1 2 2 2", "1 1 2 1"};
- System.out.println(numWays(2, 2, c));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement