1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
| public class Main {
static int resx = Integer.MAX_VALUE;
static int resy = Integer.MAX_VALUE;
public static void main(String[] args){ int n = 4; int x1 = 0, y1 = 0, d1 = 2; int x2 = 3, y2 = 0, d2 = 3; int x3 = 2, y3 = 3, d3 = 3;
int[][] counts = new int[n][n]; counts[x1][y1] = -1; counts[x2][y2] = -1; counts[x3][y3] = -1;
foo(x1, y1, d1, counts, new boolean[n][n]); foo(x2, y2, d2, counts, new boolean[n][n]); foo(x3, y3, d3, counts, new boolean[n][n]);
System.out.println(resx + " " + resy); }
public static void foo(int x, int y, int distance, int[][] counts, boolean[][] visited) { if (x < 0 || x >= counts.length || y < 0 || y >= counts.length) { return; } if (visited[x][y]) { return; } if (distance < 0) { return; }
if (distance == 0) { counts[x][y] ++; if (counts[x][y] == 3) { getMinPoint(x, y); } } visited[x][y] = true;
foo(x-1, y, distance - 1, counts, visited); foo(x+1, y, distance - 1, counts, visited); foo(x, y-1, distance - 1, counts, visited); foo(x, y+1, distance - 1, counts, visited); } private static void getMinPoint(int x, int y) { if (x < resx || y < resy) { resx = x; resy = y; } } }
|