A Knight's Journey
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 35342   Accepted: 12051

Description

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 how 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

3
1 1
2 3
4 3

Sample Output

Scenario #1:
A1 Scenario #2:
impossible Scenario #3:
A1B3C1A2B4C2A3B1C3A4B2C4

给一个棋盘,给你一匹马,马只能走“日”字格,问马能否不重复地走完所有格并且输出其走的位置的字典顺序。

头一次做深搜,中间出了不少问题,包括color在深搜之后居然没给它还原回去,这样的错误也行。。。

然后字典顺序是一个,如何记录结果path路径。

深搜给我的第一个感受就是能不用全局变量就不用全局变量,总在这里容易出错,另外,记录走的步数要设立一个参数step,之后再每层加1,这样的做法才是正途。。。

稀里糊涂地说了这么多,都是自己当时犯下的错,各种辛酸只有自己明白了。。。

代码:

#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <string>
#include <cstring>
#pragma warning(disable:4996)
using namespace std; struct struct_a{
int x;
int y;
}path[900]; int Test,i,p,q,m,n,flag,num;
int move_x[10]={-1,1,-2,2,-2,2,-1,1};//改成字典顺序
int move_y[10]={-2,-2,-1,-1,1,1,2,2}; int color[30][30]; int dfs(int a,int b,int step)
{
if(flag==1)
return 1;
int k,temp_x,temp_y;
if(step==q*p)
{
flag=1;
return 1;
}
for(k=0;k<8;k++)
{
temp_x = a + move_x[k];
temp_y = b + move_y[k]; if(temp_x>=1 && temp_x<=p && temp_y>=1 && temp_y<=q && color[temp_x][temp_y]==0)
{
color[temp_x][temp_y] = color[a][b]+1;
path[step].x=temp_x;//别用其他变量标记,递归这样不容易出现问题
path[step].y=temp_y;
dfs(temp_x,temp_y,step+1);
if(flag)
return 1;
}
}
color[a][b]=0;//如果最终选择不是这里,要记得清空
return 0;
} int main()
{
cin>>Test; for(i=1;i<=Test;i++)
{
cin>>p>>q;
cout<<"Scenario #"<<i<<":"<<endl; flag=0;
memset(color,0,sizeof(color));
color[1][1]=1;
path[0].x=1;
path[0].y=1; dfs(1,1,1); if(flag==1)
{
int v;
for(v=0;v<p*q;v++)
{
char temp_c=path[v].y-1+'A';
cout<<temp_c<<path[v].x;
}
cout<<endl;
}
else
cout<<"impossible"<<endl;
cout<<endl;
} return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

POJ 2488:A Knight's Journey 深搜入门之走马观花的更多相关文章

  1. POJ 2488 -- A Knight's Journey(骑士游历)

    POJ 2488 -- A Knight's Journey(骑士游历) 题意: 给出一个国际棋盘的大小,判断马能否不重复的走过所有格,并记录下其中按字典序排列的第一种路径. 经典的“骑士游历”问题 ...

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

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

  3. POJ 2488 A Knight's Journey(DFS)

    A Knight's Journey Time Limit: 1000MSMemory Limit: 65536K Total Submissions: 34633Accepted: 11815 De ...

  4. poj 2488 A Knight's Journey( dfs )

    题目:http://poj.org/problem?id=2488 题意: 给出一个国际棋盘的大小,判断马能否不重复的走过所有格,并记录下其中按字典序排列的第一种路径. #include <io ...

  5. POJ 2488 A Knight's Journey (回溯法 | DFS)

    题目链接:http://poj.org/problem?id=2488 题意: 在国际象棋的题盘上有一个骑士,骑士只能走“日”,即站在某一个位置,它可以往周围八个满足条件的格子上跳跃,现在给你一个p ...

  6. poj 2488 A Knight's Journey 【骑士周游 dfs + 记忆路径】

    题目地址:http://poj.org/problem?id=2488 Sample Input 3 1 1 2 3 4 3 Sample Output Scenario #1: A1 Scenari ...

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

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

  8. [poj]2488 A Knight's Journey dfs+路径打印

    Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 45941   Accepted: 15637 Description Bac ...

  9. POJ 2488 A Knight's Journey【DFS】

    补个很久之前的题解.... 题目链接: http://poj.org/problem?id=2488 题意: 马走"日"字,让你为他设计一条道路,走遍所有格,并输出字典序最小的一条 ...

随机推荐

  1. L1-046. 整除光棍(模拟除法)

    题意: 这里所谓的“光棍”,并不是指单身汪啦~ 说的是全部由1组成的数字,比如1.11.111.1111等.传说任何一个光棍都能被一个不以5结尾的奇数整除.比如,111111就可以被13整除. 现在, ...

  2. Intel欲与AMD共同做大PC市场

    自从2017年发布锐龙处理器以来,AMD在高性能处理器市场上正在恢复失地,CPU市场份额在今年Q1季度已经提升到了13.3%,要知道一年前不过8.6%而已.前面两代锐龙处理器相比Intel酷睿在单核性 ...

  3. 第1节 IMPALA:1、impala的基本介绍

    impala的介绍: impala是cloudera公司开源提供的一款高效率的sql查询工具 impala可以兼容hive的绝大多数的语法,可以完全的替代表hive impala与hive的关系:紧耦 ...

  4. MySQL设置各类字符集

    一.查看字符集编码: 登录mysql show variables like '%character%'; 二.修改编码: 编辑/etc/my.cnf ,设置后的配置文件如下: [root@node0 ...

  5. Android的事件处理机制之基于监听的事件处理

    无论是桌面应用还是手机应用程序,面对用户的使用,经常需要处理的便是用户的各种动作,也就是需要为用户动作提供响应,这种为用户动作提供响应的机制就是事件处理. 而Android为我们提供了两套强大的响应机 ...

  6. 二进制中1的个数(n=(n&n-1))

    题目描述 输入一个整数,输出该数二进制表示中1的个数.其中负数用补码表示. 解题:利用Java系统提供的函数Integer.toBinaryString(n),将整数转化为二进制,之后再将二进制的0用 ...

  7. 剑指offer,双指针法,vector输出不完美

    原因:由于在第一个res push_back给allRes的时候allRes的列已经确定,所以在输出的时候会输出一些多余的东西,在输出的时候不好处理 #include <iostream> ...

  8. git杂碎汇总

    1. .gitignore文件使用 1.如果不想某类文件(编译后.配置等文件)加入到git版本管理,可以在这个文件中配置规则,进行过滤筛选: 2.配置规则 以斜杠"/"表示目录:e ...

  9. Spark 资料整理

    http://jerryshao.me/architecture/2013/10/08/spark-storage-module-analysis/ http://blog.csdn.net/aliv ...

  10. java学习之IO流(学习之旅,一)

    个人在学习IO流的时候看到如下所示java 流类图结构的时候,我的感想是,这么多··处于蒙的状态. Java流类图结构 这么多,没有分类不好学,那我们就慢慢一口一口的吃,这样每天学习一点就好了,其实很 ...