题目链接: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. eclipse安装阿里规范模板

    https://github.com/alibaba/p3c/tree/master/p3c-formatter 1.代码模板(含注释等) 2.代码格式化

  2. thinkPHP 模板操作

    1.assign赋值 $this->assign('title','模板操作'); $this->assign('bests',$bests);//$bests是二维数组 2.变量的输出 ...

  3. Unable to verify your data submission.加入了_csrf也报400错误的解决

    <input type="hidden" name="_csrf" value="<?=Yii::$app->request-> ...

  4. php和c++自带的排序算法

    PHP的 sort() 排序算法与 C++的 sort() 排序算法均为不稳定的排序算法,也就是说,两个值相同的数经过排序后,两者比较过程中还进行了交换位置,后期开发应主要这个问题

  5. virtualenv(for python)

    完整: http://docs.jinkan.org/docs/flask/installation.html#installation   virtualenv 你很可能想在开发中用上 virtua ...

  6. B P5 第十三届北航程序设计竞赛预赛

    https://buaacoding.cn/contest-ng/index.html#/188/problems 其实这题挺简单的. 注意到答案的大小最多是22 二分,check长度是mid的不同子 ...

  7. c++ 和 matlab 下的caffe模型输入差异

    在向一个caffe模型传递输入数据的时候,要注意以下两点: 1. opencv中Mat数据在内存中的存放方式是按行存储,matlab中图像在内存中的存放方式是按列存储. 2. opencv中Mat数据 ...

  8. 数据结构之C语言模拟整数数组实现

    #include <stdio.h> #include <malloc.h> #include <stdlib.h> typedef struct Arr { in ...

  9. vue学习中遇到的onchange、push、splice、forEach方法使用

    最近在做vue的练习,发现有些js中的基础知识掌握的不牢,记录一下: 1.onchange事件:是在域的内容改变时发生,单选框与复选框改变后触发的事件. 2.push方法:向数组的末尾添加一个或多个元 ...

  10. asp.net core 2.1 生成swagger文档

    新建asp.netcore2.1 api项目 “WebApplication1” 在nuget管理器中添加对Swashbuckle.AspNetCore 3.0.0.Microsoft.AspNetC ...