Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package examworm;
- /**
- *
- * @author יורם
- */
- public class ExamWorm {
- /**
- * @param args the command line arguments
- */
- public static void main(String[] args) {
- int mat[][] = {
- { 3, 4 },
- { 50, 5 }
- };
- System.out.println(longestWorm(0,0,0,0,mat));
- }
- public static int longestWorm(int i, int j,int before,int counter, int [][] mat)
- {
- if(j==mat[i].length)
- {
- return counter;
- }
- if(i==mat.length)
- {
- return counter;
- }
- if(j==-1)
- {
- return counter;
- }
- if(i==-1)
- {
- return counter;
- }
- if(counter==0)
- {
- counter=1;
- }
- else
- {
- if(before+1==mat[i][j])
- {
- counter++;
- }
- else
- {
- return counter;
- }
- }
- return max(longestWorm(i+1,j,mat[i][j],counter,mat),longestWorm(i,j+1,mat[i][j],counter,mat),longestWorm(i-1,j,mat[i][j],counter,mat),longestWorm(i,j-1,mat[i][j],counter,mat));
- }
- public static int max(int num1,int num2)
- {
- if(num1>num2)
- {
- return num1;
- }
- return num2;
- }
- public static int max(int n1,int n2,int n3, int n4)
- {
- return max(max(max(n1,n2),n3),n4);
- }
- }
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package examworm;
- /**
- *
- * @author יורם
- */
- public class ExamWorm {
- /**
- * @param args the command line arguments
- */
- public static void main(String[] args) {
- int mat[][] = {
- { 3, 4 },
- { 50, 5 }
- };
- System.out.println(longestWorm(0,0,0,0,mat));
- }
- public static int longestWorm(int i, int j,int before,int counter, int [][] mat)
- {
- if(j==mat[i].length)
- {
- return counter;
- }
- if(i==mat.length)
- {
- return counter;
- }
- if(j==-1)
- {
- return counter;
- }
- if(i==-1)
- {
- return counter;
- }
- if(counter==0)
- {
- counter=1;
- }
- else
- {
- if(before+1==mat[i][j])
- {
- counter++;
- }
- else
- {
- return counter;
- }
- }
- return max(longestWorm(i+1,j,mat[i][j],counter,mat),longestWorm(i,j+1,mat[i][j],counter,mat),longestWorm(i-1,j,mat[i][j],counter,mat),longestWorm(i,j-1,mat[i][j],counter,mat));
- }
- public static int max(int num1,int num2)
- {
- if(num1>num2)
- {
- return num1;
- }
- return num2;
- }
- public static int max(int n1,int n2,int n3, int n4)
- {
- return max(max(max(n1,n2),n3),n4);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement