Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.FileReader;
- import java.io.FileWriter;
- import java.io.IOException;
- import java.util.Scanner;
- import static java.lang.System.out;
- class Program {
- public static void Sort() throws IOException {
- Scanner read = new Scanner(new FileReader("Ships.txt"));
- int n = Integer.parseInt(read.next());
- int m = Integer.parseInt(read.next());
- int[][] array = new int[n][m];
- for (int i = 0; i < n; i++) {
- for (int j = 0; j < m; j++) {
- array[i][j] = Integer.parseInt(read.next());
- }
- }
- out.println("Array from file is: ");
- out.println(n + " rows " + m + " columns " + "\n");
- for (int i = 0; i < n; i++) {
- for (int j = 0; j < m; j++) {
- out.print(array[i][j] + " ");
- }
- out.println();
- }
- out.println();
- int counter = 0;
- for (int i = 0; i < n; i++) {
- for (int j = 0; j < m; j++) {
- if(is4Horizontal(array,i,j));
- else if ( is3Horizontal(array,i,j));
- else if ( is2Horizontal(array,i,j));
- else if ( is1Horizontal(array,i,j));
- }
- }
- }
- public static boolean is4Horizontal(int [][] arr, int i, int j) {
- if (arr[i][j] == 1) {
- if ((i + 3) <= arr.length) {
- int q = i+3;
- for (int z = i; z < q; z++) {
- if (arr[z][j] == 0) {
- return false;
- }
- }
- out.println("Row - " + i + " Col - " + j + " 4 Horiz");
- return true;
- }
- else return false;
- } else {
- return false;
- }
- }
- public static boolean is3Horizontal(int [][] arr, int i, int j) {
- if (arr[i][j] == 1) {
- if ((i + 2) <= arr.length) {
- int q = i+2;
- for (int z = i; z < q; z++) {
- if (arr[z][j] == 0) {
- return false;
- }
- }
- out.println("Row - " + i + " Col - " + j + " 3 Horiz");
- return true;
- }
- else return false;
- } else {
- return false;
- }
- }
- public static boolean is2Horizontal(int [][] arr, int i, int j) {
- if (arr[i][j] == 1) {
- if ((i + 1) <= arr.length) {
- int q = i+1;
- for (int z = i; z < q; z++) {
- if (arr[z][j] == 0) {
- return false;
- }
- }
- out.println("Row - " + i + " Col - " + j + " 2 Horiz");
- return true;
- }
- else return false;
- } else {
- return false;
- }
- }
- public static boolean is1Horizontal(int [][] arr, int i, int j) {
- if (arr[i][j] == 1) {
- if ((i) <= arr.length) {
- int q = i;
- for (int z = i; z < q; z++) {
- if (arr[z][j] == 0) {
- return false;
- }
- }
- out.println("Row - " + i + " Col - " + j + " 1 Horiz");
- return true;
- }
- else return false;
- } else {
- return false;
- }
- }
- public static void main(String[] args) throws IOException {
- Sort();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement