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 ...
随机推荐
- N种内核注入DLL的思路及实现
内核注入,技术古老但很实用.现在部分RK趋向无进程,玩的是SYS+DLL,有的无文件,全部存在于内存中.可能有部分人会说:"都进内核了.什么不能干?".是啊,要是内核中可以做包括R ...
- 孙鑫VC学习笔记:多线程编程
孙鑫VC学习笔记:多线程编程 SkySeraph Dec 11st 2010 HQU Email:zgzhaobo@gmail.com QQ:452728574 Latest Modified ...
- Jmeter 小攻略(转)
http://www.myexception.cn/open-source/1346307.html
- BZOJ 3542 [Poi2014]Couriers ——可持久化线段树
[题目分析] 查找区间内出现次数大于一半的数字. 直接用主席树,线段树上维护区间大小,由于要求出现次数大于一半,每到一个节点可以分治下去. 时间复杂度(N+Q)logN [代码] #include & ...
- wpf中dropdownButton控件下拉居中。。。
设置模版中popup控件的HorizontalOffset属性来控制居中. 还是对popup控件不熟,折腾了一会.
- VS链接过程中与MSVCRT.lib冲突
vs代码生成有/MT,/MTd,/Md,/MDd四个编译选项,分别代表多线程.多线程调试.多线程DLL.多线程调试DLL. 编译时引用的lib分别为libcmt.li.libcmtd.lib.msvc ...
- happypack 原理解析
说起 happypack 可能很多同学还比较陌生,其实 happypack 是 webpack 的一个插件,目的是通过多进程模型,来加速代码构建,目前我们的线上服务器已经上线这个插件功能,并做了一定适 ...
- Codeforces Round #335 (Div. 2)
水 A - Magic Spheres 这题也卡了很久很久,关键是“至少”,所以只要判断多出来的是否比需要的多就行了. #include <bits/stdc++.h> using nam ...
- Robotium编写测试用例如何模拟Junit4的BeforeClass和AfterClass方法2 - SingleLaunchActivityTestCase
本文来源于:http://blog.csdn.net/zhubaitian/article/details/39296753 在上一遍笔记博客中本以为只能在Setup和TearDown中做条件判断来实 ...
- React的第一步
首先了解React中所牵扯到的几个重要的概念 什么是React? 是Facebook的开发团队开发出来的一个用于构建用户界面个js库,最近才开源出来公布于世,它的初衷是用于创建“独立的视图组件”,一个 ...