poj2488 A Knight's Journey裸dfs
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 35868 | Accepted: 12227 |
Description

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+回溯,因为字典 ...
随机推荐
- 写在Github被微软收购之际 - Github的那些另类用法
这几天朋友圈被微软75亿美元收购Github的新闻刷屏了.Jerry也来贡献一篇和Github相关的文章. 这篇文章包含了Jerry平时对于Github的一些另类用法.目录如下: 1. 部署HTML应 ...
- 因技术垃圾直接上手groovy的工作感悟
因为熟悉devops,用IDEA的git完成版本提交,不太喜欢公司的代码控制,从事groovy基本没有代码控制,提交就打个压缩包给指定的人. 没有持续继承,持续部署(CICD). http://xio ...
- Codeforces Round #327 590B Chip 'n Dale Rescue Rangers(等效转换,二分)
t和可到达具有单调性,二分就不多说了.下面说下O(1)的做法,实际上是等效转换,因为答案一定存在,如果在t0之前,那么分解一下 直接按照只有v计算就可以了.反过来如果计算的结果大于t0,那么表示答案在 ...
- 【BZOJ1045】糖果传递(基于贪心的数学题)
点此看题面 大致题意: 有\(n\)个小朋友坐成一圈,每人有\(a[i]\)个糖果.每人只能给左右两人传递糖果,传递一个糖果代价为1,求使所有人获得均等糖果的最小代价. 数学转换 这题其实是一道带有浓 ...
- 【BZOJ1060】[ZJOI2007] 时态同步(树形DP)
点此看题面 大致题意: 给你一棵带权树,每次使用道具可以将某条边的边权加\(1\),问你至少需要使用多少次道具,才能使每个叶子节点到根节点的距离相等. 贪心的思想 首先,我们应该先有一个贪心的思想. ...
- yield 生成器的运行机制
yield 生成器的运行机制 当你问生成器要一个数时,生成器会执行,直至出现 yield 语句,生成器把 yield 的参数给你,之后生成器就不会往下继续运行. 当你问他要下一个数时,他会从上次的状态 ...
- java编程基础——栈压入和弹出序列
题目描述 输入两个整数序列,第一个序列表示栈的压入顺序,请判断第二个序列是否可能为该栈的弹出顺序.假设压入栈的所有数字均不相等.例如序列1,2,3,4,5是某栈的压入顺序,序列4,5,3,2,1是该压 ...
- MySQL - 在sql语句执行时是先执行触发器再检查约束条件的
在sql语句执行时是先执行触发器再检查约束条件的
- 分享自己写的基于Dapper的轻量级ORM框架~
1.说明 本项目是一个使用.NET Standard 2.0开发的,基于 Dapper 的轻量级 ORM 框架,包含基本的CRUD以及根据表达式进行一些操作的方法,目前只针对单表,不包含多表连接操作. ...
- kubernetes搭建dashboard报错
warningconfigmaps is forbidden: User "system:serviceaccount:kube-system:kubernetes-dashboard&qu ...