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

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

Source

TUD Programming Contest 2005, Darmstadt, Germany
题意:
象棋中骑士走日,给出一个p*q棋盘,问其实能否一次走遍所有的格子,按照字典序输出路径。
代码:
 //基础dfs,用vector保存路径。
#include<iostream>
#include<vector>
#include<cstdio>
#include<cstring>
using namespace std;
int p,q,t;
const int diry[]={-,,-,,-,,-,};
const int dirx[]={-,-,-,-,,,,};
int sum;
bool vis[][];
vector<int>loadx;
vector<int>loady;
void dfs(int x,int y)
{
vis[x][y]=;
sum++;
loadx.push_back(x);
loady.push_back(y);
if(sum==p*q)
return;
for(int i=;i<;i++)
{ if(x+dirx[i]<=||x+dirx[i]>q||y+diry[i]<=||y+diry[i]>p)
continue;
if(vis[x+dirx[i]][y+diry[i]])
continue;
dfs(x+dirx[i],y+diry[i]);
if(sum==p*q)
return;
}
vis[x][y]=;
sum--;
loadx.pop_back();
loady.pop_back();
}
int main()
{
scanf("%d",&t);
for(int k=;k<=t;k++)
{
scanf("%d%d",&p,&q);
sum=;
memset(vis,,sizeof(vis));
while(!loadx.empty())
{
loadx.pop_back();
loady.pop_back();
}
for(int i=;i<=q;i++)
{
if(sum==p*q)
break;
for(int j=;j<=p;j++)
{
dfs(i,j);
if(sum==p*q)
break;
}
}
printf("Scenario #%d:\n",k);
if(sum==p*q)
{
for(int i=;i<loadx.size();i++)
{
printf("%c%d",loadx[i]+,loady[i]);
}
printf("\n\n");
}
else printf("impossible\n\n");
}
return ;
}

POJ2488 dfs的更多相关文章

  1. POJ2488:A Knight's Journey(dfs)

    http://poj.org/problem?id=2488 Description Background The knight is getting bored of seeing the same ...

  2. poj2488 A Knight's Journey裸dfs

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

  3. POJ2488【DFS】

    阿西吧,搞清楚谁是行,谁是列啊!!! #include <stdio.h> #include <string.h> #include <math.h> #inclu ...

  4. POJ2488A Knight's Journey[DFS]

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

  5. 图的遍历之深度优先搜索(DFS)

    深度优先搜索(depth-first search)是对先序遍历(preorder traversal)的推广.”深度优先搜索“,顾名思义就是尽可能深的搜索一个图.想象你是身处一个迷宫的入口,迷宫中的 ...

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

    poj-2488 题意:一个人要走遍一个不大于8*8的国际棋盘,他只能走日字,要输出一条字典序最小的路径 题解: (1)题目上说的"The knight can start and end ...

  7. BZOJ 3083: 遥远的国度 [树链剖分 DFS序 LCA]

    3083: 遥远的国度 Time Limit: 10 Sec  Memory Limit: 1280 MBSubmit: 3127  Solved: 795[Submit][Status][Discu ...

  8. BZOJ 1103: [POI2007]大都市meg [DFS序 树状数组]

    1103: [POI2007]大都市meg Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 2221  Solved: 1179[Submit][Sta ...

  9. BZOJ 4196: [Noi2015]软件包管理器 [树链剖分 DFS序]

    4196: [Noi2015]软件包管理器 Time Limit: 10 Sec  Memory Limit: 512 MBSubmit: 1352  Solved: 780[Submit][Stat ...

随机推荐

  1. Asp.Net MVC中Controller与View之间传递的Model

    Controller --> View 的Model 与 提交表单后 View --> Controller 的Model 不是相同的对象,即:这两个Model为不同的指针,指向不同的地址 ...

  2. 一个简单的Object Hook的例子(win7 32bit)

    Object Hook简单的来说就是Hook对象,这里拿看雪上的一个例子,因为是在win7 32位上的,有些地方做了些修改. _OBJECT_HEADER: kd> dt _OBJECT_HEA ...

  3. OpenGL的消隐与双缓冲

    首先是大家可能已经发现,在我们之前提到的所有例子中,在图形的旋转过程中整个图形都有一定程度的闪烁现象,显得图形的过渡极不平滑,这当然不是我们所要的效果,幸好opengl 支 持一个称为双缓存的技术,可 ...

  4. AES128和AES256主要区别和安全程度是多少?他们对于机器的消耗是怎样的?两者性能如何?实际开发如何选择?

    高级加密标准(英语:Advanced Encryption Standard,缩写:AES),在密码学中又称Rijndael加密法,是美国联邦政府采用的一种区块加密标准.这个标准用来替代原先的DES, ...

  5. JavaScript BOM对象介绍

    bom:即broswer object model(浏览器对象模型),由五个对象组成:        Window:对象表示浏览器中打开的窗口 最顶层对象.       Navigator :浏览器对 ...

  6. LoadRunner如何监控Tomcat性能

    使用LoadRunner做性能测试,一般的直觉是LR只能完成脚本录制和编写模拟用户的请求行为,但是在某些情况下,要监控一些中间件或web服务器的性能时,就不能通过录制脚本来完成了,那么就需要手工来编写 ...

  7. Sql数据库帮组类

    这段时间闲下来写了一些东西,重新写了一个简单的数据库帮组类 public class MyDBHelper { public static readonly string connString = C ...

  8. hdu 并查集分类(待续)

    hdu 1829 A Bug's Life 题目大意: 给你n个动物,输入m行a,b,表示a和b应该是异性的,要你判断是否有同性恋. 并查集中,1到n代表应性别,n+1到2n代表一个性别,合并一下,判 ...

  9. stack

    stack介绍:先进后出 实现C++STL,栈有两个参数:template<class T,class Container = deque<T>> class stack: 参 ...

  10. 同步、更新、下载Android Source & SDK from 国内镜像站

    转自: 同步.更新.下载Android Source & SDK from 国内镜像站 Download the android source from china mirrors     以 ...