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 直接DFS就好,只是要记住路径:
#include <iostream>
#include <string.h>
#include <stdio.h> using namespace std;
int dx[]={-1, 1, -2, 2, -2, 2, -1, 1}, dy[] = {-2, -2, -1, -1, 1, 1, 2, 2};
int path[30][30], vis[30][30], p, q, cnt;
bool flag; void DFS(int r, int c, int sp)
{
path[sp][0] = r ;
path[sp][1] = c ;
if(sp == p*q )
{
flag = 1 ;
return ;
}
for(int i=0; i<8; i++)
{
int x = r + dx[i] ;
int y = c + dy[i] ;
if(x>=1 && x<=p && y>=1 && y<=q && !vis[x][y] && !flag)
{
vis[x][y] = 1 ;
DFS(x,y,sp+1);
vis[x][y] = 0 ;
}
}
} int main()
{
int n, k;
cin >> n ;
for(k=1; k<=n; k++)
{
flag = 0 ;
cin >> p >> q ;
memset(vis,0,sizeof(vis));
vis[1][1] = 1;
DFS(1,1,1);
cout << "Scenario #" << k << ":" << endl ;
if(flag)
{
for(int i=1; i<=p*q; i++)
printf("%c%d",path[i][1]-1+'A',path[i][0]);
}
else cout << "impossible" ;
cout << endl ;
if(k!=n) cout << endl ;
} return 0;
}

ACM题目————A Knight's Journey的更多相关文章

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

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

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

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

  3. TOJ 1702.A Knight's Journey

    2015-06-05 问题简述: 有一个 p*q 的棋盘,一个骑士(就是中国象棋里的马)想要走完所有的格子,棋盘横向是 A...Z(其中A开始 p 个),纵向是 1...q. 原题链接:http:// ...

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

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

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

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

  6. POJ2488A Knight's Journey[DFS]

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

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

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

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

  9. A Knight's Journey 分类: dfs 2015-05-03 14:51 23人阅读 评论(0) 收藏

    A Knight’s Journey Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 34085 Accepted: 11621 ...

随机推荐

  1. Django之数据聚合函数 annotate

    在我们的博客侧边栏有分类列表,显示博客已有的全部文章分类.现在想在分类名后显示该分类下有多少篇文章,该怎么做呢?最优雅的方式就是使用 Django 模型管理器的annotate方法. 模型回顾 回顾一 ...

  2. 扩展Spring切面

    概述 Spring的切面(Spring动态代理)在Spring中应用十分广泛,例如还有事务管理,重试等等.网上介绍SpringAop源码很多,这里假设你对SpringAop有基本的了解.如果你认为Sp ...

  3. [Oracle]Oracle之Chr函数返回

    Chr函数 返回:返回 String,其中包含有与指定的字符代码相关的字符. chr('39')是单引号 Chr("0") 为0的字符 Chr("1") Chr ...

  4. 图论——最小生成树_prim

    今天是最小生成树的prim的算法,因为本人水平有限所以堆优化都不是很会啊,但邻接表好像出了点小差错所以上邻接矩阵比较好一点,尽管比Kruskal慢了很多很多但这种贪心思想还是要学习的.从第一条边开始取 ...

  5. 【python-opencv】17-形态学操作-腐蚀与膨胀

    形态学操作其实就是改变物体的形状,比如腐蚀就是"变瘦",膨胀就是"变胖",看下图就明白了: 形态学操作一般作用于二值化图(也可直接作用于原图),来连接相邻的元素 ...

  6. ICMP报文

    类型:表示ICMP消息类型 代码:表示同一消息的不同信息 其他是时间戳或者标识符及序列号 类型 编码 描述   0 0 Echo Reply 3 0 网络不可达 3 1 主机不可达 3 2 协议不可达 ...

  7. CentOS配置JDK环境

    1 .下载JDK,jdk1.7.0_80.gz 2. mkdir -p /usr/lib/java 3.sudo chmod -R 777 /usr/lib/java 4.tar -zxvf jdk1 ...

  8. ConcurrentLinkedQueue

    我们要实现一个线程安全的队列有两种实现方式,阻塞算法.非阻塞算法.使用阻塞算法的队列可以用一个锁(入队和出队用同一把锁) 或两个锁(入队和出队用不同的锁)等方式来实现,而非阻塞的实现方式则可以使用循环 ...

  9. servlet输出请求头

    1.参考 Enumeration headerNames = req.getHeaderNames(); while(headerNames.hasMoreElements()) { String h ...

  10. 把web项目部署到阿里云linux服务器上

    最近弄了个试用阿里云服务器倒腾了半天终于部署好,分享一下. 1.登入阿里云打开你申请的是云服务器的实例: 点击重置密码---重置密码后重启服务器才能生效(一般需要重置密码.这里设置的密码是使用xhel ...