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

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
题意:国际象棋。然后给一个马(马走日) ,能够从随意点出发,找一条能够訪问全部格子(p*q的棋盘)的路径,注意路径假设有多条要求输出字典序最小的那条。

。

然后这个能够搜索的时候按字典序搜。

。

就是搜索方向要固定。。不能随意写了
然后其它的没什么了 直接深搜。搜到答案之后直接return ;
#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <string>
#include <cctype>
#include <vector>
#include <cstdio>
#include <cmath>
#include <deque>
#include <stack>
#include <map>
#include <set>
#define ll long long
#define maxn 116
#define pp pair<int,int>
#define INF 0x3f3f3f3f
#define max(x,y) ( ((x) > (y)) ? (x) : (y) )
#define min(x,y) ( ((x) > (y)) ? (y) : (x) )
using namespace std;
int n,m,k,ans,dir[8][2]={{-1,-2},{1,-2},{-2,-1},{2,-1},{-2,1},{2,1},{-1,2},{1,2}};
bool vis[27][27];
int sx[30],sy[30],top,ok;
void dfs(int x,int y)
{
if(ok) return ;
if(top==n*m)
{
ok=1;
for(int i=0;i<top;i++)
printf("%c%d",'A'+sy[i]-1,sx[i]);
return ;
}
for(int i=0;i<8;i++)
{
int tx=x+dir[i][0];
int ty=y+dir[i][1];
if(tx>=1&&tx<=n&&ty>=1&&ty<=m&&!vis[tx][ty])
{
vis[tx][ty]=1;sx[top]=tx;sy[top++]=ty;
dfs(tx,ty);
vis[tx][ty]=0;top--;
}
}
}
int main()
{
int T,cas=1;
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&n,&m);ok=0;
printf("Scenario #%d:\n",cas++);
memset(vis,0,sizeof(vis));top=0;
vis[1][1]=1;sx[top]=1;sy[top++]=1;
dfs(1,1);
if(!ok)
printf("impossible");
puts("");if(T)puts("");
}
return 0;
}

POJ 2488-A Knight&#39;s Journey(DFS)的更多相关文章

  1. poj 2488 A Knight&#39;s Journey(dfs+字典序路径输出)

    转载请注明出处:http://blog.csdn.net/u012860063?viewmode=contents 题目链接:http://poj.org/problem? id=2488 ----- ...

  2. pku 2488 A Knight&#39;s Journey (搜索 DFS)

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

  3. POJ 2488 A Knight&#39;s Journey

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

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

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

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

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

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

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

  7. A Knight's Journey (DFS)

    题目: Background The knight is getting bored of seeing the same black and white squares again and agai ...

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

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

  9. POJ 3083 -- Children of the Candy Corn(DFS+BFS)TLE

    POJ 3083 -- Children of the Candy Corn(DFS+BFS) 题意: 给定一个迷宫,S是起点,E是终点,#是墙不可走,.可以走 1)先输出左转优先时,从S到E的步数 ...

随机推荐

  1. *[topcoder]LittleElephantAndBalls

    http://community.topcoder.com/stat?c=problem_statement&pm=12758&rd=15704 topcoder的题经常需要找规律,而 ...

  2. .NET事件的指导原则

    C#允许编写所需的各种类型的事件.但是,为了与.NET Framwork的组件相兼容,开发人员必须遵循微软为此建立的一系列指导原则.这些指导原则的核心是,事件处理程序必须拥有两个参数.第一个参数是一个 ...

  3. ANDROID_MARS学习笔记_S01原始版_021_MP3PLAYER001_下载mp3文件

    一.简介 1.在onListItemClick()中new Intent,Intent以存储序列化后的mp2Info对象作为参数,启动serivce 2.DownloadService在onStart ...

  4. Ember.js demo2

    <!DOCTYPE html> <html> <head> <script src="http://code.jquery.com/jquery-1 ...

  5. 微小说《tree》

    自己一直是一个矛盾纠结的人,计算机专业,却喜欢画画,偶尔会写些短文,寓言,或者酸酸的情诗.. 废话不多说,各位看官有兴趣便随意看看,若有些于共鸣,便共勉之. 正文 两株幼苗从土壤中破土而出,一颗天生强 ...

  6. 应付发票审批 Hold and Release Names

    (N) AP > Setup > Invoice > Hold and Release Names 这里一般默认就可以了.Use the Invoice Hold and Relea ...

  7. 宣布正式发布 Azure 媒体服务内容保护服务

    Mingfei Yan Azure媒体服务项目经理 我们非常高兴地宣布正式发布 Azure 媒体服务内容保护服务.这包括 Microsoft PlayReady许可服务和 AES明文密钥交付服务!此外 ...

  8. BrnShop开源网上商城第三讲:插件的工作机制

    这几天BrnShop的开发工作比较多,所以这一篇文章来的晚了一些,还请大家见谅呀!还有通知大家一下BrnShop1.0.312版本已经发布,此版本添加了报表统计等新功能,需要源码的园友可以点此下载.好 ...

  9. Linux下的iwpriv(iwlist、iwconfig)的简单应用

    无线网络飞速发展的今天,许多设备都提供了连接无线网络的功能. 那么Linux下的wifi到底该怎么配置.连接呢?? 开始配置之前,我们要说说iw家族.iw是linux下常用的wifi配置工具,网上有相 ...

  10. 一致性哈希算法原理及Java实现

     一致性哈希算法在1997年由麻省理工学院提出的一种分布式哈希(DHT)实现算法,设计目标是为了解决因特网中的热点(Hot spot)问题,初衷和CARP十分类似.一致性哈希修正了CARP使用的简 单 ...