POJ2488 dfs
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 41972 | Accepted: 14286 |
Description
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 ...
随机推荐
- jQuery基础知识点(DOM操作)
1.样式属性操作 1)设置样式属性操作 ①设置单个样式: // 第一个参数表示:样式属性名称 // 第二个参数表示:样式属性值 $(selector).css(“color”, ...
- Linux学习笔记(14)文件系统管理
1 回顾分区与系统 (1)分区类型 Linux的分区一般包括主分区,扩展分区和逻辑分区.其中: 主分区最多只能分4个: 扩展分区只能有一个,也算作主分区的一种,即主分区加扩展分区最多有4个.但扩展分区 ...
- 我的 Unity2D 屏幕适配
以下方法纯属我YY,切勿当真!!! 确定一个设计尺寸,比如 devWidth = 960,devHeight = 640, 按照这个尺寸进行设计游戏. 方式一: 不管什么屏幕尺寸,都和设计的尺寸对应. ...
- C# 生成随机数
private static char[] constant = { ', 'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p ...
- java.net.SocketException: No buffer space available
https 访问url在调用量不大的情况下 java.net.SocketException: No buffer space available (maximum connections reach ...
- DZY Loves Sequences
time limit per test 1 second memory limit per test 256 megabytes input standard input output standar ...
- Nico Nico Ni~(完全背包)
Time Limit:2000MS Memory Limit:65535K Type: Program Language: Not Limited Description Lys plays L ...
- DFS(连通块) ZOJ 2743 Bubble Shooter
题目传送门 题意:从炮台射出一个球,三个及以上颜色相同的会掉落,问最后会掉落多少个球 分析:先从炮台找一个连通块,然后与顶部连接的连通块都不会掉落,剩下的就是炮台射出后跟随掉落的. #include ...
- Robotium编写测试用例如何模拟Junit4的BeforeClass和AfterClass方法1 - 条件判断法
本文来源于:http://blog.csdn.net/zhubaitian/article/details/39293883 Robotium的测试类ActivityInstrumentationTe ...
- OpenCV 线性混合(4)
带滚动条的线性混合示例: #include "stdafx.h" #include<iostream> #include<thread> #incl ...