poj2488 A Knight's Journey裸dfs
| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions: 35868 | Accepted: 12227 |
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
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
Source

搞清楚一个字典序就行了,其余的很简单。Posted by xijunlee93 at 2013-03-19 16:37:46 on Problem 2488 这一题的字典序:就是先按列排序,较小的在前。然后按行排序,也是较小的在前。 |
大意很明了,就是找到一个路径让马走完所有的点,不重复不遗漏;思路很容易找到,直接用DFS搜索标记并回溯,一个点一个点作为起点去试;找到后停止;
#include<stdio.h>
int dir[][]={-,-,-,,-,-,-,,,-,,,,-,,}; //记录方向
int g,a,b;//g用来记录是否找到解,找到后不再搜索
int vist[][],path[][];
void find(int i,int j,int k)//i,j是要走的格子,k记录已经走过的步数
{
if(k==a*b)//走完了
{
for(int i=;i<k;i++)
printf("%c%d",path[i][]+'A',path[i][]+);
printf("\n");
g=;
}
else
for(int x=;x<;x++)//8个方向依次搜索
{
int n=i+dir[x][];
int m=j+dir[x][];
if(n>=&&n<b&&m>=&&m<a&&!vist[n][m]&&!g)
{
vist[n][m]=;//标记已走
path[k][]=n,path[k][]=m;
find(n,m,k+);
vist[n][m]=;//清除标记
}
}
}
int main()
{
int n;
scanf("%d",&n);
for(int m=;m<n;m++)
{
g=;
scanf("%d %d",&a,&b);
for(int i=;i<a;i++)//一个点一个点的尝试
for(int j=;j<b;j++)
vist[i][j]=;
vist[][]=;
path[][]=,path[][]=;
printf("Scenario #%d:\n",m+);
find(,,);
if(!g) printf("impossible\n");
printf("\n");
}
return ;
}
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
using namespace std;
const int maxn=;
bool vis[maxn][maxn];
int path[][];
int n,m;
int next[][]={-,-,-,,-,-,-,,,-,,,,-,,};
bool flag;
void dfs(int x,int y,int step){
if(step==m*n){
flag=true;
for(int i=;i<step;i++){
printf("%c%d",path[i][]+'A'-,path[i][]);
}
printf("\n");
}
else
for(int k=;k<;k++){
int tx=x+next[k][];
int ty=y+next[k][];
if(tx>=&&tx<=n&&ty>=&&ty<=m&&!vis[tx][ty]&&!flag){
vis[tx][ty]=true;
path[step][]=tx;
path[step][]=ty;
dfs(tx,ty,step+);
vis[tx][ty]=false;
}
} } int main(){
int t;
scanf("%d",&t);
int Case=;
while(t--){
Case++;
memset(vis,false,sizeof(vis));
memset(path,,sizeof(path));
scanf("%d%d",&n,&m);
flag=false;
vis[][]=true;
path[][]=;
path[][]=;
printf("Scenario #%d:\n",Case);
dfs(,,);
if(!flag)
printf("impossible\n"); printf("\n");
}
return ;
}
大意很明了,就是找到一个路径让马走完所有的点,不重复不遗漏;思路很容易找到,直接用DFS搜索标记并回溯,一个点一个点作为起点去试;找到后停止;
poj2488 A Knight's Journey裸dfs的更多相关文章
- 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: 1000MSMemory Limit: 65536K Total Submissions: 34633Accepted: 11815 De ...
- 快速切题 poj2488 A Knight's Journey
A Knight's Journey Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 31195 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 ...
- poj-2488 a knight's journey(搜索题)
Time limit1000 ms Memory limit65536 kB Background The knight is getting bored of seeing the same bla ...
- POJ 2488 A Knight's Journey (DFS)
poj-2488 题意:一个人要走遍一个不大于8*8的国际棋盘,他只能走日字,要输出一条字典序最小的路径 题解: (1)题目上说的"The knight can start and end ...
- A Knight's Journey (DFS)
题目: Background The knight is getting bored of seeing the same black and white squares again and agai ...
- poj 2488 A Knight's Journey( dfs )
题目:http://poj.org/problem?id=2488 题意: 给出一个国际棋盘的大小,判断马能否不重复的走过所有格,并记录下其中按字典序排列的第一种路径. #include <io ...
- POJ2488 A Knight's Journey
题目:http://poj.org/problem?id=2488 题目大意:可以从任意点开始,只要能走完棋盘所有点,并要求字典序最小,不可能的话就impossible: 思路:dfs+回溯,因为字典 ...
随机推荐
- coursera_ML_1
机器学习定义: A computer program is said to leran from experience E with respect to some task T and some ...
- js中arr.sort的用法
sort(sortfunction)为JS的数组对象(Array)的一个方法,提供排序功能 参数 sortFunction 为可选项,是用来确定排序原则的js函数, 这个函数有两个参数,分别代表每次排 ...
- UVA 12166 Equilibrium Mobile(贪心,反演)
直接贪心.先想想最后平衡的时候,如果知道了总重量,那么每一个结点的重量其实也就确定了. 每个结点在左在右其实都不影响,只和层数有关.现在反过来,如果不修改某个结点,那么就可以计算出总质量,取总质量出现 ...
- Android(java)学习笔记90:TextView 添加超链接(两种实现方式)
1. TextView添加超链接: TextView添加超链接有两种方式,它们有区别于WebView: (1)方式1: LinearLayout layout = new LinearLayout(t ...
- 【BZOJ4866】[YNOI2017] 由乃的商场之旅(莫队)
点此看题面 大致题意: 给你一个字符串,每次给你一段区间,问这段区间内有多少个字符串在重新排列后可以变成一个回文串. 关于莫队 详见这篇博客:莫队算法学习笔记(一)--普通莫队. 关于回文 要使一个字 ...
- vuejs计算属性和侦听器
<div id='root'> 姓:<input v-model='firstName'/> 名:<input v-model='secondName'/> < ...
- PHP 的那些错误总结
问题1: [root@web01 data]# lsof -i :9000 [root@web01 data]# /application/php/sbin/php-fpm /application ...
- python_44_文件属性
#打印文件属性信息 import os#os.stat()返回的文件属性元组元素的含义 filestats=os.stat('yesterday')#获取文件/目录的状态 print(filestat ...
- Matlab将多幅图片保存为mat
%% 储存某目录所有的图片 pt = 'd:\imgs\'; ext = '*.jpg'; dis = dir([pt ext]); nms = {dis.name}; for k = 1:lengt ...
- Quartz 配置文件属性
主要配置 Property Name Req'd Type Default Value org.quartz.scheduler.instanceName no string 'QuartzSched ...