POJ3050 这题太水了

来源:互联网 发布:sql小计合计 编辑:程序博客网 时间:2024/06/10 07:48

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