Untitled
unknown
plain_text
2 years ago
3.4 kB
11
Indexable
package future;
import java.io.FileInputStream;
import java.util.Scanner;
import java.util.function.BiConsumer;
class index {
int row;
int col;
public index(int x, int y) {
this.row = x;
this.col = y;
}
}
public class maybay {
// static index[] mangrs;
static int[] dichchuyenrow = { -1, -1, -1 };
static int[] dichcuyencol = { -1, 0, 1 };
static int N, sum, max;
static int[][] map;
static boolean check;
static void try1(int index, int r, int c, boolean check) {
if (sum < 0) {
return;
}
if (index == N ) {
if (sum > max) {
max = sum;
return;
}
}
if (check) {
for (int j = 0; j <= 1; j++) {
if (j == 0) {
check = false;
int min = Math.max(r-6, 0);
for (int i1 = r-1; i1 > min; i1--) {
for (int j1 = 0; j1 < 5; j1++) {
if (map[i1][j1] == -1) {
map[i1][j1] = -2;
}
}
}
for (int i = 0; i < 3; i++) {
int nextr = r + dichchuyenrow[i];
int nextc = c + dichcuyencol[i];
if (nextr >= 0 && nextr < N && nextc >= 0 && nextc < 5) {
if(map[nextr][nextc] == -2) {
try1(index + 1, nextr, nextc, check);
}
else {
sum += map[nextr][nextc];
try1(index + 1, nextr, nextc, check);
sum -= map[nextr][nextc];
}
}
}
for (int i1 = r-1; i1 > min; i1--) {
for (int j1 = 0; j1 < 5; j1++) {
if (map[i1][j1] == -2) {
map[i1][j1] = -1;
}
}
}
check = true;
}
if (j == 1) {
for (int i = 0; i < 3; i++) {
int nextr = r + dichchuyenrow[i];
int nextc = c + dichcuyencol[i];
if (nextr >= 0 && nextr < N && nextc >= 0 && nextc < 5) {
if(map[nextr][nextc] == -2) {
sum += 0;
try1(index + 1, nextr, nextc, check);
sum -= 0;
}
else {
sum += map[nextr][nextc];
try1(index + 1, nextr, nextc, check);
sum -= map[nextr][nextc];
}
}
}
}
}
}
else {
for (int i = 0; i < 3; i++) {
int nextr = r + dichchuyenrow[i];
int nextc = c + dichcuyencol[i];
if (nextr >= 0 && nextr < N && nextc >= 0 && nextc < 5) {
if(map[nextr][nextc] == -2) {
sum += -1;
try1(index + 1, nextr, nextc, check);
sum -= -1;
}
else {
sum += map[nextr][nextc];
try1(index + 1, nextr, nextc, check);
sum -= map[nextr][nextc];
}
}
}
}
}
public static void main(String[] args) throws Exception {
System.setIn(new FileInputStream("src/practice/input.txt"));
Scanner scanner = new Scanner(System.in);
int T = scanner.nextInt();
for (int t = 1; t <= T; t++) {
System.out.println("Case #" + t);
N = scanner.nextInt();
map = new int[N][5];
for (int i = 0; i < N; i++) {
for (int j = 0; j < 5; j++) {
map[i][j] = scanner.nextInt();
if (map[i][j] == 2) {
map[i][j] = -1;
}
}
}
sum = 0;
max = -1;
check = true;
try1(0, N , 2,check);
if(max == -1) {
System.out.println("-1");
}
else
System.out.println(max );
}
}
}
Editor is loading...
Leave a Comment