Ex 1

mail@pastecode.io avatar
unknown
java
2 years ago
3.7 kB
2
Indexable
Never
import java.lang.reflect.Array;
import java.util.*;

public class Main {

    public static int[][] getSecondArray() {
        int[][] array = new int[50][50];
        for (int i = 0; i < 50; i++) {
            for (int j = 0; j < 50; j++) {
                array[i][j] = Math.abs((new Random()).nextInt() % 2);
            }
        }
        return array;
    }

    public static int[][] getFirstArray() {
        return new int[][]{
                {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
                {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
                {0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0},
                {0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0},
                {0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0},
                {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
                {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
                {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
                {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
                {0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
                {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0},
                {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0},
                {0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1},
                {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0},
                {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0},
        };
    }

    public static Map<String, Object> BigCross1(int[][] M, int m) {
        Map<String, Object> map = new HashMap<>(Map.of(
                "length", 0,
                "point", "Not Exists"
        ));
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < m; j++) {
                int lengthUp = checkLine(M, m, "UP", i, j);
                int lengthDown = checkLine(M, m, "DOWN", i, j);
                int lengthRight = checkLine(M, m, "RIGHT", i, j);
                int lengthLeft = checkLine(M, m, "LEFT", i, j);
                List<Integer> lengths = Arrays.asList(lengthUp, lengthDown, lengthRight, lengthLeft);
                Collections.sort(lengths);
                int minLength = lengths.get(0) == 0 ? 0 : (lengths.get(0) * 2) - 1;
                if (minLength > Integer.parseInt(map.get("length").toString())) {
                    map.put("length", minLength);
                    map.put("point", i + "," + j);
                }
            }
        }
        return map;
    }

    public static int checkLine(int[][] M, int m, String direction, int i, int j) {
        int length = 0;
        switch (direction) {
            case "UP":
                for (int k = i; k > -1; k--) {
                    if (M[k][j] == 1) length++;
                    else break;
                }
                break;
            case "DOWN":
                for (int k = i; k < m; k++) {
                    if (M[k][j] == 1) length++;
                    else break;
                }
                break;
            case "RIGHT":
                for (int k = j; k < m; k++) {
                    if (M[i][k] == 1) length++;
                    else break;
                }
                break;
            case "LEFT":
                for (int k = j; k > -1; k--) {
                    if (M[i][k] == 1) length++;
                    else break;
                }
                break;
        }
        return length;
    }

    public static void main(String[] args) {
        Map<String, Object> result = BigCross1(getFirstArray(), 15);
        System.out.println(result.get("length"));
        System.out.println(result.get("point"));
//        getSecondArray();
    }


}