Poj(2488),按照字典序深搜
题目链接: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),按照字典序深搜的更多相关文章
- POJ 1128 拓扑排序 + 深搜
/* (⊙v⊙)嗯 貌似是一个建图 拓扑+深搜的过程.至于为什么要深搜嘛..一个月前敲得题现在全部推了重敲,于是明白了.因为题意要求如果有多个可能的解的话. * 就要输出字典序最小的那个.所以可以对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, ...
- POJ 2676 Sudoku(深搜)
Sudoku Time Limit : 4000/2000ms (Java/Other) Memory Limit : 131072/65536K (Java/Other) Total Submi ...
- POJ 2676 数独+dfs深搜
数独 #include "cstdio" #include "cstring" #include "cstdlib" #include &q ...
- POJ 2488:A Knight's Journey 深搜入门之走马观花
A Knight's Journey Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 35342 Accepted: 12 ...
- 深搜+回溯 POJ 2676 Sudoku
POJ 2676 Sudoku Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 17627 Accepted: 8538 ...
- poj 3249 Test for Job (记忆化深搜)
http://poj.org/problem?id=3249 Test for Job Time Limit: 5000MS Memory Limit: 65536K Total Submissi ...
- (深搜)Sum It Up -- poj --1564
链接: http://poj.org/problem?id=1564 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=88230#probl ...
- 广搜,深搜,单源最短路径,POJ(1130),ZOJ(1085)
题目链接: http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=85 http://poj.org/problem?id=1130 这 ...
随机推荐
- struts1学习
转载:https://blog.csdn.net/toyouheart/article/details/4509466
- windows 远程到ubuntu桌面
Windows remote connect ubuntu desktop 1. install xRDP sudo apt-get update sudo apt-get install xrdp ...
- 转 mysql front安装与使用教程 mysql 工具
mysql front安装与使用教程 由 kaikai0220 创建,Alma 最后一次修改 2018-04-25 mysql front一款小巧的管理Mysql的应用工具,那么这个工具该如何安装和使 ...
- Nvelocity中格式化金钱和日期
//格式化金钱( 9,999.00)function formatCurrency(num) { num = num.toString().replace(/\$|\,/g, ''); ...
- python 运算符-赋值运算
结果是值 算数运算: a = 10 * 10 赋值运算: a = a + 1 a +=1 结果是布尔值 比较运算: a = 1 > 5 逻辑运算: a = 1 > 6 or 1= ...
- 既然有了HBase,为什么还需要Kudu呢?
不多说,直接上干货! 那既然有了HBase,为什么还需要Kudu呢? 简单的说,就是嫌弃HBase在OLAP(联机分析处理)场合,SQL/MR类的批量检索场景中,性能不够好.通常这种海量数据OLAP场 ...
- PHP 魔术方法__set() __get() 方法详解
__set() is run when writing data to inaccessible properties. __get() is utilized for reading data fr ...
- 【转】linux之pmap命令!
原贴:http://tonykorn97.itpub.net/post/6414/249221 linux之pmap命令! ====================================== ...
- [转]JAVA Iterator 的用法
java.util包中包含了一系列重要的集合类.本文将从分析源码入手,深入研究一个集合类的内部结构,以及遍历集合的迭代模式的源码实现内幕. 下面我们先简单讨论一个根接口Collection,然后分析一 ...
- C#设计模式——单例
单例模式是设计模式中最简单的形式之一.这一模式的目的是使得类的一个对象成为系统中的唯一实例.对于系统中的某些类来说,只有一个实例很重要,例如,一个系统中可以存在多个打印任务,但是只能有一个正在工作的任 ...