Advertisement
anas_harby

Untitled

Mar 21st, 2016
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.68 KB | None | 0 0
  1. package eg.edu.alexu.csd.datastructure.iceHockey.cs23;
  2.  
  3. import java.awt.Point;
  4. import eg.edu.alexu.csd.datastructure.iceHockey.IPlayersFinder;
  5.  
  6. public class Hockey implements IPlayersFinder {
  7.  
  8.     int n;
  9.     int m;
  10.     int c_players;
  11.     Point[] players = new Point[2500];
  12.     String[] image = new String[51];
  13.    
  14.     public void process(String[] photo, int team, int threshold, int x, int y) {
  15.        
  16.         Point lowerLeft = new Point(), upperRight = new Point();
  17.         lowerLeft = new Point(x*2,y*2); upperRight = new Point(x*2 + 2, y*2 + 2);
  18.         int area = 2*2;
  19.         Point[] points = new Point[2500];
  20.         int c_points=0;
  21.         points[c_points] = new Point(x,y);     
  22.         c_points++;    
  23.         for(int i=0; i<c_points; i++)
  24.         {
  25.             image[points[i].y] = image[points[i].y].substring(0, points[i].x) + '-' + image[points[i].y].substring(points[i].x+1);  
  26.             Point[] adj = new Point[4];
  27.             adj[0] = new Point(points[i].x +1, points[i].y);
  28.             adj[1] = new Point(points[i].x -1, points[i].y);
  29.             adj[2] = new Point(points[i].x, points[i].y +1);           
  30.             adj[3] = new Point(points[i].x, points[i].y -1);           
  31.             for(int j=0; j<4; j++)
  32.             {
  33.                 boolean f = false;
  34.                 if(adj[j].x <0 || adj[j].y <0 || adj[j].x >= m || adj[j].y >= n || image[adj[j].y].charAt(adj[j].x)!=(char) team + '0')
  35.                     f = true;
  36.                 if(!f)
  37.                 {
  38.                     points[c_points] = adj[j];
  39.                     c_points++;
  40.                     lowerLeft = new Point(Math.min(lowerLeft.x, adj[j].x * 2), Math.min(lowerLeft.y, adj[j].y * 2));
  41.                     upperRight= new Point(Math.max(upperRight.x, (adj[j].x +1)*2), Math.max(upperRight.y, (adj[j].y +1)*2));
  42.                     area+= 2*2;
  43.                 }
  44.             }
  45.         }
  46.         if(area>=threshold)
  47.         {
  48.             Point center = new Point((lowerLeft.x + upperRight.x)/2,(lowerLeft.y + upperRight.y)/2);
  49.                 players[c_players] = center;
  50.                 c_players++;
  51.         }
  52.     }
  53.    
  54.    
  55.     @Override
  56.     public Point[] findPlayers(String[] photo, int team, int threshold) {
  57.         if (threshold==0 || photo.length==0)
  58.         {
  59.             Point players_cp [] = new Point[0];
  60.             return players_cp;
  61.         }
  62.         n = photo.length; m = photo[0].length(); c_players=0;
  63.         java.lang.System.arraycopy(photo, 0, image, 0, photo.length);
  64.         for(int i=0; i<n; i++)
  65.         {
  66.             for(int j=0; j<m; j++)
  67.             {
  68.                 if(image[i].charAt(j) == (char)team + '0')
  69.                     process(image, team, threshold, j, i);
  70.             }
  71.         }
  72.         for(int i=1; i<c_players; i++)
  73.         {
  74.             Point temp = players[i];
  75.             int j = i-1;
  76.             while(j>=0 && temp.x < players[j].x)
  77.             {
  78.                 players[j+1] = players[j];
  79.                 j--;
  80.             }
  81.             while(j>=0 && temp.x == players[j].x && temp.y < players[j].y)
  82.             {
  83.                 players[j+1] = players[j];
  84.                 j--;
  85.             }
  86.             players[j+1] = temp;
  87.         }
  88.         Point players_cp [] = new Point[c_players];
  89.         java.lang.System.arraycopy(players, 0, players_cp, 0, c_players);
  90.         return players_cp;
  91.     }
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement