走迷宫问题,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. 项目积累——CSS应用

    <tr onmouseover=" this.style.backgroundColor= '#E0FFFF' "  onmouseout="this.style. ...

  2. html5—— 应用程序缓存

    使用 HTML5,通过创建 cache manifest 文件,可以轻松地创建 web 应用的离线版本. 什么是应用程序缓存(Application Cache)? HTML5 引入了应用程序缓存,这 ...

  3. OC基础(14)

    Xcode设置 内存管理原则 *:first-child { margin-top: 0 !important; } body > *:last-child { margin-bottom: 0 ...

  4. JAVA~多线程:sleep、yield方法

    sleep不考虑其它线程的优先级 yield让位给相同或更高优先级的线程 sleep yield package multiThread2; public class TestThread042Yie ...

  5. Facebook三种分享方式

    一.去Facebook开发者中心注册APP,获取APP ID https://developers.facebook.com 二.导入 FBSDKCoreKit.Framework, FBSDKLog ...

  6. C#中如何判断联系电话的合法性

    string tel = tb_tel.Text.Trim();//联系电话if (!string.IsNullOrEmpty(tb_tel.Text.Trim())){try{//num = Con ...

  7. Duilib学习笔记《05》— 消息响应处理

    在Duilib学习笔记<04>中已经知道了如何将窗体显示出来,而如何处理窗体上的事件.消息呢? 一. 系统消息 窗体显示的时候我们就已经说了,窗体是继承CWindowWnd类的,对于窗体的 ...

  8. html5 搖一搖

    <script> // 首先在页面上要监听运动传感事件 function init(){ if (window.DeviceMotionEvent) { // 移动浏览器支持运动传感事件 ...

  9. centreon 降低rrd磁盘读写

    参考 https://documentation.centreon.com/docs/centreon/en/2.7.x/faq/performance.html 修改rrdcached配置 vim ...

  10. OpenGL函数解析之gluPerspective()

    函数原型: void gluPerspective(GLdouble fovy,GLdouble aspect,GLdouble zNear,GLdouble zFar); 参数说明: fovy:指定 ...