题目链接: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 DeferedResult和servlet3.1 AsyncContext异步请求

    先看一个简单的示例: @RequestMapping("/getFuture") public Future<String> getFuture() { System. ...

  2. uva 442

    #include<iostream>#include<stack>#include<map>using namespace std;struct node{ int ...

  3. my08_mysqldump+binlog server备份

    备份策略描述 ******************************************* mysqldump备份适用于小数据量的备份,比如100G以下的数据量,就可以使用逻辑备份 举例两个 ...

  4. Factorization Machines with libFM 论文阅读

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

  5. maya2017无法安装卸载激活失败

    AUTODESK系列软件着实令人头疼,安装失败之后不能完全卸载!!!(比如maya,cad,3dsmax等).有时手动删除注册表重装之后还是会出现各种问题,每个版本的C++Runtime和.NET f ...

  6. [转]怎么样快速入门AngularJS?

    本文转自:http://www.ngnice.com/posts/205af1ea1e13d2 怎么样快速学习AngularJS? 相信很多初学者都有过或者类似的疑问,其实这个问题没有标准的答案,每个 ...

  7. [转]使用 YCombo 做 JS /CSS开发 合并 压缩

    本文转自:http://www.neoease.com/minimize-javascript-files-using-ycombo/ 前文已介绍过 YCombo 及相关的 CSS 和 JS 合并工具 ...

  8. RSA算法、SSL协议学习笔记

    最近学习计算机网络,涉及到SSL协议,我想起了去年密码学课程讲过的非对称加密RSA算法,结合阮老师的博客,写写学习笔记,这里再回忆一下. RSA算法 RSA算法是一种非对称密码算法,所谓非对称,就是指 ...

  9. 【Linux】debian 7 安装 rz sz lrzsz

    通常linux服务器是通过ssh客户端来进行远程登录和管理的.然而如何方便的实现客户端与linux服务器端的文件交互呢?这就需要用到rz(上传).sz(下载)工具.在Ubuntu 10.10下安装rz ...

  10. @Html.Partial,@Html.Action,@Html.RenderPartial,@Html.RenderAction [转]

    @Html.Action:需要有对应的Action,并且Action方法有返回值.(注:处理完业务逻辑同时,也需要返回所需值) @{Html.RenderAction}:需要有对应的Action,Ac ...