POJ2488A Knight's Journey[DFS]
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 41936 | Accepted: 14269 |
Description
![](http://poj.org/images/2488_1.jpg)
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 ...
随机推荐
- Ajax中get和post使用问题
使用get遇到的问题: 1.问题一. 缓存:当每次访问的url相同,客户端直接读取本地缓存里面的内容,即使后台数据变化前台也不会有变化: 解决方法:在?后面链接一个num=[随机数Math.rando ...
- 认识基本的mfc控件
几乎可以在每个windows程序中都看到按钮.复选框.文本框以及下拉列表等等,这些都是控件.而且很多常用的控件已经内置到操作系统当中了,在Visual C++中,这些常用控件已经简答到能用“拖放”这种 ...
- java类 对象 和构造方法
github地址:https://github.com/lily1010/java_learn/tree/master/dog java中对象和类 java中万物皆对象,比如说动物,里面有猫,狗,鱼等 ...
- AWS EC2 复制实例后,自定义指标无法显示数据
从一个实例创建了一个AMI,然后通过这个AMI创建新的EC2实例,结果发票自定义指标不会显示: 系统一直在邮件中提示: print() on closed filehandle MDATA at Cl ...
- -[UIViewController _loadViewFromNibNamed:bundle:]
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '-[UIViewC ...
- java 中 return 的两种常见的用法
一:return语句总是用在方法中,有两个作用: 一个是返回方法指定类型的值(这个值总是确定的), 一个是结束方法的执行(仅仅一个return语句). 二:实例1 -- 返回一个String priv ...
- kubernetes听云实战发布版
➠更多技术干货请戳:听云博客 听云线上使用k8s已经有一段时间了,下面对一些听云使用过程中的问题进行一些梳理,包括架构设计,安装部署和后期维护. 目录结构如下: 下面进入正题: 我们以听云系统的一个报 ...
- iOS中的小知识点
1.tableView隐藏滚动条 self.tableView.showsVerticalScrollIndicator = NO; 2.关于属性 使用assign: 对基础数据类型 (NSI ...
- sshd安装
centos yum install openssh-server #chkconfig --level 2345 sshd on #service sshd restart 重新启动 #netsta ...
- zh-Hans vs.net 通过 管理nuget程序包下载简体中文语言包 zh-cn
zh-Hans vs.net 通过 管理nuget程序包下载简体中文语言包 在搜索中输入:zh-hans