Advertisement
Guest User

Untitled

a guest
Apr 8th, 2020
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.37 KB | None | 0 0
  1. import java.io.FileReader;
  2. import java.io.FileWriter;
  3. import java.io.IOException;
  4. import java.util.Scanner;
  5.  
  6. import static java.lang.System.out;
  7.  
  8. class Program {
  9.  
  10.  
  11.     public static void Sort() throws IOException {
  12.  
  13.         Scanner read = new Scanner(new FileReader("Ships.txt"));
  14.  
  15.         int n = Integer.parseInt(read.next());
  16.         int m = Integer.parseInt(read.next());
  17.  
  18.         int[][] array = new int[n][m];
  19.  
  20.         for (int i = 0; i < n; i++) {
  21.             for (int j = 0; j < m; j++) {
  22.                 array[i][j] = Integer.parseInt(read.next());
  23.             }
  24.         }
  25.  
  26.         out.println("Array from file is: ");
  27.         out.println(n + " rows " + m + " columns " + "\n");
  28.  
  29.  
  30.         for (int i = 0; i < n; i++) {
  31.             for (int j = 0; j < m; j++) {
  32.                 out.print(array[i][j] + " ");
  33.             }
  34.             out.println();
  35.         }
  36.         out.println();
  37.  
  38.  
  39.         for (int i = 0; i < n; i++) {
  40.             for (int j = 0; j < m; j++) {
  41.  
  42.  
  43.                 if(is4Vertical(array,i,j));
  44.                 else if ( is3Vertical(array,i,j));
  45.                 else if ( is2Vertical(array,i,j));
  46.                 else if ( is1x1(array,i,j));
  47.  
  48.                 if(is4Horizontal(array,i,j));
  49.                 else if ( is3Horizontal(array,i,j));
  50.                 else if (is2Horizontal(array,i,j));
  51.  
  52.             }
  53.  
  54.             }
  55.  
  56.  
  57.         }
  58.  
  59.     public static boolean is4Horizontal(int [][] arr, int i, int j) {
  60.  
  61.         if (arr[i][j] == 1) {
  62.  
  63.             if ((j + 3) < arr.length) {
  64.                 int temp = i+3;
  65.                 for (int z = j; z < temp; z++) {
  66.                     if (arr[i][z] == 0) {
  67.                         return false;
  68.                     }
  69.                 }
  70.                 out.println("Row - " + i + "  Col - " + j + "  4 Horizontal");
  71.                 return true;
  72.             }
  73.             else return false;
  74.         } else {
  75.             return false;
  76.         }
  77.     }
  78.  
  79.     public static boolean is3Horizontal(int [][] arr, int i, int j) {
  80.  
  81.         if (arr[i][j] == 1) {
  82.             if ((j + 2) < arr.length) {
  83.                 int temp = i+2;
  84.                 for (int z = j; z < temp; z++) {
  85.                     if (arr[i][z] == 0) {
  86.                         return false;
  87.                     }
  88.                 }
  89.                 out.println("Row - " + i + "  Col - " + j + "  3 Horizontal");
  90.                 return true;
  91.             }
  92.             else return false;
  93.         } else {
  94.             return false;
  95.         }
  96.     }
  97.  
  98.     public static boolean is2Horizontal(int [][] arr, int i, int j) {
  99.  
  100.         if (arr[i][j] == 1) {
  101.             if ((j + 1) < arr.length) {
  102.                 int temp = i+1;
  103.                 for (int z = j; z < temp; z++) {
  104.                     if (arr[i][z] == 0) {
  105.                         return false;
  106.                     }
  107.                 }
  108.                 out.println("Row - " + i + "  Col - " + j + "  2 Horizontal");
  109.                 return true;
  110.             }
  111.             else return false;
  112.         } else {
  113.             return false;
  114.         }
  115.     }
  116.  
  117.     public static boolean is4Vertical(int [][] arr, int i, int j) {
  118.  
  119.         if (arr[i][j] == 1) {
  120.             if ((i + 3) < arr.length) {
  121.                 int temp = i+3;
  122.                 for (int z = i; z < temp; z++) {
  123.                     if (arr[z][j] == 0) {
  124.                         return false;
  125.                     }
  126.                 }
  127.                 out.println("Row - " + i + "  Col - " + j + "  4 Vertic");
  128.                 return true;
  129.             }
  130.             else return false;
  131.         } else {
  132.             return false;
  133.         }
  134.     }
  135.  
  136.     public static boolean is3Vertical(int [][] arr, int i, int j) {
  137.         if (arr[i][j] == 1) {
  138.             if ((i + 2) < arr.length) {
  139.                 int temp = i+2;
  140.                 for (int z = i; z < temp; z++) {
  141.                     if (arr[z][j] == 0) {
  142.                         return false;
  143.                     }
  144.                 }
  145.                 out.println("Row - " + i + "  Col - " + j + "  3 Vertic");
  146.                 return true;
  147.             }
  148.             else return false;
  149.         } else {
  150.             return false;
  151.         }
  152.     }
  153.  
  154.     public static boolean is2Vertical(int [][] arr, int i, int j) {
  155.         if (arr[i][j] == 1) {
  156.             if ((i + 1) < arr.length) {
  157.                 int temp = i+1;
  158.                 for (int z = i; z < temp; z++) {
  159.                     if (arr[z][j] == 0) {
  160.                         return false;
  161.                     }
  162.                 }
  163.                 out.println("Row - " + i + "  Col - " + j + "  2 Vertic");
  164.                 return true;
  165.             }
  166.             else return false;
  167.         } else {
  168.             return false;
  169.         }
  170.     }
  171.  
  172.     public static boolean is1x1(int [][] arr, int i, int j) {
  173.         if (arr[i][j] == 1) {
  174.             if ((i) < arr.length) {
  175.                 int temp = i;
  176.                 for (int z = i; z < temp; z++) {
  177.                     if (arr[z][j] == 0) {
  178.                         return false;
  179.                     }
  180.                 }
  181.                 out.println("Row - " + i + "  Col - " + j + "  1 x 1");
  182.                 return true;
  183.             }
  184.             else return false;
  185.         } else {
  186.             return false;
  187.         }
  188.     }
  189.  
  190.  
  191.  
  192.     public static void main(String[] args) throws IOException {
  193.  
  194.         Sort();
  195.  
  196.     }
  197.  
  198.  
  199. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement