Advertisement
Guest User

solutionWorm

a guest
Aug 28th, 2014
201
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.40 KB | None | 0 0
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6.  
  7. package examworm;
  8.  
  9. /**
  10.  *
  11.  * @author יורם
  12.  */
  13. public class ExamWorm {
  14.  
  15.     /**
  16.      * @param args the command line arguments
  17.      */
  18.     public static void main(String[] args) {
  19.        int mat[][] = {
  20.                         { 3, 4  },
  21.                         { 50, 5 }
  22.                         };
  23.          System.out.println(longestWorm(0,0,0,0,mat));
  24.     }
  25.    
  26.      public static int longestWorm(int i, int j,int before,int counter, int [][] mat)
  27.      {
  28.          if(j==mat[i].length)
  29.          {
  30.              return counter;
  31.          }
  32.          if(i==mat.length)
  33.          {
  34.              return counter;
  35.          }
  36.          if(j==-1)
  37.          {
  38.              return counter;
  39.          }
  40.          if(i==-1)
  41.          {
  42.              return counter;
  43.          }
  44.         if(counter==0)
  45.         {
  46.             counter=1;
  47.         }
  48.         else
  49.         {
  50.             if(before+1==mat[i][j])
  51.             {
  52.                 counter++;
  53.             }
  54.             else
  55.             {
  56.                 return counter;
  57.             }
  58.         }
  59.        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));
  60.      }
  61.      public static int max(int num1,int num2)
  62.      {
  63.        if(num1>num2)
  64.        {
  65.            return num1;
  66.        }
  67.        return num2;  
  68.      }
  69.      public static int max(int n1,int n2,int n3, int n4)
  70.      {
  71.          return max(max(max(n1,n2),n3),n4);
  72.      }
  73. }
  74. /*
  75.  * To change this license header, choose License Headers in Project Properties.
  76.  * To change this template file, choose Tools | Templates
  77.  * and open the template in the editor.
  78.  */
  79.  
  80. package examworm;
  81.  
  82. /**
  83.  *
  84.  * @author יורם
  85.  */
  86. public class ExamWorm {
  87.  
  88.     /**
  89.      * @param args the command line arguments
  90.      */
  91.     public static void main(String[] args) {
  92.        int mat[][] = {
  93.                         { 3, 4  },
  94.                         { 50, 5 }
  95.                         };
  96.          System.out.println(longestWorm(0,0,0,0,mat));
  97.     }
  98.    
  99.      public static int longestWorm(int i, int j,int before,int counter, int [][] mat)
  100.      {
  101.          if(j==mat[i].length)
  102.          {
  103.              return counter;
  104.          }
  105.          if(i==mat.length)
  106.          {
  107.              return counter;
  108.          }
  109.          if(j==-1)
  110.          {
  111.              return counter;
  112.          }
  113.          if(i==-1)
  114.          {
  115.              return counter;
  116.          }
  117.         if(counter==0)
  118.         {
  119.             counter=1;
  120.         }
  121.         else
  122.         {
  123.             if(before+1==mat[i][j])
  124.             {
  125.                 counter++;
  126.             }
  127.             else
  128.             {
  129.                 return counter;
  130.             }
  131.         }
  132.        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));
  133.      }
  134.      public static int max(int num1,int num2)
  135.      {
  136.        if(num1>num2)
  137.        {
  138.            return num1;
  139.        }
  140.        return num2;  
  141.      }
  142.      public static int max(int n1,int n2,int n3, int n4)
  143.      {
  144.          return max(max(max(n1,n2),n3),n4);
  145.      }
  146. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement