Advertisement
Guest User

A326407

a guest
Oct 2nd, 2019
240
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.49 KB | None | 0 0
  1. import java.lang.Math;
  2.  
  3. class A326407{
  4.    
  5.     public static void main(String[] args){
  6.    
  7.         int length = 100;
  8.         int side = (int) (Math.ceil(Math.sqrt((double)length)) + 1);
  9.         int[][] grid = new int[side][side];
  10.         fillGrid(grid);
  11.         int[][] marked = markMines(grid);
  12.         int[][] transf = transform(marked);
  13.         //display(transf);
  14.         read(length, transf);
  15.     }
  16.    
  17.     static int[] coordinates(int n){
  18.         int[] output = new int[2];
  19.         int lower = (int) (Math.ceil(Math.sqrt(n)) - 1);
  20.         int reminder = n - lower * lower;
  21.        
  22.         if (reminder > lower + 1){
  23.                 output[0] = 2*lower+1-reminder;
  24.                 output[1] = lower ;
  25.         } else {
  26.             output[0]= lower ;
  27.             output[1]= reminder -1;
  28.         }
  29.         //System.out.println(n+"   " + lower + "  " + reminder + "  " + output[0] + "   " +output[1]);
  30.         return output;
  31.     }
  32.    
  33.     static void fillGrid(int[][] grid){
  34.         grid[0][0] = 1;
  35.         for (int i = 2; i < grid.length*grid.length+1 ; i++){
  36.             grid[coordinates(i)[0]][coordinates(i)[1]]=i;
  37.         }
  38.     }
  39.  
  40.     static int[][] markMines(int[][] grid){
  41.         int[][] output = new int[grid.length][grid.length];
  42.         for(int i = 0; i < grid.length; i++){
  43.             for(int j = 0; j < grid.length; j++){
  44.                 if (isPrime(grid[i][j])){
  45.                     output[i][j] = -1;
  46.                 } else {
  47.                     output[i][j] = grid[i][j];
  48.                 }
  49.             }
  50.         }
  51.         return output;
  52.     }
  53.  
  54.     static int checkMine(int i, int j, int[][] grid){
  55.         if (grid[i][j] == -1) {return 1;}
  56.         else {return 0;}
  57.     }
  58.  
  59.     static int[][] transform(int[][] grid){
  60.         int[][] output = new int[grid.length][grid.length];
  61.        
  62.         for(int i = 0; i < grid.length; i++){
  63.             for(int j = 0; j < grid.length; j++){
  64.                 if (checkMine(i,j,grid)==1){output[i][j]=-1;}
  65.                 else {
  66.                     for (int k = -1; k<2;k++){
  67.                         for (int l = -1; l<2;l++){
  68.                             if ((i+k>-1) && (j+l>-1) && (i+k<grid.length) && (j+l<grid.length)) {output[i][j]=output[i][j]+checkMine(i+k,j+l,grid);}
  69.                         }
  70.                     }
  71.                 }
  72.             }
  73.         }
  74.         return output;
  75.     }
  76.    
  77.     static boolean isPrime(int n){
  78.         if (n==1){return false;}
  79.         if (n==2){return true;}
  80.         if (n>2){
  81.             if (n%2==0) {return false;}
  82.             else{
  83.                 for(int i=3; i*i < n+1 ;i++){
  84.                     if (n%i == 0) {return false;}
  85.                 }
  86.             }
  87.         }
  88.         return true;
  89.     }
  90.    
  91.     static void display(int[][] array){
  92.         for (int i = 0; i< array.length; i++){
  93.             for (int j = 0; j< array[0].length; j++){
  94.                 System.out.print(array[i][j]+"\t");
  95.             }
  96.         System.out.println();
  97.         }
  98.     }
  99.  
  100.     static void read(int n, int[][] grid){
  101.         for (int i = 1; i< n+1; i++){
  102.             System.out.println(i+ "  " + grid[coordinates(i)[0]][coordinates(i)[1]]);
  103.         }
  104.     }
  105. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement