Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package eg.edu.alexu.csd.datastructure.iceHockey.cs23;
- import java.awt.Point;
- import eg.edu.alexu.csd.datastructure.iceHockey.IPlayersFinder;
- public class Hockey implements IPlayersFinder {
- int n;
- int m;
- int c_players;
- Point[] players = new Point[2500];
- String[] image = new String[51];
- public void process(String[] photo, int team, int threshold, int x, int y) {
- Point lowerLeft = new Point(), upperRight = new Point();
- lowerLeft = new Point(x*2,y*2); upperRight = new Point(x*2 + 2, y*2 + 2);
- int area = 2*2;
- Point[] points = new Point[2500];
- int c_points=0;
- points[c_points] = new Point(x,y);
- c_points++;
- for(int i=0; i<c_points; i++)
- {
- image[points[i].y] = image[points[i].y].substring(0, points[i].x) + '-' + image[points[i].y].substring(points[i].x+1);
- Point[] adj = new Point[4];
- adj[0] = new Point(points[i].x +1, points[i].y);
- adj[1] = new Point(points[i].x -1, points[i].y);
- adj[2] = new Point(points[i].x, points[i].y +1);
- adj[3] = new Point(points[i].x, points[i].y -1);
- for(int j=0; j<4; j++)
- {
- boolean f = false;
- 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')
- f = true;
- if(!f)
- {
- points[c_points] = adj[j];
- c_points++;
- lowerLeft = new Point(Math.min(lowerLeft.x, adj[j].x * 2), Math.min(lowerLeft.y, adj[j].y * 2));
- upperRight= new Point(Math.max(upperRight.x, (adj[j].x +1)*2), Math.max(upperRight.y, (adj[j].y +1)*2));
- area+= 2*2;
- }
- }
- }
- if(area>=threshold)
- {
- Point center = new Point((lowerLeft.x + upperRight.x)/2,(lowerLeft.y + upperRight.y)/2);
- players[c_players] = center;
- c_players++;
- }
- }
- @Override
- public Point[] findPlayers(String[] photo, int team, int threshold) {
- if (threshold==0 || photo.length==0)
- {
- Point players_cp [] = new Point[0];
- return players_cp;
- }
- n = photo.length; m = photo[0].length(); c_players=0;
- java.lang.System.arraycopy(photo, 0, image, 0, photo.length);
- for(int i=0; i<n; i++)
- {
- for(int j=0; j<m; j++)
- {
- if(image[i].charAt(j) == (char)team + '0')
- process(image, team, threshold, j, i);
- }
- }
- for(int i=1; i<c_players; i++)
- {
- Point temp = players[i];
- int j = i-1;
- while(j>=0 && temp.x < players[j].x)
- {
- players[j+1] = players[j];
- j--;
- }
- while(j>=0 && temp.x == players[j].x && temp.y < players[j].y)
- {
- players[j+1] = players[j];
- j--;
- }
- players[j+1] = temp;
- }
- Point players_cp [] = new Point[c_players];
- java.lang.System.arraycopy(players, 0, players_cp, 0, c_players);
- return players_cp;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement