MarbleGame优化解法

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class MarbleGame2 {

static int[][] board;
static int[][] tboard;
static int N;
static int[] spinfo = new int[3];
static int score;
static int tscore;

// 以上下左右四个方向而来后的转向
static int[][] veer = { { 1, 3, 0, 2 }, { 3, 0, 1, 2 }, { 2, 0, 3, 1 }, { 1, 2, 3, 0 }, { 1, 0, 3, 2 } };
// 上下左右四个方向的坐标变化
static int[][] dir = { { -1, 0 }, { 1, 0 }, { 0, -1 }, { 0, 1 } };
// 下上右左四个方向的坐标变化
static int[][] dir2 = { { 1, 0 }, { -1, 0 }, { 0, 1 }, { 0, -1 } };

public static void main(String[] args) throws FileNotFoundException {
    // TODO Auto-generated method stub
    Scanner sc = new Scanner(System.in);
    sc = new Scanner(new File("src/input.txt"));
    int T = sc.nextInt();
    for (int t = 0; t < T; t++) {
        score = 0;
        N = sc.nextInt();
        board = new int[N][N];
        tboard = new int[N][N];
        for (int i = 0; i < N; i++) {
            for (int j = 0; j < N; j++) {
                board[i][j] = sc.nextInt();
            }
        }
        for (int i = 0; i < N; i++) {
            for (int j = 0; j < N; j++) {
                if ((board[i][j] % 10) != 0) {
                    for (int k = 0; k < 4; k++) {
                        if (board[(i + dir2[k][0] + N) % N][(j + dir2[k][1] + N) % N] == 0) {
                            tscore = 0;
                            CopyArray();
                            Marble(i, j, k);
                            if (tscore > score) {
                                score = tscore;
                                spinfo[0] = i;
                                spinfo[1] = j;
                                spinfo[2] = k;
                            }
                        }
                    }
                }
            }
        }
        System.out.println("maxscore: " + score);
        System.out.println("startpoint: (" + spinfo[0] + "," + spinfo[1] + ")");
        String hitdir = "";
        switch (spinfo[2]) {
        case 0:
            hitdir = "下";
            break;
        case 1:
            hitdir = "上";
            break;
        case 2:
            hitdir = "右";
            break;
        case 3:
            hitdir = "左";
            break;
        }
        System.out.println("从" + hitdir + "方向其弹射");
        System.out.println();
    }
}

private static void Marble(int x, int y, int d) {
    // TODO Auto-generated method stub
    tboard[x][y]--;
    tscore++;
    int veerdir = veer[(tboard[x][y] / 10) - 1][d];
    for (int i = 0; i < N; i++) {
        x = (x + dir[veerdir][0] + N) % N;
        y = (y + dir[veerdir][1] + N) % N;
        if ((tboard[x][y] % 10) != 0) {
            Marble(x, y, veerdir);
            break;
        }
    }
}

private static void CopyArray() {
    // TODO Auto-generated method stub
    for (int i = 0; i < N; i++) {
        for (int j = 0; j < N; j++) {
            tboard[i][j] = board[i][j];
        }
    }
}

}

sample input:
4
3
0 41 0
41 53 11
0 31 0
5
0 0 31 0 0
51 31 0 0 0
0 0 42 31 32
0 0 21 0 0
0 11 0 0 32
6
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
52 0 0 0 0 0
0 0 0 0 0 0
6
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 11 0 0 21
52 0 0 0 22 0
0 0 0 0 0 0

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值