POJ2488 dfs
| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions: 41972 | Accepted: 14286 |
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
input begins with a positive integer n in the first line. The following
lines contain n test cases. Each test case consists of a single line
with two positive integers p and q, such that 1 <= p * q <= 26.
This represents 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
output for every scenario begins with a line containing "Scenario #i:",
where i is the number of the scenario starting at 1. Then print a
single line containing the lexicographically first path that visits all
squares 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
//基础dfs,用vector保存路径。
#include<iostream>
#include<vector>
#include<cstdio>
#include<cstring>
using namespace std;
int p,q,t;
const int diry[]={-,,-,,-,,-,};
const int dirx[]={-,-,-,-,,,,};
int sum;
bool vis[][];
vector<int>loadx;
vector<int>loady;
void dfs(int x,int y)
{
vis[x][y]=;
sum++;
loadx.push_back(x);
loady.push_back(y);
if(sum==p*q)
return;
for(int i=;i<;i++)
{ if(x+dirx[i]<=||x+dirx[i]>q||y+diry[i]<=||y+diry[i]>p)
continue;
if(vis[x+dirx[i]][y+diry[i]])
continue;
dfs(x+dirx[i],y+diry[i]);
if(sum==p*q)
return;
}
vis[x][y]=;
sum--;
loadx.pop_back();
loady.pop_back();
}
int main()
{
scanf("%d",&t);
for(int k=;k<=t;k++)
{
scanf("%d%d",&p,&q);
sum=;
memset(vis,,sizeof(vis));
while(!loadx.empty())
{
loadx.pop_back();
loady.pop_back();
}
for(int i=;i<=q;i++)
{
if(sum==p*q)
break;
for(int j=;j<=p;j++)
{
dfs(i,j);
if(sum==p*q)
break;
}
}
printf("Scenario #%d:\n",k);
if(sum==p*q)
{
for(int i=;i<loadx.size();i++)
{
printf("%c%d",loadx[i]+,loady[i]);
}
printf("\n\n");
}
else printf("impossible\n\n");
}
return ;
}
POJ2488 dfs的更多相关文章
- 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 ...
- POJ2488【DFS】
阿西吧,搞清楚谁是行,谁是列啊!!! #include <stdio.h> #include <string.h> #include <math.h> #inclu ...
- POJ2488A Knight's Journey[DFS]
A Knight's Journey Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 41936 Accepted: 14 ...
- 图的遍历之深度优先搜索(DFS)
深度优先搜索(depth-first search)是对先序遍历(preorder traversal)的推广.”深度优先搜索“,顾名思义就是尽可能深的搜索一个图.想象你是身处一个迷宫的入口,迷宫中的 ...
- POJ 2488 A Knight's Journey (DFS)
poj-2488 题意:一个人要走遍一个不大于8*8的国际棋盘,他只能走日字,要输出一条字典序最小的路径 题解: (1)题目上说的"The knight can start and end ...
- BZOJ 3083: 遥远的国度 [树链剖分 DFS序 LCA]
3083: 遥远的国度 Time Limit: 10 Sec Memory Limit: 1280 MBSubmit: 3127 Solved: 795[Submit][Status][Discu ...
- BZOJ 1103: [POI2007]大都市meg [DFS序 树状数组]
1103: [POI2007]大都市meg Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 2221 Solved: 1179[Submit][Sta ...
- BZOJ 4196: [Noi2015]软件包管理器 [树链剖分 DFS序]
4196: [Noi2015]软件包管理器 Time Limit: 10 Sec Memory Limit: 512 MBSubmit: 1352 Solved: 780[Submit][Stat ...
随机推荐
- hihocoder #1333 : 平衡树·Splay2
描述 小Ho:好麻烦啊~~~~~ 小Hi:小Ho你在干嘛呢? 小Ho:我在干活啊!前几天老师让我帮忙管理一下团队的人员,但是感觉好难啊. 小Hi:说来听听? 小Ho:事情是这样的.我们有一个运动同好会 ...
- python web编程 创建一个web服务器
这里就介绍几个底层的用于创建web服务器的模块,其中最为主要的就是BaseHTTPServer,很多框架和web服务器就是在他们的基础上创建的 基础知识 要建立一个Web 服务,一个基本的服务器和一个 ...
- Arduino101学习笔记(七)—— 时间API
1.毫秒时间 获取机器运行的时间长度, 单位毫秒. 系统最长的记录时间为9小时22分, 如果超出时间将从0开始. 警告: 时间为 unsigned long类型, 如果用 int 保存时间将得到错误结 ...
- sql篇,动态合并数据
背景: 为何说是一雪前耻呢,想当年,我还小,我出去面试远洋,远远地看着浩哥在那里坐着,然后下班去吃饭,我和东辉却在那里静静地等待着第二轮的技术面试(结果是没有面上,一个是学历问题),终于一个小个子姐姐 ...
- 生成n位随机字符串
--1.借助newid() Go --创建视图(因为在函数中无法直接使用newid()) create view vnewid as select newid() N'MacoId'; go --创建 ...
- Struts2请求参数校验
校验的分类 客户端数据校验 和 服务器端数据校验 客户端数据校验 ,通过JavaScript 完成校验 (改善用户体验,使用户减少出错 ) 服务器数据校验 ,通过Java代码 完成校验 struts2 ...
- 使用Eclipse将Web项目打Jar包方法
1.对下载.安装和运行Eclipse,就不再说了: 2.找到待打包项目: 3.右键,Export-->Export: 4.选择,Jar: 5.按如图操作: 6.完成后:
- JavaScript设计模式——前奏(封装和信息隐藏)
前面一篇讲了js设计模式的前奏,包括接口的讲解.. 三:封装和信息隐藏: 信息隐藏用来进行解耦,定义一些私有的数据和方法. 封装是用来实现信息隐藏的技术,通过闭包实现私有数据的定义和使用. 接口在这其 ...
- 判断密文加密类型hash-identifier
判断密文加密类型hash-identifier 在安全领域中,加密数据随处可见.而在这些数据中,重要的数据往往采用哈希算法进行加密.例如,Linux密码使用sha512,Windows密码采用LM ...
- 解决Myeclipse10 Building Workspace速度慢的问题
解决方法如下: 选择项目,选择Project -> Properties -> Builders,取消JavaScript Validator,validation或者其它你认为没必要的选 ...