Background
The knight is getting bored of seeing the same black and white squares again and again and has decided to make a journey

around the world. Whenever a knight moves, it is two squares in one direction and one square perpendicular to this. The world of a knight is the chessboard he is living on. Our knight lives on a chessboard that has a smaller area than a regular 8 * 8 board, but it is still rectangular. Can you help this adventurous knight to make travel plans?

Problem

Find a path such that the knight visits every square once. The knight can start and end on any square of the board.

Input

The input begins with a positive integer n in the first line. The following lines contain n test cases. Each test case consists of a single line with two positive integers p and q, such that 1 <= p * q <= 26. This represents a p * q chessboard, where p describes how many different square numbers 1, . . . , p exist, q describes h

  1. dfs
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <iostream>
  6. #include <algorithm>
  7. using namespace std;
  8. int p,q;
  9. int flag=0;
  10. bool vis[100][100];
  11. int path[100][100];
  12. int tx[8]={-1,1,-2,2,-2,2,-1,1};
  13. int ty[8]={-2,-2,-1,-1,1,1,2,2};
  14. bool judge(int x,int y)
  15. {
  16. if(x>=1&&y>=1&&x<=p&&y<=q&&!flag&&!vis[x][y]) return true;
  17. return false;
  18. }
  19. void dfs(int x,int y,int step)//这里不知道你们有没有问题,反正我碰到了,就是,这里的x和y,与坐标系不一样
  20. {
  21. path[step][0]=x;//x代表行
  22. path[step][1]=y;//y代表列
  23. if(step==p*q)//直角坐标系中x虽然是横轴,但是x的改变则是列的变化
  24. {
  25. flag=1;
  26. return ;
  27. }
  28. for(int i=0;i<8;i++)
  29. {
  30. int sx=x+tx[i];
  31. int sy=y+ty[i];
  32. if(judge(sx,sy))
  33. {
  34. vis[sx][sy]=1;
  35. dfs(sx,sy,step+1);
  36. vis[sx][sy]=0;
  37. }
  38. }
  39. }
  40.  
  41. int main()
  42. {
  43. int n,c=0;
  44. cin>>n;
  45. while(n--)
  46. {
  47. scanf("%d%d",&p,&q);
  48. printf("Scenario #%d:\n",++c);
  49. memset(vis,0,sizeof(vis));
  50. flag=0;
  51. vis[1][1]=1;
  52. dfs(1,1,1);
  53. if(flag==1)
  54. {
  55. for(int i=1;i<=p*q;i++)
  56. {
  57. printf("%c%d",path[i][1]-1+'A',path[i][0]);
  58. }
  59. printf("\n");
  60. }
  61. else printf("impossible\n");
  62. if(n!=0) printf("\n");
  63. }
  64. return 0;
  65. }

  

ow many different square letters exist. These are the first q letters of the Latin alphabet: A, . . .

Output

The output for every scenario begins with a line containing "Scenario #i:", where i is the number of the scenario starting at 1. Then print a single line containing the lexicographically first path that visits all squares of the chessboard with knight moves followed by an empty line. The path should be given on a single line by concatenating the names of the visited squares. Each square name consists of a capital letter followed by a number.
If no such path exist, you should output impossible on a single line.

Sample Input

  1. 3
  2. 1 1
  3. 2 3
  4. 4 3

Sample Output

  1. Scenario #1:
  2. A1
  3.  
  4. Scenario #2:
  5. impossible
  6.  
  7. Scenario #3:
  8. A1B3C1A2B4C2A3B1C3A4B2C4
  9.  
  10. 题目大意:就是给你pq列,求马是否可以走完,可以求出路径,不可以输出-1
    意思很明确,不过毕竟是英文题,有点难读

寒假训练 A - A Knight's Journey 搜索的更多相关文章

  1. Poj 2488 A Knight's Journey(搜索)

    Background The knight is getting bored of seeing the same black and white squares again and again an ...

  2. A Knight's Journey 分类: POJ 搜索 2015-08-08 07:32 2人阅读 评论(0) 收藏

    A Knight's Journey Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 35564 Accepted: 12119 ...

  3. HDOJ-三部曲一(搜索、数学)- A Knight's Journey

    A Knight's Journey Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other) ...

  4. 广大暑假训练1(poj 2488) A Knight's Journey 解题报告

    题目链接:http://vjudge.net/contest/view.action?cid=51369#problem/A   (A - Children of the Candy Corn) ht ...

  5. POJ2488-A Knight's Journey(DFS+回溯)

    题目链接:http://poj.org/problem?id=2488 A Knight's Journey Time Limit: 1000MS   Memory Limit: 65536K Tot ...

  6. POJ 2488 A Knight's Journey(深搜+回溯)

    A Knight's Journey Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other) ...

  7. 迷宫问题bfs, A Knight's Journey(dfs)

    迷宫问题(bfs) POJ - 3984   #include <iostream> #include <queue> #include <stack> #incl ...

  8. poj2488 A Knight's Journey裸dfs

    A Knight's Journey Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 35868   Accepted: 12 ...

  9. POJ2488A Knight's Journey[DFS]

    A Knight's Journey Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 41936   Accepted: 14 ...

随机推荐

  1. 设计模式教程(Design Patterns Tutorial)笔记之一 创建型模式(Creational Patterns)

    目录 · 概述 · Factory · What is the Factory Design Pattern? · Sample Code · Abstract Factory · What is t ...

  2. Sphinx coreseek 3.2

    功能 中文的拆词索引 MySQL中like模糊查询.   >1>5>%可以用索引 配置 用编辑器打开 path 配置 改绝对路径 https://blog.csdn.net/u013 ...

  3. css布局------块元素水平垂直居中的四种方法

    HTML <div class="parent answer-1"> <div></div></div> CSS .parent { ...

  4. 图片上传预览js

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  5. MVC 【ASPX视图引擎】

    新建项目----ASP.NET MVC 4 Web 应用程序------选择模板(空).视图引擎(ASPX) 1.认识控制器Controller using System; using System. ...

  6. asp.net通过后台代码给前台设置css样式,下拉列表在js中的取值

    后台根据不同的用户登陆隐藏或显示前台div标签 前台: 将div声明成服务器端控件 <div id="div1" runat="server">.. ...

  7. 本地计算机上的MySQL服务启动后停止。某些服务在未由其他服务或程序使用时将自动

    重新安装MySQL数据库,由于安装的时候马虎,一路next(事实上,某些地方需要严格的配置,我忘记注意了),导致现在出了很多麻烦. 错误信息: 本地计算机上的MySQL服务启动后停止.某些服务在未由其 ...

  8. Python3 系列之 编程规范篇

    编码规范 编码 如无特殊情况, 文件一律使用 UTF-8 编码 如无特殊情况, 文件头部必须加入 #--coding:utf-8-- 标识 缩进 统一使用 4 个空格进行缩进 引号 自然语言 使用双引 ...

  9. STOMP

    STOMP: 说明:STOMP is a simple text-orientated messaging protocol. 面向文本消息协议 spring 之stomp https://www.c ...

  10. 【代码笔记】Web-HTML-链接

    一,效果图. 二,代码. <!DOCTYPE html> <html> <head> <meta charset="utf-8"> ...