poj2488--A Knight's Journey(dfs,骑士问题)
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 31147 | Accepted: 10655 |
Description

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
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
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
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int t , n , m , sum ;
int mm[30][30] ;
int pre[1000] ;
int a[8][2] = { {-2,-1},{-2,1},{-1,-2},{-1,2},{1,-2},{1,2},{2,-1},{2,1} };
int dfs(int x,int y,int temp)
{
if( temp == sum )
return 1 ;
int flag = 0 , xx , yy , i ;
for(i = 0 ; i < 8 ; i++)
{
xx = x + a[i][0] ;
yy = y + a[i][1] ;
if( xx >= 0 && xx < n && yy >= 0 && yy < m && !mm[xx][yy] )
{
mm[xx][yy] = 1 ;
pre[x*m+y] = xx*m+yy ;
flag = dfs(xx,yy,temp+1);
if( flag ) return flag ;
mm[xx][yy] = 0 ;
}
}
return flag ;
}
int main()
{
int i , j , k , tt ;
scanf("%d", &t);
for(tt = 1 ; tt <= t ; tt++)
{
memset(mm,0,sizeof(mm));
memset(pre,-1,sizeof(pre));
scanf("%d %d", &m, &n);
sum = n*m ;
mm[0][0] = 1 ;
k = dfs(0,0,1);
printf("Scenario #%d:\n", tt);
if(k == 0)
printf("impossible");
else
{
for(i = 0 , k = 0 ; i < sum ; i++)
{
printf("%c%c", k/m+'A', k%m+'1');
k = pre[k] ;
}
}
printf("\n\n");
}
return 0;
}
poj2488--A Knight's Journey(dfs,骑士问题)的更多相关文章
- POJ 2488-A Knight's Journey(DFS)
A Knight's Journey Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 31702 Accepted: 10 ...
- DFS深搜——Red and Black——A Knight's Journey
深搜,从一点向各处搜找到全部能走的地方. Problem Description There is a rectangular room, covered with square tiles. Eac ...
- pku 2488 A Knight's Journey (搜索 DFS)
A Knight's Journey Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 28697 Accepted: 98 ...
- poj 2488 A Knight's Journey(dfs+字典序路径输出)
转载请注明出处:http://blog.csdn.net/u012860063?viewmode=contents 题目链接:http://poj.org/problem? id=2488 ----- ...
- POJ 2488 A Knight's Journey
A Knight's Journey Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 29226 Accepted: 10 ...
- POJ2488:A Knight's Journey(dfs)
http://poj.org/problem?id=2488 Description Background The knight is getting bored of seeing the same ...
- poj2488 A Knight's Journey裸dfs
A Knight's Journey Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 35868 Accepted: 12 ...
- poj 2488 A Knight's Journey 【骑士周游 dfs + 记忆路径】
题目地址:http://poj.org/problem?id=2488 Sample Input 3 1 1 2 3 4 3 Sample Output Scenario #1: A1 Scenari ...
- POJ2488A Knight's Journey[DFS]
A Knight's Journey Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 41936 Accepted: 14 ...
随机推荐
- python 一些函数和类用法记录
这一篇主要用来记录在学习过程中遇到的一些觉得有意思的函数或者类的用法,有一些用法感觉很炫酷. 1.collections.defaultdict from collections import def ...
- 生产环境屏蔽swagger(动态组装bean)
spring动态组装bean 背景介绍: 整合swagger时需要在生产环境中屏蔽掉swagger的地址,不能在生产环境使用 解决方案 使用动态profile在生产环境中不注入swagger的bean ...
- CSS--基础块级元素与内联元素
在CSS中,html中的标签元素大体被分为三种不同的类型:块状元素.内联元素(又叫行内元素)和内联块状元素.在HTML和XHTML中,块级元素不能继承自行内元素(即不能嵌套在行内元素),<p&g ...
- soc desgin 目前需要做的事情
1.熟练的画时序图 达到一旦有想法可以立即通过时序图表达出来. 2.下面是项目中经常用到的典型设计 2.1串并互相转换 2.2cdc 2.3握手协议 2.4cgc(门控时钟) 2.5AHB2reg文件 ...
- Python中的函数(2)
一.实参和形参 def greet_user(username): """显示简单的问候语,且显示用户名""" print(& ...
- LeetCode(77) Combinations
题目 Given two integers n and k, return all possible combinations of k numbers out of 1 - n. For examp ...
- POJ 1655 Balancing Act (树形DP求树的重心)
题意: 求一棵树中以某个点为重心最小的子树集, 就是去掉这个点, 图中节点最多的联通块节点最少. 分析: 想知道这个点是不是最优的点, 只要比较它子树的数量和除去这部分其他的数量(它的父节点那部分树) ...
- 关于markdown 的简单使用(已更新)
markdown的介绍 Markdown是一种可以使用普通文本编辑器编写的标记语言,通过类似HTML的标记语法,它可以使普通文本内容具有一定的格式. Markdown具有一系列衍生版本,用于扩展Mar ...
- redis异常信息:MISCONF Redis is configured to save RDB snapshots, but it is currently not able to persist on disk.。。。。
redis.exceptions.ResponseError: MISCONF Redis is configured to save RDB snapshots, but it is current ...
- 查看linux ubuntu版本
ubuntu版本: lsb_release -a linux版本: uname -a proc目录下记录的当前系统运行的各种数据,version记录的版本信息直接可以通过cat查看到. 使用命令:c ...