POJ3050 Hopscotch

题目:http://acm.pku.edu.cn/JudgeOnline/problem?id=3050

思路:深搜,通过set来筛选,开始想复杂了,后来看到讨论区的某个代码,的确就5*5一点点大,直接深搜就可以了。

 

  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4. import java.util.HashSet;
  5. import java.util.Set;
  6. public class Main {
  7.     static Set<Integer> set;
  8.     static int[][] a;
  9.     public static void main(String[] args) throws IOException {
  10.         BufferedReader read = new BufferedReader(new InputStreamReader(
  11.                 System.in));
  12.         a = new int[5][5];
  13.         String[] s;
  14.         for (int i = 0; i < 5; i++) {
  15.             s = read.readLine().split(" ");
  16.             for (int j = 0; j < 5; j++) {
  17.                 a[i][j] = Integer.parseInt(s[j]);
  18.             }
  19.         }
  20.         set = new HashSet<Integer>();
  21.         for (int i = 0; i < 5; i++) {
  22.             for (int j = 0; j < 5; j++) {
  23.                 search(00, i, j);
  24.             }
  25.         }
  26.         System.out.println(set.size());
  27.     }
  28.     public static void search(int sum, int dp, int x, int y) {
  29.         if (dp == 6) {
  30.             set.add(sum);
  31.             return;
  32.         }
  33.         if (x - 1 >= 0) {
  34.             search(sum * 10 + a[x - 1][y], dp + 1, x - 1, y);
  35.         }
  36.         if (x + 1 < 5) {
  37.             search(sum * 10 + a[x + 1][y], dp + 1, x + 1, y);
  38.         }
  39.         if (y - 1 >= 0) {
  40.             search(sum * 10 + a[x][y - 1], dp + 1, x, y - 1);
  41.         }
  42.         if (y + 1 < 5) {
  43.             search(sum * 10 + a[x][y + 1], dp + 1, x, y + 1);
  44.         }
  45.     }
  46. }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值