sourav8256

safe_spot

Aug 15th, 2018
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.75 KB | None | 0 0
  1. import java.util.*;
  2. import java.io.*;
  3.  
  4. public class MyClass {
  5.     public static void main(String args[]) {
  6.         new MyClass();
  7.     }
  8.    
  9.     public class Chef{
  10.         int x;
  11.         int y;
  12.         int val;
  13.     }
  14.    
  15.     int n;
  16.     Scanner sc;
  17.     ArrayList<Chef> chefs = new ArrayList();
  18.    
  19.     public MyClass(){
  20.      sc = new Scanner(System.in);
  21.      n = sc.nextInt();
  22.      chefs = getChefs();
  23.      process();
  24.     }
  25.    
  26.    
  27.     void process(){
  28.                
  29.         int minDistance = Integer.MAX_VALUE;
  30.         int x=0;
  31.         int y=0;
  32.        
  33.         for(int i=0;i<n;i++){
  34.             for(int j=0;j<n;j++){
  35.                 int totalDist = getTotalDist(j,i);
  36.                 if(minDistance>totalDist){
  37.                     minDistance = totalDist;
  38.                     x=j;
  39.                     y=i;
  40.                 }
  41.             }
  42.         }
  43.        
  44.         System.out.print(y+" "+x);
  45.     }
  46.    
  47.    
  48.     int getTotalDist(int x,int y){
  49.         int sum = 0;
  50.         for(int i=0;i<chefs.size();i++){
  51.             sum+=Math.sqrt(Math.pow(chefs.get(i).x-x,2)+Math.pow(chefs.get(i).y-y,2));
  52.         }
  53.        
  54.         return sum;
  55.     }
  56.    
  57.     ArrayList<Chef> getChefs(){
  58.          ArrayList<Chef> chefs = new ArrayList();
  59.          for(int i=0;i<n;i++){
  60.              for(int j=0;j<n;j++){
  61.                  int num = sc.nextInt();
  62.                  if(num>0){
  63.                      Chef chef = new Chef();
  64.                      chef.x=j;
  65.                      chef.y=i;
  66.                      chef.val=num;
  67.                      chefs.add(chef);
  68.                  }
  69.              }
  70.          }
  71.          
  72.          
  73.      return chefs;  
  74.      
  75.     }
  76.    
  77. }
  78.  
  79.  
  80. /*
  81.  
  82. 4
  83. -1 -1 -1 1
  84. -1 -1 -1 -1
  85. 2 -1 -1 -1
  86. 1 2 -1 -1
  87.  
  88. */
Add Comment
Please, Sign In to add comment