日进一步之“A Knight's Journey”

题目大意:

  一个骑士,类似中国象棋的马,在一个矩形的棋盘上跳。

  询问是否存在一个起点,能让这个骑士不重复的跳完全部棋盘格子。

  输出骑士的行动路线,要求路径为最小字典序。

  无法达到目标则输出  impossible

  要注意每次输出换行后再换一行。

  输入 p q ,保证 1 <= p*q <= 26 。

  样例:

    3

    1 1

    2 3

    4 3

    ——————————————

    Scenario #1:

    A1

 

    Scenario #2:

    impossible

 

    Scenario #3:

    A1B3C1A2B4C2A3B1C3A4B2C4

解题思路:

  最小字典序的要求用一个存放方向的数组体现。

  本次搜索不要走本次走过的点。

  每次行动更新标记数组,如果走到了一个之前搜索过的地方,就pass了。

  利用类或者结构体存放点坐标。

  用java的话一定要“实例化变量”。

  最开始出现了一个bug:

    在搜索中存放的坐标到了输出的时候,全都变成了最后一个坐标。

    比如输入 “4 3”  就连续输出了 12 个 C4。。。。

    改了半天,最后索性全部擦掉,改了一下类成员变量和输出方式,然后过了。

AC代码:

 1 import java.util.*;
 2 
 3 class Pass{
 4     int xx;
 5     int yy;
 6     String temp = "";
 7     void set(int t1,int t2){ 
 8         xx = t1; yy = t2; 
 9         temp = "" + (char)(xx + 'A') + (yy + 1);
10     }
11     String get(){return temp;}
12     
13 }
14 
15 public class Main{
16     
17     static boolean mark[][] = new boolean[30][30];
18     static int m; static int n;
19     static Pass p[] = new Pass[30];
20 
21     static boolean dfs(int x,int y,int step){
22         //p[step] = new Pass();
23         int dir[][]={{-2,-1},{-2,1},{-1,-2},{-1,2},{1,-2},{1,2},{2,-1},{2,1}};
24         if(step == m * n){
25             mark[x][y] = true;
26             p[step].set(x,y);
27             return true;
28         }
29         mark[x][y] = true;
30         p[step].set(x,y);
31 
32         int tx;int ty;
33         for(int i = 0;i < 8;i ++){
34             tx = x + dir[i][0];
35             ty = y + dir[i][1];
36             if(tx >= 0 && ty >= 0 && tx < n && ty < m && mark[tx][ty] == false){
37                 if(dfs(tx,ty,step + 1) == true){ return true; }
38                 mark[tx][ty] = false;
39             }
40         }
41         return false;
42     }
43     
44     public static void main(String[] args){
45         Scanner sc = new Scanner(System.in);
46         for(int i = 0;i < 30;i ++){p[i] = new Pass();}
47         int T = sc.nextInt();int k = 1;
48         while(T > 0){
49             for(int i = 0;i < 30;i ++){for(int j = 0;j < 30;j ++){ mark[i][j] = false; }}
50             boolean flag = false;
51             m = sc.nextInt();
52             n = sc.nextInt();
53             System.out.printf("Scenario #%d:\n",k++);
54             for(int i = 0;i < n;i ++){
55                 if(flag == true){break;}
56                 for(int j = 0;j < m;j ++){
57                     if(dfs(i,j,1) == true){
58                         flag = true;
59                         break;
60                     }
61                 }
62             }
63             if(flag == true){
64                 String out = "";
65                 for(int h = 1;h <= n * m;h ++){
66                     out = out + p[h].get();
67                 }
68                 System.out.println(out);System.out.println();
69             }
70             if(flag == false){System.out.println("impossible");System.out.println();}
71             T --;
72         }
73     }
74 }

 

PS:

  之所以用 “日进一步” 这个标题,不止是因为骑士的每一步都是走日字的,还包含了我希望我们的小队的每个成员都能日进一步的愿望,达到我们的梦想!!!!!!

  借博客园众多大佬的力量,在此许一次愿。。。。。。

转载于:https://www.cnblogs.com/love-fromAtoZ/p/7552085.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值