POJ2488A Knight's Journey[DFS]
| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions: 41936 | Accepted: 14269 |
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
该死行走数组写错了该死该死该死
//
// main.cpp
// poj2488
//
// Created by Candy on 9/27/16.
// Copyright © 2016 Candy. All rights reserved.
// #include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
const int N=;
inline int read(){
char c=getchar();int x=,f=;
while(c<''||c>''){if(c=='-')f=-;c=getchar();}
while(c>=''&&c<=''){x=x*+c-'';c=getchar();}
return x;
}
int T,n,m,sum,vis[N][N],flag=,cas=;
struct data{
int x,y;
data(int a=,int b=):x(a),y(b){}
}path[N];
int dx[]={-,,-,,-,,-,},dy[]={-,-,-,-,,,,};
void print(){
for(int i=;i<=sum;i++){
int x=path[i].x,y=path[i].y;
printf("%c%d",'A'-+y,x);
}
}
void dfs(int x,int y,int d){//printf("dfs %d %d %d\n",x,y,d);
path[d]=data(x,y);
if(d==sum){flag=;return;}
for(int i=;i<;i++){
int nx=x+dx[i],ny=y+dy[i];
if(nx>=&&nx<=n&&ny>=&&ny<=m&&!vis[nx][ny]&&!flag){
vis[nx][ny]=;
dfs(nx,ny,d+);
vis[nx][ny]=;
}
}
} int main(int argc, const char * argv[]) {
T=read();
while(T--){
n=read();m=read();
sum=n*m; flag=;
memset(vis,,sizeof(vis));
vis[][]=;
dfs(,,);
printf("Scenario #%d:\n",++cas);
if(!flag) printf("impossible");else print();
printf("\n\n");
}
return ;
}
POJ2488A 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 ...
- poj2488 A Knight's Journey
A Knight's Journey Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 24840 Accepted: ...
- 迷宫问题bfs, A Knight's Journey(dfs)
迷宫问题(bfs) POJ - 3984 #include <iostream> #include <queue> #include <stack> #incl ...
- A Knight's Journey(dfs)
Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 25950 Accepted: 8853 Description Back ...
- 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 dfs+路径打印
Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 45941 Accepted: 15637 Description Bac ...
- POJ2488A Knight's Journey
http://poj.org/problem?id=2488 题意 : 给你棋盘大小,判断马能否走完棋盘上所有格子,前提是不走已经走过的格子,然后输出时按照字典序排序的第一种路径 思路 : 这个题吧, ...
- POJ2248 A Knight's Journey(DFS)
题目链接. 题目大意: 给定一个矩阵,马的初始位置在(0,0),要求给出一个方案,使马走遍所有的点. 列为数字,行为字母,搜索按字典序. 分析: 用 vis[x][y] 标记是否已经访问.因为要搜索所 ...
- poj2488--A Knight's Journey(dfs,骑士问题)
A Knight's Journey Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 31147 Accepted: 10 ...
随机推荐
- 【HTML点滴】WWW简介
www 什么是WWW www(world wide web),又称为万维网,或通常称为web,是一个基于超文本方式的信息检索服务工具. WWW的工作模式 C/S结构(client/server结构), ...
- MySQL支持的数据类型
1.整型 MySQL数据类型 含义(有符号) tinyint(m) 1个字节 范围(-128~127) smallint(m) 2个字节 范围(-32768~32767) mediumint(m) 3 ...
- CSS学习总结(二)
一.id及class选择符 id和class的名称是由用户自定义的.id号可以唯一地标识html元素,为元素指定样式.id选择符以#来定义. 1.id选择符 注:在网页中,每个id名只能是唯一不重 ...
- 关于WPF中文件夹浏览对话框的方式
文件夹浏览时dialogresult要写全引用路径 string path=null; FolderBrowserDialog fbd = new FolderBrowserDialog(); fbd ...
- AE_复制当前图层
private void 复制ToolStripMenuItem_Click(object sender, EventArgs e) { int layercount = axMapControl2. ...
- entityframework lamda 使用where时的注意事项
我在项目中做了个底层 访问数据库泛型类 BaseEFDao<T> 在获取实体模型的时候使用了 Entities.CreateObjectSet<T>().Where(Func& ...
- iOS 检查更新
注意:苹果官方是不允许app具有检查更新提示! //直接跳转到AppStore - (void)setUpAppUpdate { [ServerData queryGetURL:@{@" ...
- 看苹果官方API
command+shift+0会出现如下图 然后输入你想找的API 记得找带Reference这种标记的文档
- MVC 生成图片,下载文件
/// <summary> /// 生成图片 /// </summary> /// <param name="collection"></ ...
- 一位资深开发的个人经历 【转自百度贴吧 java吧 原标题 4年java 3年产品 现在又开始做android了】
楼主2007年从一家天津的三流大学毕业.毕业前报了一个职位培训,毕业后可以推荐工作.因为推荐的公司都是北京的,所以就来北京了. 找了一个月工作,没有找到要我的,就在出租屋里宅了起来,打着考研的旗号,又 ...