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

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


该死行走数组写错了该死该死该死
//
// main.cpp
// poj2488
//
// Created by Candy on 9/27/16.
// Copyright © 2016 Candy. All rights reserved.
// #include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
const int N=;
inline int read(){
char c=getchar();int x=,f=;
while(c<''||c>''){if(c=='-')f=-;c=getchar();}
while(c>=''&&c<=''){x=x*+c-'';c=getchar();}
return x;
}
int T,n,m,sum,vis[N][N],flag=,cas=;
struct data{
int x,y;
data(int a=,int b=):x(a),y(b){}
}path[N];
int dx[]={-,,-,,-,,-,},dy[]={-,-,-,-,,,,};
void print(){
for(int i=;i<=sum;i++){
int x=path[i].x,y=path[i].y;
printf("%c%d",'A'-+y,x);
}
}
void dfs(int x,int y,int d){//printf("dfs %d %d %d\n",x,y,d);
path[d]=data(x,y);
if(d==sum){flag=;return;}
for(int i=;i<;i++){
int nx=x+dx[i],ny=y+dy[i];
if(nx>=&&nx<=n&&ny>=&&ny<=m&&!vis[nx][ny]&&!flag){
vis[nx][ny]=;
dfs(nx,ny,d+);
vis[nx][ny]=;
}
}
} int main(int argc, const char * argv[]) {
T=read();
while(T--){
n=read();m=read();
sum=n*m; flag=;
memset(vis,,sizeof(vis));
vis[][]=;
dfs(,,);
printf("Scenario #%d:\n",++cas);
if(!flag) printf("impossible");else print();
printf("\n\n");
}
return ;
}

POJ2488A Knight's Journey[DFS]的更多相关文章

  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. poj2488 A Knight's Journey

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

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

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

  4. A Knight's Journey(dfs)

    Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 25950   Accepted: 8853 Description Back ...

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

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

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

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

  7. POJ2488A Knight's Journey

    http://poj.org/problem?id=2488 题意 : 给你棋盘大小,判断马能否走完棋盘上所有格子,前提是不走已经走过的格子,然后输出时按照字典序排序的第一种路径 思路 : 这个题吧, ...

  8. POJ2248 A Knight's Journey(DFS)

    题目链接. 题目大意: 给定一个矩阵,马的初始位置在(0,0),要求给出一个方案,使马走遍所有的点. 列为数字,行为字母,搜索按字典序. 分析: 用 vis[x][y] 标记是否已经访问.因为要搜索所 ...

  9. poj2488--A Knight&#39;s Journey(dfs,骑士问题)

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

随机推荐

  1. Unsplash.it - 实用的图片占位符,支持个性化设置

    Unsplash.it 是一个使用漂亮的图像作为占位符的工具.只要把你的图像尺寸(宽与高)放到网址后面的参数中,你会得到一个占位符.你可以很容易地得到一个随机图像或者是一个模糊图像.也支持获取灰度图像 ...

  2. JS的window.location应用实例

    window.location 对象用于获得当前页面的地址 (URL),并把浏览器重定向到新的页面. Window Locationwindow.location 对象在编写时可不使用 window ...

  3. Web安全之CSRF攻击

    CSRF是什么? CSRF(Cross Site Request Forgery),中文是跨站点请求伪造.CSRF攻击者在用户已经登录目标网站之后,诱使用户访问一个攻击页面,利用目标网站对用户的信任, ...

  4. 如何使用代码或脚本启用SharePoint的备用语言

    SP的多语言,需要安装语言包,然后手工去sharepoint下启动备用语言,如下图: [网站操作]-[语言设置]: 方法一:采用powershell处理 在很多项目情况下,需要用代码进行备用语言启动. ...

  5. AdaBoost

    一直想写Adaboost来着,但迟迟未能动笔.其算法思想虽然简单"听取多人意见,最后综合决策",但一般书上对其算法的流程描述实在是过于晦涩.昨日11月1日下午,邹博在我组织的机器学 ...

  6. javascript 中 !~ 什么意思

    快过年放假了,也终于闲下来了.每天游览于各种技术文章中,这种状态好极了.下午看篇关于js的文章,其中有如下这么一段引起了我的注意. (function () { var names = []; ret ...

  7. Merry Christmas & Happy New Year!!

    圣诞快乐,新年快乐!

  8. 转使用chrome命令行:disable-web-security 实现浏览器跨域

    注意chrome 48 版本后此方法就不能用了 推荐一个新方式来实现本地跨域调试线上资源,搜索chrome插件 Allow-Control-Allow-Origin: *.https://chrome ...

  9. onMeasure流程解析

    0.预备知识 我们的手机屏幕的布局其实是嵌套的,最外层是一个phoneWindow,这个view和手机屏幕一样大,里面是一个frameLayout,再里面才是我们自己写的布局文件. 我们在绘制控件前必 ...

  10. C语言的基本输入与输出函数(全解)

    C语言的基本输入与输出函数 1.1.1 格式化输入输出函数 Turbo C2.0 标准库提供了两个控制台格式化输入. 输出函数printf() 和scanf(), 这两个函数可以在标准输入输出设备上以 ...