Untitled

 avatar
unknown
plain_text
a year ago
1.8 kB
4
Indexable
import java.util.Scanner;

public class Main {
	static int T, M, N;
	static int[][] arr = new int[20][20];
	static int[][] v = new int[20][20];
	static int ans;

	static int[] dxc = { -1, 0, 1, 1, 1, 0 };

	static int[] dyc = { 0, 1, 1, 0, -1, -1 };

	static int[] dxl = { -1, -1, 0, 1, 0, -1 };

	static int[] dyl = { 0, 1, 1, 0, -1, -1 };

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		T = sc.nextInt();
		for (int tc = 1; tc <= T; tc++) {
			ans = 0;
			M = sc.nextInt();
			N = sc.nextInt();
			for (int i = 1; i <= N; i++) {
				for (int j = 1; j <= M; j++) {
					arr[i][j] = sc.nextInt();
				}
			}
			for (int i = 1; i <= N; i++) {
				for (int j = 1; j <= M; j++) {
					v[i][j] = 1;
					int sum = arr[i][j];
					bt(i, j, sum, 1);
					v = new int[N][M];
					// reset mang v
				}
			}
			System.out.println("Case #" + tc);
			System.out.println((long) (ans * ans));
		}
	}

	public static boolean safe(int x, int y) {
		return x >= 1 && x <= N && y > 0 && y <= M;
	}

	public static void bt(int x, int y, int sum, int count) {
		if (count == 4) {
			if (ans < sum) {
				ans = sum;
			}
			return;
		}
		for (int i = 0; i < 6; i++) {
			if (y % 2 == 0) {
				int nx = x + dxc[i];
				int ny = y + dyc[i];
				if (safe(nx, ny) && v[nx][ny] == 0) {
					v[nx][ny] = 1;
					bt(nx, ny, sum + arr[nx][ny], count + 1);
					bt(x, y, sum + arr[nx][ny], count + 1);
					v[nx][ny] = 0;
				}
			} else {
				int nx = x + dxl[i];
				int ny = y + dyl[i];
				if (safe(nx, ny) && v[nx][ny] == 0) {
					v[nx][ny] = 1;
					bt(nx, ny, sum + arr[nx][ny], count + 1);
					bt(x, y, sum + arr[nx][ny], count + 1);
					v[nx][ny] = 0;
				}
			}
		}
	}
}
Editor is loading...
Leave a Comment