题目链接: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. springmvc怎么重定向

    转载大神: https://blog.csdn.net/wabiaozia/article/details/50252661

  2. Filter责任链模式

    Filter责任链的创建 org.apache.catalina.core.ApplicationFilterFactory#createFilterChain,  此方法是被org.apache.c ...

  3. vue.js入门环境搭建

    1.node.js环境(npm包管理器) 2.vue-cli手脚架构建工具 3.cnpm npm的淘宝镜像 安装node.js 从node.js官网下载并安装node,安装过程一路“下一步”就可以 安 ...

  4. 基于webpack的vue项目路径别名

    在vue的项目里,我们可以使用../这样的相对路径的方式引用不同目录的组件: import userinfo from '../../../components/userinfo.vue'; 使用.. ...

  5. Zookeeper---初识

    1.Zookeeper是  Apache开源  的 分布式应用程序   服务治理: 在分布式环境中 协调和管理服务 是一个复杂的过程: ZooKeeper通过其简单的架构和API解决了这个问题: Zo ...

  6. Readthedocs+Github搭建文档

    一.文档撰写前提 环境部署: > git clone https://github.com/toooney/demo-readthedocs.git > pip install sphin ...

  7. python 矢量化的字符串

  8. SVN仓库连同版本信息迁移新服务器的步骤

    SVN仓库连同版本信息迁移新服务器的步骤 步骤一:导出(1)链接原服务器,找到SVN Server安装路径下的bin文件,并复制文件路径,如 C:\Program File\SVN Server\bi ...

  9. EF的注解Annotation和Fluent API

    注意:Annotation特性标记可组合使用,也就是在一个类或属性上可以附加多个annotations特性 一.常用注解和对应的Fluent API 1.[Required]              ...

  10. 浅谈SQL Server中的三种物理连接操作(Nested Loop Join、Merge Join、Hash Join)

    简介 在SQL Server中,我们所常见的表与表之间的Inner Join,Outer Join都会被执行引擎根据所选的列,数据上是否有索引,所选数据的选择性转化为Loop Join,Merge J ...