A Knight's Journey (DFS)
题目:
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
Output
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的棋盘,跳马在上面的任意一格开始移动,只能走‘日’字,问你能不能经过棋盘上面所有的格子;(百度的题意,明明是说经过所有的格子,有道硬是翻译
成了“找到一条这样的路,骑士每一次都要去一次”),输出要按照字典顺序输出 分析:
深度优先搜索,要经过所有的格子,那就肯定经过(1,1),所以就可以从(1,1)开始搜索; AC代码:
#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
int t,p,q,flag;
int a[30][30];
int step[30][30];
int f[8][2]={{1,-2},{-1,-2},{-2,-1},{2,-1},{-2,1},{2,1},{-1,2},{1,2}};
void dfs(int x,int y,int z)
{
step[z][1]=x;
step[z][2]=y;
if (z==p*q)
{
flag=1;
return ;
}
for (int i=0;i<8;i++)
{
int xi=x+f[i][0];
int yi=y+f[i][1];
if (xi>=1&&xi<=p&&yi>=1&&yi<=q&&!a[xi][yi]&&!flag)
{
a[xi][yi]=1;
dfs(xi,yi,z+1);
a[xi][yi]=0;
}
}
}
int main()
{
cin>>t;
for (int i=1;i<=t;i++)
{
flag=0;
scanf("%d%d",&p,&q);
memset(a,0,sizeof(a));
memset(step,0,sizeof(step));
a[1][1]=1;
dfs(1,1,1);
printf("Scenario #%d:\n",i);
if (flag==1)
{
for (int j=1;j<=p*q;j++)
printf("%c%d",step[j][2]+'A'-1,step[j][1]);
printf("\n");
}
else
printf("impossible\n");
if (i!=t)
printf("\n");
}
return 0;
}
A Knight's Journey (DFS)的更多相关文章
- POJ 2488 A Knight's Journey(DFS)
A Knight's Journey Time Limit: 1000MSMemory Limit: 65536K Total Submissions: 34633Accepted: 11815 De ...
- POJ 2488 A Knight's Journey (DFS)
poj-2488 题意:一个人要走遍一个不大于8*8的国际棋盘,他只能走日字,要输出一条字典序最小的路径 题解: (1)题目上说的"The knight can start and end ...
- poj 2488 A Knight's Journey( dfs )
题目:http://poj.org/problem?id=2488 题意: 给出一个国际棋盘的大小,判断马能否不重复的走过所有格,并记录下其中按字典序排列的第一种路径. #include <io ...
- POJ2488-A Knight's Journey(DFS+回溯)
题目链接:http://poj.org/problem?id=2488 A Knight's Journey Time Limit: 1000MS Memory Limit: 65536K Tot ...
- POJ 2488-A Knight's Journey(DFS)
A Knight's Journey Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 31702 Accepted: 10 ...
- LeetCode Subsets II (DFS)
题意: 给一个集合,有n个可能相同的元素,求出所有的子集(包括空集,但是不能重复). 思路: 看这个就差不多了.LEETCODE SUBSETS (DFS) class Solution { publ ...
- LeetCode Subsets (DFS)
题意: 给一个集合,有n个互不相同的元素,求出所有的子集(包括空集,但是不能重复). 思路: DFS方法:由于集合中的元素是不可能出现相同的,所以不用解决相同的元素而导致重复统计. class Sol ...
- HDU 2553 N皇后问题(dfs)
N皇后问题 Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Description 在 ...
- 深搜(DFS)广搜(BFS)详解
图的深搜与广搜 一.介绍: p { margin-bottom: 0.25cm; direction: ltr; line-height: 120%; text-align: justify; orp ...
随机推荐
- 试验指标|试验单位|均方|随机模型|固定模型|字母标记法|LSR|q检验|LSD|重复值|弥补缺失数据|可加性|平方根转换|对数转换|反正弦转化
第五章 方差分析 试验指标是什么? 就是统计的测量值,eg:身高体重 试验单位( experimental unit )是什么? 实验载体,比如一只小白鼠 均方是什么? 就是方差 随机模型的τ有何特点 ...
- ZJNU 2212 - Turn-based game
Mr.Lee每隔1/x s攻击一次,cpu每隔1/y s攻击一次 因为时间与答案无关,最后只看boss受到了多少次攻击 所以可以在每个人的频率上同时乘以xy 即Mr.Lee每隔y s攻击一次,cpu每 ...
- F - No Link, Cut Tree! Gym - 101484F
Marge is already preparing for Christmas and bought a beautiful tree, decorated with shiny ornaments ...
- E - Ingredients 拓扑排序+01背包
题源:https://codeforces.com/gym/101635/attachments 题意: n行,每行给定字符串s1,s2,s3代表一些菜谱名.s2和s3是煮成是的必要条件,然后给出c和 ...
- C++ malloc函数
malloc的全称是memory allocation,中文叫动态内存分配,用于申请一块连续的指定大小的内存块区域以void*类型返回分配的内存区域地址,当无法知道内存具体位置的时候,想要绑定真正的内 ...
- ubuntu下pycharm的安装
打开百度,输入pycharm下载,点击下图的第二个英文链接. 进入后选择linux下的Community进行下载,而左边的Professional是要钱购买的,当然花钱的体验效果肯定会更好. 下载完成 ...
- python处理nii格式文件
网上已经有很多代码了,但是注释的都不全,看起来很费解,我自己加了一些注释,重新发出来,尽可能的通俗易懂 读取前需要先安装库 pip install nibabel pip install matplo ...
- ionic3 修改页面切换动画
在app.module.ts中 配置pageTransition属性 [ BrowserModule, IonicModule.forRoot(MyApp, { pageTransition: 'io ...
- spring cloud解决eureka的client注册时默认使用hostname而不是ip
eureka的client注册到server时默认是使用hostname而不是ip.这样的话使用feign client通过eureka进行服务间相互调用时也会使用hostname进行调用,从而调用失 ...
- LinkedList源码剖析
LinkedList简介 LinkedList是基于双向循环链表(从源码中可以很容易看出)实现的,除了可以当做链表来操作外,它还可以当做栈.队列和双端队列来使用. LinkedList同样是非线程安全 ...