POJ——3984
走迷宫问题,POJ上面的题
#include <stdio.h>
#include <stdlib.h> #define SIZE 5 bool findpath = false;
int DX[]={-,,,};//每一步对应的纵坐标变化量,竖着的棋盘是X轴
int DY[]={,,-,};//每一步对应的横坐标变化量,横着的棋盘是Y轴
int maze[SIZE][SIZE]={{,,,,},{,,,,},{,,,,},{,,,,},{,,,,}};//迷宫情况
int count=;//
int countMax =;//MAX值
int step=;//走到第几步
int best_x[]={};//存储最优路径的x坐标
int best_y[]={};//存储最优路径的y坐标
int lujing_x[]={};//存储每一步的x坐标
int lujing_y[]={};//存储每一步的y坐标 //计算当前可以达到目标点的路径长度
int lujing_length()
{
int num=;
for(int i=;i<SIZE;i++)
for(int j=;j<SIZE;j++)
{
if(maze[i][j]==)
num ++;
}
return num;
} void DFS(int x,int y){
if(x==SIZE-&&y==SIZE-){
findpath = true;
count=lujing_length();
if(count<countMax){
countMax = count;
for(int i=;i<countMax;i++){
best_x[i] = lujing_x[i];
best_y[i] = lujing_y[i];
}
}
return;
} for(int i=;i<;i++){
int NX = x +DX[i];
int NY = y +DY[i];
if(NX<SIZE&&NY<SIZE&&NX>=&&NY>=&&maze[NX][NY]==&&(NX+NY!=))
{
int tmp = maze[NX][NY];
maze[NX][NY] = ;
lujing_x[step] = NX;//把每一步的路径存下来
lujing_y[step] = NY;
step++;
DFS(NX,NY);
step--;
maze[NX][NY] = tmp;
} }
} int main(){
//for(int i=0;i<SIZE;i++)
// for(int j=0;j<SIZE;j++){
// printf("Please input the maze elements:\n");
// scanf("%d",maze[i][j]);
// }
DFS(,);
//printf("findpath:%d\n",findpath);//是否找到路径
printf("(0, 0)\n");
for(int i=;i<countMax;i++){
printf("(%d, %d)\n",best_x[i],best_y[i]);//打印出路径
}
//system("pause");
}
POJ——3984的更多相关文章
- POJ 3984 迷宫问题
K - 迷宫问题 Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Submit Sta ...
- POJ 3984(DFS入门题 +stack储存路径)
POJ 3984 Description 定义一个二维数组: int maze[5][5] = { 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, ...
- BFS(最短路+路径打印) POJ 3984 迷宫问题
题目传送门 /* BFS:额,这题的数据范围太小了.但是重点是最短路的求法和输出路径的写法. dir数组记录是当前点的上一个点是从哪个方向过来的,搜索+,那么回溯- */ /************* ...
- Q - 迷宫问题 POJ - 3984(BFS / DFS + 记录路径)
Q - 迷宫问题 POJ - 3984 定义一个二维数组: int maze[5][5] = { 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, ...
- poj 3984
http://poj.org/problem?id=3984 题目很简单,就是简单的BFS吧,主要的难点在于坐标的问题 这个呢,可以反其道而行之,就是你从(1,1)到(5,5),你肯定走过一次 走过一 ...
- poj 3984 迷宫问题(dfs)
题目链接:http://poj.org/problem?id=3984 思路:经典型的DFS题目.搜索时注意剪枝:越界处理,不能访问处理. 代码: #include <iostream> ...
- POJ - 3984迷宫问题(最短路径输出)
题目链接:http://poj.org/problem?id=3984 题目: 迷宫问题 Time Limit: 1000MS Memory Limit: 65536K Total Submiss ...
- POJ 3984 - 迷宫问题 - [BFS水题]
题目链接:http://poj.org/problem?id=3984 Description 定义一个二维数组: int maze[5][5] = { 0, 1, 0, 0, 0, 0, 1, 0, ...
- [POJ 3984] 迷宫问题(BFS最短路径的记录和打印问题)
题目链接:http://poj.org/problem?id=3984 宽度优先搜索最短路径的记录和打印问题 #include<iostream> #include<queue> ...
- POJ 3984 迷宫问题 bfs 难度:0
http://poj.org/problem?id=3984 典型的迷宫问题,记录最快到达某个点的是哪个点即可 #include <cstdio> #include <cstring ...
随机推荐
- JQuery上传插件uploadify整理(Options)
下载 现在有两个版本了,我此次使用的依然是Flash版本的,虽然现在绝大部分浏览器都兼容HTMKL5,目前位置,除了做手机项目外,一般我们项目中不允许使用HTML5标签. 属性介绍(Options) ...
- ASP.Net软件工程师基础(三)
1.多态 答: (1)虚方法 public class Child : Person { public void Speach() { base.Speach(); } public virtual ...
- php文件删除unlink()详解
请记住从PHP文件创建的教训,我们创建了一个文件,名为testFile.txt . $myFile = "testFile.txt"; $fh = fopen($myFile, ' ...
- android界面布局技巧(一)
(1)//得到手机的宽高 Display display = getWindowManager().getDefaultDisplay(); int screenWidth = display.get ...
- http是什么?
http HyperText Transfer Protocol 超文本传输协议,是一个应用层通信协议. 可以用wireshark抓取.
- 【转载】Myeclipse如何自动创建hibernate
Myeclipse如何自动创建hibernate:http://jingyan.baidu.com/article/456c463b99f4370a583144a8.html An internal ...
- 关于 LimitedConcurrencyLevelTaskScheduler 的疑惑
1. LimitedConcurrencyLevelTaskScheduler 介绍 这个TaskScheduler用过的应该都知道,微软开源的一个任务调度器,它的代码很简单, 也很好懂,但是我没有明 ...
- JDK源码分析之集合02ArrayList
一.前言 有了前一篇对集合类的概述,我们知道ArrayList是属于Collection类系中的一个具体实现类,其特点是长度可以动态改变,集合内部使用数组保存元素.下面我们对源码进行分析. 二.Arr ...
- FPGA中的时序分析(四)
常用约束语句说明 关于Fmax 上述是实现Fmax的计算公式,clock skew delay的计算如下图, 就是两个时钟的差值.到头来,影响Fmax的值的大小就是组合逻辑,而Fmax是针对 ...
- django-ajax之post方式
post方式不同于get方式可以被django直接得到,因为django为post加入了csrf保护, 详细的文档地址https://docs.djangoproject.com/en/dev/re ...