Advertisement
Guest User

Untitled

a guest
Apr 7th, 2020
162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.61 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.         int counter = 0;
  39.  
  40.  
  41.  
  42.  
  43.  
  44.         for (int i = 0; i < n; i++) {
  45.             for (int j = 0; j < m; j++) {
  46.  
  47.  
  48.                 if(is4Horizontal(array,i,j));
  49.                 else if ( is3Horizontal(array,i,j));
  50.                 else if ( is2Horizontal(array,i,j));
  51.                 else if ( is1Horizontal(array,i,j));
  52.  
  53.  
  54.  
  55.  
  56.  
  57.                 }
  58.  
  59.  
  60.  
  61.  
  62.  
  63.            
  64.             }
  65.  
  66.  
  67.         }
  68.  
  69.     public static boolean is4Horizontal(int [][] arr, int i, int j) {
  70.         if (arr[i][j] == 1) {
  71.             if ((i + 3) <= arr.length) {
  72.                 int q = i+3;
  73.                 for (int z = i; z < q; z++) {
  74.                     if (arr[z][j] == 0) {
  75.                         return false;
  76.                     }
  77.                 }
  78.                 out.println("Row - " + i + "  Col - " + j + "  4 Horiz");
  79.                 return true;
  80.             }
  81.             else return false;
  82.         } else {
  83.             return false;
  84.         }
  85.     }
  86.  
  87.     public static boolean is3Horizontal(int [][] arr, int i, int j) {
  88.         if (arr[i][j] == 1) {
  89.             if ((i + 2) <= arr.length) {
  90.                 int q = i+2;
  91.                 for (int z = i; z < q; z++) {
  92.                     if (arr[z][j] == 0) {
  93.                         return false;
  94.                     }
  95.                 }
  96.                 out.println("Row - " + i + "  Col - " + j + "  3 Horiz");
  97.                 return true;
  98.             }
  99.             else return false;
  100.         } else {
  101.             return false;
  102.         }
  103.     }
  104.  
  105.     public static boolean is2Horizontal(int [][] arr, int i, int j) {
  106.         if (arr[i][j] == 1) {
  107.             if ((i + 1) <= arr.length) {
  108.                 int q = i+1;
  109.                 for (int z = i; z < q; z++) {
  110.                     if (arr[z][j] == 0) {
  111.                         return false;
  112.                     }
  113.                 }
  114.                 out.println("Row - " + i + "  Col - " + j + "  2 Horiz");
  115.                 return true;
  116.             }
  117.             else return false;
  118.         } else {
  119.             return false;
  120.         }
  121.     }
  122.  
  123.     public static boolean is1Horizontal(int [][] arr, int i, int j) {
  124.         if (arr[i][j] == 1) {
  125.             if ((i) <= arr.length) {
  126.                 int q = i;
  127.                 for (int z = i; z < q; z++) {
  128.                     if (arr[z][j] == 0) {
  129.                         return false;
  130.                     }
  131.                 }
  132.                 out.println("Row - " + i + "  Col - " + j + "  1 Horiz");
  133.                 return true;
  134.             }
  135.             else return false;
  136.         } else {
  137.             return false;
  138.         }
  139.     }
  140.  
  141.  
  142.  
  143.  
  144.  
  145.  
  146.  
  147.     public static void main(String[] args) throws IOException {
  148.  
  149.         Sort();
  150.  
  151.  
  152.     }
  153.  
  154.  
  155. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement