走迷宫问题,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的更多相关文章

  1. POJ 3984 迷宫问题

    K - 迷宫问题 Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Sta ...

  2. 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, ...

  3. BFS(最短路+路径打印) POJ 3984 迷宫问题

    题目传送门 /* BFS:额,这题的数据范围太小了.但是重点是最短路的求法和输出路径的写法. dir数组记录是当前点的上一个点是从哪个方向过来的,搜索+,那么回溯- */ /************* ...

  4. 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, ...

  5. poj 3984

    http://poj.org/problem?id=3984 题目很简单,就是简单的BFS吧,主要的难点在于坐标的问题 这个呢,可以反其道而行之,就是你从(1,1)到(5,5),你肯定走过一次 走过一 ...

  6. poj 3984 迷宫问题(dfs)

    题目链接:http://poj.org/problem?id=3984 思路:经典型的DFS题目.搜索时注意剪枝:越界处理,不能访问处理. 代码: #include <iostream> ...

  7. POJ - 3984迷宫问题(最短路径输出)

    题目链接:http://poj.org/problem?id=3984 题目: 迷宫问题 Time Limit: 1000MS   Memory Limit: 65536K Total Submiss ...

  8. POJ 3984 - 迷宫问题 - [BFS水题]

    题目链接:http://poj.org/problem?id=3984 Description 定义一个二维数组: int maze[5][5] = { 0, 1, 0, 0, 0, 0, 1, 0, ...

  9. [POJ 3984] 迷宫问题(BFS最短路径的记录和打印问题)

    题目链接:http://poj.org/problem?id=3984 宽度优先搜索最短路径的记录和打印问题 #include<iostream> #include<queue> ...

  10. POJ 3984 迷宫问题 bfs 难度:0

    http://poj.org/problem?id=3984 典型的迷宫问题,记录最快到达某个点的是哪个点即可 #include <cstdio> #include <cstring ...

随机推荐

  1. mysql 使用说明-2

    3.3.4 Retrieving Information from a Table Select 命令从表格中取回信息 SELECT what_to_select FROM which_table W ...

  2. NSArray、NSMutableArray基本用法

    NSArray.NSMutableArray基本用法 一.基本操作 初始化方法:1.init返回一个空数组 2.initWithArray从已有数组初始化 3.initWithContentsOfFi ...

  3. transform的使用

    *:first-child { margin-top: 0 !important; } body > *:last-child { margin-bottom: 0 !important; } ...

  4. jdk集合常用方法分析之ArrayList&LinkedList&以及两者的对比分析

    集合使用注意事项: 1.集合当中只能放置对象的引用,无法放置原生数据类型,我们需要使用原生数据类型的包装类才能加入到集合当中去(JDK5之后会进行自动的装箱和拆箱操作,表面上看集合中是可以直接放置原生 ...

  5. DP小乐乐化妆品

    Hrbust1814 http://acm.hrbust.edu.cn/index.php?m=ProblemSet&a=showProblem&problem_id=1814 #in ...

  6. Solr数据库连接之多表关联

    Solr环境配置好后,有很多时候我们需要把数据库里的数据添加到索引里,这时就需要配置跟数据库的连接,下面我们看配置的步骤. 1. 配置 solrconfig.xml  (在slor 主目录 core ...

  7. SASS使用总结

    简单用法: 变量 sass中可以定义变量,方便统一修改和维护. //sass style $fontStack: Helvetica, sans-serif; $primaryColor: #333; ...

  8. centos 7 安装音乐播放器(亲测可用)

    方法来源网上,非原创. 1. Install the nux repo  $> su - $> yum update # optional but recommanded $> rp ...

  9. 过河-状压DP

    http://www.luogu.org/problem/show?pid=1052 题目描述 在河上有一座独木桥,一只青蛙想沿着独木桥从河的一侧跳到另一侧.在桥上有一些石子,青蛙很讨厌踩在这些石子上 ...

  10. 学习总结 java 父子级

    package com.hanqi; //父类 public class Father { // public Father() // { // // } // public Father(Strin ...