Advertisement
What_Ever

Untitled

Mar 19th, 2016
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.15 KB | None | 0 0
  1. package eg.edu.alexu.csd.datastructure.iceHockey.cs69;
  2.  
  3. import java.awt.Point;
  4.  
  5. import eg.edu.alexu.csd.datastructure.iceHockey.IPlayersFinder;
  6.  
  7. public class IceHockey implements IPlayersFinder {
  8.     Point[] p = new Point[10000];
  9.     String[] pic;
  10.     int n,len,strlen,maxX=-1,maxY=-1,minX=99999,minY=99999;
  11.     boolean[][] visited;
  12.     int comp = -1,pSize= 0;
  13.     @Override
  14.     public Point[] findPlayers(String[] photo, int team, int threshold) {
  15.         pSize = 0;
  16.         pic = photo;
  17.         if(photo == null )return new Point[0];
  18.         if(photo.length == 0)return new Point[0];
  19.         if(photo[0] == null)return new Point[0];
  20.         if(photo[0].length() == 0)return new Point[0];
  21.         n = team;
  22.         len = photo.length;
  23.         strlen = photo[0].length();
  24.         visited = new boolean[photo.length][photo[0].length()];
  25.         for(int i = 0 ; i < photo.length ;i++)
  26.         {
  27.             for(int j = 0 ; j < photo[0].length() ; j++)
  28.             {
  29.                 comp = -1;
  30.                 maxX = -1;
  31.                 maxY = -1;
  32.                 minX = 99999;
  33.                 minY = 99999;
  34.                 if(!visited[i][j])
  35.                 {
  36.                     if(photo[i].charAt(j) == team + '0')
  37.                     {
  38.                         comp = 0;
  39.                         solve(j,i);
  40.                     }
  41.                     if(comp * 4 >= threshold)
  42.                     {
  43.                         p[pSize++] = new Point(minX + maxX + 1,minY + maxY + 1);
  44.                     }
  45.                 }
  46.             }
  47.         }
  48.         Point[] points = new Point[pSize];
  49.         for(int i = 0 ; i < pSize ; i++)points[i] = p[i];
  50.         boolean swapped = true;
  51.         Point temp;
  52.         for(int i = 0 ; i < pSize-1 ; i++)
  53.         {
  54.             if(!swapped)break;
  55.             for(int j = i+1 ; j < pSize ; j++)
  56.             {
  57.                 if(points[j].x < points[i].x)
  58.                 {
  59.                     temp = points[i];
  60.                     points[i] = points[j];
  61.                     points[j] = temp;
  62.                 }
  63.                 else if(points[j].x == points[i].x)
  64.                 {
  65.                     if(points[j].y < points[i].y)
  66.                     {
  67.                         temp = points[i];
  68.                         points[i] = points[j];
  69.                         points[j] = temp;
  70.                     }
  71.                 }
  72.             }
  73.         }
  74.         return points;
  75.     }
  76.     public  void solve(int x,int y)
  77.     {
  78.         if(x == strlen || y == len || x < 0 || y < 0 )return;
  79.         if(!visited[y][x] && pic[y].charAt(x) == n +'0')
  80.         {
  81.             maxY = Math.max(maxY, y);
  82.             maxX = Math.max(maxX, x);
  83.             minX = Math.min(minX, x);
  84.             minY = Math.min(minY, y);
  85.             visited[y][x] = true;
  86.             comp++;
  87.             solve(x+1,y);
  88.             solve(x,y+1);
  89.             solve(x-1,y);
  90.             solve(x,y-1);
  91.         }
  92.     }
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement