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 ...
随机推荐
- html+css源码之实现登录弹出框遮罩层效果
在web开发中,很多网站都做了一些特别炫丽的效果,比如用户登录弹框遮罩层效果,本文章向大家介绍css如何实现登录弹出框遮罩层效果,需要的朋友可以参考一下本文章的源代码. html+css实现登录弹出框 ...
- webform中的Eval的使用
<strong>.在前台html控件调用c#后台变量.</strong> 在后台的类代码里定义一个字符串.如 public partial class Index : Syst ...
- 命令行重新安装.net framework
①运行cmd ②运行命令: cd c:\\windows\\microsoft.net\\framework\v4.0.30319 ③在上述目录下执行如下命令 aspnet_regiis.exe -i ...
- 解决VS2008 开发Windows Mobile 项目生成速度慢的问题
最近用VS2008开发Windows Mobile程序,使用C#..NET Compact Framework,发现项目生成速度比较慢.用VS2008打开项目后,开始一段时间生成速度还能忍受,时间一长 ...
- oracle创建job权限
grant create job to ja_ods; grant manage scheduler to ja_ods;
- python中时间和时区
1.时区 http://blog.csdn.net/cz157733055/article/details/38319195 2.时间 datetime.timedelta代表两个时间之间的的时间差 ...
- USACO Section 3.3 商店购物 Shopping Offers
题目背景 在商店中,每一种商品都有一个价格(用整数表示).例如,一朵花的价格是 2 zorkmids (z),而一个花瓶的价格是 5z .为了吸引更多的顾客,商店举行了促销活动. 题目描述 促销活动把 ...
- leetcode 88
88. Merge Sorted Array Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as on ...
- leetcode 38
38. Count and Say The count-and-say sequence is the sequence of integers beginning as follows:1, 11, ...
- Unieap3.5-前台js用SQL语句执行数据请求
执行UPDATE var sql=" update T_SS_SETTLEMENT_RECORD "+ " set CINVOICE_INFO_FLAG='Y',&quo ...