POJ3050 这题太水了

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

public class Hopscotch {

static int[][] grid = new int[5][5];
static boolean[] vis = new boolean[2000000];
static int[] tres = new int[7];
static int[][] dir = new int[][] { { -1, 0 }, { 1, 0 }, { 0, -1 }, { 0, 1 } };
static int cot;

public static void main(String[] args) throws FileNotFoundException {
    // TODO Auto-generated method stub
    @SuppressWarnings("resource")
    Scanner sc = new Scanner(System.in);
    sc = new Scanner(new File("files/hopscotch"));
    for (int i = 0; i < 5; i++)
        for (int j = 0; j < 5; j++)
            grid[i][j] = sc.nextInt();
    cot = 0;
    for (int i = 0; i < 5; i++)
        for (int j = 0; j < 5; j++) {
            tres = new int[7];
            tres[0] = 1;
            tres[1] = grid[i][j];
            dfs(i, j, 1);
        }
    System.out.println(cot);
}

private static void dfs(int x, int y, int step) {
    // TODO Auto-generated method stub
    if (step == 6) {
        int tmp = 1;
        for (int i = 1; i < 7; i++) {
            tmp *= 10;
            tmp += tres[i];
        }
        if (!vis[tmp]) {
            vis[tmp] = true;
            cot++;
        }
        return;
    }
    int nx, ny;
    for (int i = 0; i < 4; i++) {
        nx = x + dir[i][0];
        ny = y + dir[i][1];
        if (nx >= 0 && nx <= 4 && ny >= 0 && ny <= 4) {
            tres[step + 1] = grid[nx][ny];
            dfs(nx, ny, step + 1);
        }
    }
}

}

sample input:
1 1 1 1 1
1 1 1 1 1
1 1 1 1 1
1 1 1 2 1
1 1 1 1 1

sample output:
15

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值