Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package eg.edu.alexu.csd.datastructure.iceHockey.cs69;
- import java.awt.Point;
- import eg.edu.alexu.csd.datastructure.iceHockey.IPlayersFinder;
- public class IceHockey implements IPlayersFinder {
- Point[] p = new Point[10000];
- String[] pic;
- int n,len,strlen,maxX=-1,maxY=-1,minX=99999,minY=99999;
- boolean[][] visited;
- int comp = -1,pSize= 0;
- @Override
- public Point[] findPlayers(String[] photo, int team, int threshold) {
- pSize = 0;
- pic = photo;
- if(photo == null )return new Point[0];
- if(photo.length == 0)return new Point[0];
- if(photo[0] == null)return new Point[0];
- if(photo[0].length() == 0)return new Point[0];
- n = team;
- len = photo.length;
- strlen = photo[0].length();
- visited = new boolean[photo.length][photo[0].length()];
- for(int i = 0 ; i < photo.length ;i++)
- {
- for(int j = 0 ; j < photo[0].length() ; j++)
- {
- comp = -1;
- maxX = -1;
- maxY = -1;
- minX = 99999;
- minY = 99999;
- if(!visited[i][j])
- {
- if(photo[i].charAt(j) == team + '0')
- {
- comp = 0;
- solve(j,i);
- }
- if(comp * 4 >= threshold)
- {
- p[pSize++] = new Point(minX + maxX + 1,minY + maxY + 1);
- }
- }
- }
- }
- Point[] points = new Point[pSize];
- for(int i = 0 ; i < pSize ; i++)points[i] = p[i];
- boolean swapped = true;
- Point temp;
- for(int i = 0 ; i < pSize-1 ; i++)
- {
- if(!swapped)break;
- for(int j = i+1 ; j < pSize ; j++)
- {
- if(points[j].x < points[i].x)
- {
- temp = points[i];
- points[i] = points[j];
- points[j] = temp;
- }
- else if(points[j].x == points[i].x)
- {
- if(points[j].y < points[i].y)
- {
- temp = points[i];
- points[i] = points[j];
- points[j] = temp;
- }
- }
- }
- }
- return points;
- }
- public void solve(int x,int y)
- {
- if(x == strlen || y == len || x < 0 || y < 0 )return;
- if(!visited[y][x] && pic[y].charAt(x) == n +'0')
- {
- maxY = Math.max(maxY, y);
- maxX = Math.max(maxX, x);
- minX = Math.min(minX, x);
- minY = Math.min(minY, y);
- visited[y][x] = true;
- comp++;
- solve(x+1,y);
- solve(x,y+1);
- solve(x-1,y);
- solve(x,y-1);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement