描述
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.
输入
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, . . .
输出
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.
样例输入

3
1 1
2 3
4 3

样例输出

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

题目大意:给定棋盘长和宽,一马从左上角开始,问能否遍历棋盘,如果能,输出字典序最小的路径,如果不能,输出“impossible”

这道题很像马走日(我的博客里有),只是要输出路径,还要字典序最小的,所以要注意这匹马优先选择的走法,其他没什么难度

代码如下:

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
int k,m,n,w[8]={-1,1,-2,2,-2,2,-1,1},u[8]={-2,-2,-1,-1,1,1,2,2};//注意这里的优先选择路径
int a[1001][2];
int v[100][100];
bool check(int x,int y)
{
if(x>=1&&x<=m&&y>=1&&y<=n&&!v[x][y])
return 1;
return 0;
}
void find(int x,int y,int s)
{
int i;
if(k==0)
{
a[s][0]=x;
a[s][1]=y;
if(s==m*n)
{
k=1;
return;
}
}
for(i=0;i<8;i++)
if(check(x+w[i],y+u[i]))
{
v[x][y]=1;
find(x+w[i],y+u[i],s+1);
v[x][y]=0;
}
}
int main()
{
int i,j,p;
scanf("%d",&p);
for(i=0;i<p;i++)
{
k=0;
scanf("%d%d",&m,&n);
find(1,1,1);
printf("Scenario #%d:\n",i+1);
if(k==0)
printf("impossible\n\n");
else
{
for(j=1;j<=m*n;j++)
printf("%c%d",a[j][1]+64,a[j][0]);
printf("\n\n");
}
}
}

难度不大,注意细节

NOI2.5 1490:A Knight's Journey的更多相关文章

  1. POJ2488A Knight's Journey[DFS]

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

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

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

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

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

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

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

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

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

  7. poj2488 A Knight's Journey

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

  8. TOJ 1702.A Knight's Journey

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

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

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

随机推荐

  1. [Linux] 使用awk比较两个文件的内容

    当需要比较A , B两个文件 , A文件中存在 , 并且把也在B文件中存在的行去除掉 , 可以使用这个awk的用法来 awk  '{if(ARGIND==1) {val[$0]}else{if($0 ...

  2. linux包之dracut

    这是一个工具类,不是一个后台服务类 centos7.2-minimal就下面三个包 [root@1st-kvm ~]# rpm -qa|grep dracutdracut-config-rescue- ...

  3. ArcGIS-PictureMarkerSymbol-向地图添加图片标记

    1.基于4.13 版本 <link rel="stylesheet" href="https://js.arcgis.com/4.13/esri/themes/li ...

  4. 【Kubernetes】容器集群管理常用命令笔记

    一.集群部署-查询集群状态 ①查询k8s master各组件健康状态: kubectl get componentstatus ②查询k8s node健康状态: kubectl get node 二. ...

  5. 精选腾讯技术干货200+篇,云加社区全年沙龙PPT免费下载!

    2019年已经过去,小编为大家整理了这一年以来云加社区发布的 200多篇腾讯干货,点击文章标题即可跳转到原文,请速速收藏哦~ 看腾讯技术: 腾讯成本优化黑科技:整机CPU利用率最高提升至90%: 腾讯 ...

  6. 洛谷$P$4137 $Rmq\ Problem / mex$ 主席树

    正解:主席树 解题报告: 传送门$QwQ$ 本来以为是道入门无脑板子题,,,然后康了眼数据范围发现并没有我想像的那么简单昂$kk$ 这时候看到$n$的范围不大,显然考虑离散化?但是又感觉似乎布星?因为 ...

  7. 小小知识点(二十)利用MATLAB计算定积分

    一重定积分 1. Z = trapz(X,Y,dim) 梯形数值积分,通过已知参数x,y按dim维使用梯形公式进行积分 %举例说明1 clc clear all % int(sin(x),0,pi) ...

  8. SpringBoot 总结篇

            时至今日,SpringBoot 系列文章也算是告一段落,回想起当初立flag的情景,仿佛还历历在目.用一个月时间学完 SpringBoot 并整理成文章?又定一些异想天开计划,当时这样 ...

  9. Django之表高级操作

    目录 一.如何开启自己的测试脚本? 二.对表数据的添加.更新.删除 1.create() 2.update() 3.delete() 4.查看执行的sql语句 三. 单表查询13个操作 返回Query ...

  10. spring boot集成spring-boot-starter-mail邮件功能

    前情提要 以目前IT系统功能来看,邮件功能是非常重要的一个功能.例如:找回密码.邮箱验证,邮件动态码.忘记密码,邮件营销等,都需要用到邮件功能.结合当下最流行的spring boot微服务,推出了sp ...