题目链接:http://poj.org/problem?id=2488

思路:按照一定的字典序深搜,当时我的想法是把所有的可行的路径都找出来,然后字典序排序。

后来,凡哥说可以在搜索路径的时候就按照字典序搜索,这样一找到可行的路径就输出来就行了。这里我吸取了之前八皇后问题时犯的错,并且优化了一下写法,就是flag,这是参考了jhf大神的写法了。

但是jhf大神的写法,思路和我一样,但是他的x,y坐标还要转来转去,我就没有这么写了,还是按照我的代码风格好一些。

#include <stdio.h>
#include <string.h> bool vis[][];
int to[][] = {{-,-},{,-},{-,-},{,-},{-,},{,},{-,},{,}};
int R,C;
bool flag; struct Path
{
int r;
int c;
} path[*]; bool judge(int r,int c)
{
if(r<||r>=R||c<||c>=C||vis[r][c])
return false;
return true;
} void dfs(int r,int c,int k)
{
path[k].c = c;
path[k].r = r; if(k==C*R)
{
flag = true;
return ;
}
for(int i=; i<; i++)
{
int rx = r+to[i][];
int cx = c+to[i][];
if(judge(rx,cx))
{
vis[rx][cx] = true;
dfs(rx,cx,k+);
if(flag)
return;
vis[rx][cx] = false;
}
}
} int main()
{
int t;
scanf("%d",&t);
for(int cases=; cases<=t; cases++)
{
memset(vis,false,sizeof(vis)); printf("Scenario #%d:\n",cases);
scanf("%d%d",&R,&C);
for(int i=; i<C; i++)
{
for(int j=; j<R; j++)
{
flag = false;
vis[j][i] = true;
dfs(j,i,);
if(flag)
break;
vis[j][i] = false;
}
if(flag)
break;
}
if(flag)
{
for(int i=;i<=R*C;i++)
printf("%c%d",path[i].c+'A',path[i].r+);
puts("\n");
}
else printf("impossible\n\n");
}
return ;
}

Poj(2488),按照字典序深搜的更多相关文章

  1. POJ 1128 拓扑排序 + 深搜

    /* (⊙v⊙)嗯 貌似是一个建图 拓扑+深搜的过程.至于为什么要深搜嘛..一个月前敲得题现在全部推了重敲,于是明白了.因为题意要求如果有多个可能的解的话. * 就要输出字典序最小的那个.所以可以对2 ...

  2. poj 3984 -- 迷宫问题 深搜

    迷宫问题 Description 定义一个二维数组: int maze[5][5] = { 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, ...

  3. POJ 2676 Sudoku(深搜)

    Sudoku Time Limit : 4000/2000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other) Total Submi ...

  4. POJ 2676 数独+dfs深搜

    数独 #include "cstdio" #include "cstring" #include "cstdlib" #include &q ...

  5. POJ 2488:A Knight's Journey 深搜入门之走马观花

    A Knight's Journey Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 35342   Accepted: 12 ...

  6. 深搜+回溯 POJ 2676 Sudoku

    POJ 2676 Sudoku Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 17627   Accepted: 8538 ...

  7. poj 3249 Test for Job (记忆化深搜)

    http://poj.org/problem?id=3249 Test for Job Time Limit: 5000MS   Memory Limit: 65536K Total Submissi ...

  8. (深搜)Sum It Up -- poj --1564

    链接: http://poj.org/problem?id=1564 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=88230#probl ...

  9. 广搜,深搜,单源最短路径,POJ(1130),ZOJ(1085)

    题目链接: http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=85 http://poj.org/problem?id=1130 这 ...

随机推荐

  1. 练习五十三:for循环练习

    对100以内的两位数,请使用一个两重循环打印出所有十位数都比各位数字小的数,并统计个数 l = [] for i in range(1,9): for j in range(i): l.append( ...

  2. Flex布局教程

    一.Flex布局是什么? Flex 是 Flexible Box 的缩写,意为"弹性布局",用来为盒状模型提供最大的灵活性.任何一个容器都可以指定为 Flex 布局. .box{ ...

  3. 转 rac中并行 PARALLEL 的设置

    sample 1: rac中并 行的设置 https://blog.csdn.net/wll_1017/article/details/8285574 我们的生产库一般在节点一上的压力比较大,在节点二 ...

  4. 转 PYTHON2 编码处理-str与Unicode的区别

    https://www.cnblogs.com/long2015/p/4090824.html

  5. redis初步学习 0

    2.1 Redis是什么 REmote DIctionary Server(Redis) 是一个由Salvatore Sanfilippo写的key-value存储系统.Redis提供了一些丰富的数据 ...

  6. pip install keras==1.2.1

    [该方法仅适用于压缩包中含有setup.py的情况] 先从GitHub上找到想要下载的历史版本,右键复制链接地址. 然后执行命令: pip install https://github.com/ker ...

  7. Factorization Machines with libFM 论文阅读

    Factorization Machines with libFM https://pan.baidu.com/s/1aAyhHGNSrZQFDfoz8VsHIQ libFM网站:http://www ...

  8. vue router-link 添加在定义事件

    在vue学习中遇到给router-link 标签添加事件@click .@mouseover等无效的情况 我想要做的是v-for遍历出来的选项卡, 鼠标移上去出现删除标签,移除标签消失的效果 原代码: ...

  9. jemeter接口测试基础

    前言: 本文主要针对http接口进行测试,使用Jmeter工具实现. Jmter工具设计之初是用于做性能测试的,它在实现对各种接口的调用方面已经做的比较成熟,因此,本次直接使用Jmeter工具来完成对 ...

  10. rsync 命令中的路径斜线

    rsync命令大家都知道,但是其中的一个小细节比较容易被忽略,那就是 路径结尾的 "/" ,在路径的结尾有没有斜线,结果是大不同的.现举例说明: 假设现有两个目录,一个名为sour ...