题目大意: 输入一个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. [geeksforgeeks] Lowest Common Ancestor in a Binary Search Tree.

    http://www.geeksforgeeks.org/lowest-common-ancestor-in-a-binary-search-tree/ Lowest Common Ancestor ...

  2. BZOJ1083: [SCOI2005]繁忙的都市

    水题之王SP…这题就裸的最小生成树 /************************************************************** Problem: 1083 User ...

  3. 【动态规划】 之最长公共子序列LCS

    int lcs_len(char *a, char *b, int c[][N]){ int aLen=strlen(a), bLen=strlen(b), i,j; ; i<=aLen; i+ ...

  4. ZOJ3762 The Bonus Salary!(最小费用最大流)

    题意:给你N个的任务一定要在每天的[Li,Ri]时段完成,然后你只有K天的时间,每个任务有个val,然后求K天里能够获得的最大bonus. 思路:拿到手第一直觉是最小费用最大流,然后不会建图,就跑去想 ...

  5. ZOJ3550 Big Keng(三分)

    题意:给定一个立体的图形,上面是圆柱,下面是圆台,圆柱的底面半径和圆台的上半径相等,然后体积的V时,问这个图形的表面积最小可以是多少.(不算上表面).一开始拿到题以为可以YY出一个结果,就认为它是圆锥 ...

  6. Mime Types

    Mime Types 1.http://www.freeformatter.com/mime-types-list.html 2.http://www.webmaster-toolkit.com/mi ...

  7. C# 与C/C++相互调用

    C++调用C#的DLLhttp://www.csharpwin.com/csharpspace/11385r8940.shtml C#调用C/C++动态库必须注意的几个问题http://www.rob ...

  8. HDU 1788 Chinese remainder theorem again

    题目链接 题意 : 中文题不详述. 思路 : 由N%Mi=(Mi-a)可得(N+a)%Mi=0;要取最小的N即找Mi的最小公倍数即可. #include <cstdio> #include ...

  9. android模拟器(genymotion)+appium+python 框架执行过程中问题解答

    1.case运行过程中中文输入不进去? 答:注意事项 1)需要修改系统编码为utf-8,才能解决中文输入问题,case执行入口文件添加代码如下: import sys reload(sys) sys. ...

  10. AC自动机总结

    AC自动机的模板 void buildAC() { while(!q.empty()) q.pop(); q.push(); while(!q.empty()) { int x=q.front();q ...