题目大意: 输入一个p*q的棋盘, 行用数字表示, 列用大写字母表示 , 1 <= p*q <= 26, 输出能够把棋盘全部空格走完且每个空格只走一次的字典序最小的路径。不存在则输出“impossible”

坑の所在: 行列的表示, 每组解后有一空行。

思路: 八方向dfs,方向数组要按字典序排列, 记录步数, 最先找到的一组解便是字典序最小解,便可停止搜索。

AC代码:

#include<iostream>
#include<cstdio>
using namespace std;
int p, q;
bool vis[30][30];
int d[8][2] = { -1,-2, 1,-2, -2,-1, 2,-1, -2,1, 2,1, -1,2, 1, 2 };
struct node{
int x, y;
}next[30][30];
bool check(int a, int b)
{
if(a >= 0 && a < p && b >= 0 && b < q) return true;
return false;
}
bool stop;
void init()
{
for(int i = 0; i < 30; i++)
for(int k = 0; k < 30; k++){
vis[i][k] = false;
next[i][k].x = next[i][k].y = -1;
}
vis[0][0] = true;
stop = 0;
}
void dfs(int a, int b, int step)
{
if(step == p*q) {
stop = true;
return;
}
for(int i = 0; i < 8; i++){
int dx = a + d[i][0];
int dy = b + d[i][1];
if(check(dx, dy) && !vis[dx][dy]){
next[a][b].x = dx;
next[a][b].y = dy;
vis[dx][dy] = true;
//cout<<a<<" "<<b<<" "<<next[a][b].x<<" "<<next[a][b].y<<endl;
dfs(dx, dy, step+1);
if(stop) return;
vis[dx][dy] = false;
}
}
}
void output()
{
int a = 0, b = 0, t, step = 1;
printf("%c%d", a+'A', b+1);
while(step < p*q){
printf("%c%d", next[a][b].y+'A', next[a][b].x+1);
t = a;
a = next[a][b].x;
b = next[t][b].y;
step++;
}
printf("\n");
} int main()
{
int T;
cin>>T;
for(int k = 1; k <= T; k++){
scanf("%d%d", &p, &q);
init();
dfs(0, 0, 1);
printf("Scenario #%d:\n", k);
if(stop) output();
else printf("impossible\n");
if(k != T)printf("\n");
}
return 0;
}
作者:u011652573 发表于2014-6-1 15:36:08 原文链接
阅读:33 评论:0 查看评论

[原]poj-2488-water-DFS的更多相关文章

  1. POJ 2488 -- A Knight's Journey(骑士游历)

    POJ 2488 -- A Knight's Journey(骑士游历) 题意: 给出一个国际棋盘的大小,判断马能否不重复的走过所有格,并记录下其中按字典序排列的第一种路径. 经典的“骑士游历”问题 ...

  2. POJ.3172 Scales (DFS)

    POJ.3172 Scales (DFS) 题意分析 一开始没看数据范围,上来直接01背包写的.RE后看数据范围吓死了.然后写了个2^1000的DFS,妥妥的T. 后来想到了预处理前缀和的方法.细节以 ...

  3. poj 2488 A Knight's Journey( dfs )

    题目:http://poj.org/problem?id=2488 题意: 给出一个国际棋盘的大小,判断马能否不重复的走过所有格,并记录下其中按字典序排列的第一种路径. #include <io ...

  4. poj 2488 A Knight's Journey 【骑士周游 dfs + 记忆路径】

    题目地址:http://poj.org/problem?id=2488 Sample Input 3 1 1 2 3 4 3 Sample Output Scenario #1: A1 Scenari ...

  5. [poj 2331] Water pipe ID A*迭代加深搜索(dfs)

    Water pipe Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 2265 Accepted: 602 Description ...

  6. POJ 2488 A Knight's Journey【DFS】

    补个很久之前的题解.... 题目链接: http://poj.org/problem?id=2488 题意: 马走"日"字,让你为他设计一条道路,走遍所有格,并输出字典序最小的一条 ...

  7. poj 2488 A Knight&#39;s Journey(dfs+字典序路径输出)

    转载请注明出处:http://blog.csdn.net/u012860063?viewmode=contents 题目链接:http://poj.org/problem? id=2488 ----- ...

  8. POJ 2488 A Knight's Journey (回溯法 | DFS)

    题目链接:http://poj.org/problem?id=2488 题意: 在国际象棋的题盘上有一个骑士,骑士只能走“日”,即站在某一个位置,它可以往周围八个满足条件的格子上跳跃,现在给你一个p ...

  9. POJ 2488 A Knight's Journey(DFS)

    A Knight's Journey Time Limit: 1000MSMemory Limit: 65536K Total Submissions: 34633Accepted: 11815 De ...

  10. POJ 2488 DFS

    DES:给一个n行m列的棋盘.马以L型走.问能否从某一位置开始走完棋盘上的每个位置.若能继续输出字典序最小的一条路径. 很典型的dfs.搜的时候就按照字典序从小到大的顺序.搜到第一条路径时停止搜索输出 ...

随机推荐

  1. bzoj 1800 暴力枚举

    直接暴力枚举四个点,然后判断是否能组成矩形就行了 注意枚举的点的标号从小到大,保证不重复枚举 /**************************************************** ...

  2. Load hlsl

    这个函数和sample差不多 不过没有samplestate和filter http://msdn.microsoft.com/zh-cn/library/windows/desktop/bb5096 ...

  3. Js高程笔记->引用类型

    1 . Object 对象    2 . Array 对象 :       检测方法:ES5 : isArray       转换方法: toLocaleString , toString , val ...

  4. Curse of Dimensionality

    Curse of Dimensionality Curse of Dimensionality refers to non-intuitive properties of data observed ...

  5. 一个利用window.name实现的windowStorage

    //key:value|key:value var windowStorage = { _inited: false, _data: {}, init: function(str) { var tmp ...

  6. android 关于Location of the Android SDK has not been setup in the preferences的解决方法

    今天在部署android开发环境的时候,每次打开eclipse的时候点击AVD Manager的按钮就会弹出Location of the Android SDK has not been setup ...

  7. linux yum 命令 详解

    linux yum命令详解 yum(全称为 Yellow dog Updater, Modified)是一个在Fedora和RedHat以及SUSE中的Shell前端软件包管理器.基於RPM包管理,能 ...

  8. DirectShow 最简单的入门 -- 播放一段视频

    #include <dshow.h> #pragma comment(lib,"strmbase.lib") #pragma comment(lib,"qua ...

  9. Javascript里的那些距离们

    1.有滚动条的控件的距离: scrollTop和scrollLeft:分别指有滚动条的容器控件的滚动条的top和left:页面滚动条的通用取法:document.body.scrollTop(FF\C ...

  10. 国内Jquery CDN

    新浪CDN: <script src="http://lib.sinaapp.com/js/jquery/1.9.1/jquery-1.9.1.min.js">< ...