迷宫问题(DFS,BFS)

  1 /********************************
  2 啊哈!算法
  3 深度优先搜索算法
  4 迷宫问题
  5 输入:
  6 5 4
  7 0 0 1 0
  8 0 0 0 0
  9 0 0 1 0
 10 0 1 0 0
 11 0 0 0 1
 12 1 1 4 3
 13
 14 输出:7
 15 *********************************/
 16 #include<iostream>
 17 #include<ctime>
 18
 19 using namespace std;
 20 int count=0;
 21 int endx,endy,m,n;
 22 void dfs(int **pos,bool **book,int sx,int sy,int step,int &min)
 23 {
 24     if(sx==endx && sy==endy)
 25     {
 26         if(step<=min)
 27             min=step;
 28         cout<<"第"<<count++<<"方案:"<<step<<endl;
 29         return;//出口
 30     }
 31     int move[4][2]={
 32         {0,1},
 33         {0,-1},
 34         {1,0},
 35         {-1,0}
 36     };
 37
 38     int tx,ty;
 39     for(int i=0;i<4;i++)
 40     {
 41         tx=sx+move[i][0];
 42         ty=sy+move[i][1];
 43         if(tx>m || tx<1 || ty>n || ty<1)
 44             continue;
 45         if(pos[tx][ty]==0 && book[tx][ty]==0)
 46         {
 47             book[tx][ty]=true;
 48             dfs(pos,book,tx,ty,step+1,min);
 49             book[tx][ty]=0;//**尝试结束,取消这个点的标记
 50         }
 51     }
 52 }
 53
 54
 55 int main()
 56 {
 57     int i,j;
 58     cout<<"输入迷宫的行列:";
 59     cin>>m>>n;
 60     int **pos=new int *[m+1];//迷宫。指针数组
 61     bool **book=new bool*[m+1];//记录是否走过
 62     for(i=1;i<m+1;i++)
 63     {
 64         pos[i]=new int[n+1];
 65         book[i]=new bool[n+1];
 66     }
 67
 68     cout<<"输入迷宫(1表示障碍物,0表示通道,空格隔开):"<<endl;
 69     for(i=1;i<m+1;i++)
 70     {
 71         for(j=1;j<n+1;j++)
 72         {
 73
 74             cin>>pos[i][j];
 75             while(!cin.good())
 76             {
 77                 cin.clear();
 78                 while(cin.get()!=‘\n‘)
 79                     continue;
 80                 cout<<"在第"<<i+1<<"行"<<"第"<<j+1<<"列"<<"输入错误"<<endl;
 81                 cout<<"重新输入:"<<endl;
 82                 cin>>pos[i][j];
 83             }
 84         }
 85     }
 86
 87     cout<<endl<<"迷宫:"<<endl;
 88     for(i=1;i<m+1;i++)
 89     {
 90         for(j=1;j<n+1;j++)
 91             cout<<pos[i][j]<<" ";
 92         cout<<endl;
 93     }
 94
 95
 96     for(i=1;i<m+1;i++)
 97         for(j=1;j<n+1;j++)
 98             book[i][j]=0;
 99
100     int startx,starty;
101     cout<<"输入起始点: ";
102     cin>>startx>>starty;
103     book[startx][starty]=true;
104
105     cout<<"输入终点: ";
106     cin>>endx>>endy;
107
108     int step=0,min=99999;
109
110     dfs(pos,book,startx,starty,step,min);
111     if (min<99999)
112         cout<<"最短步数是: "<<min<<endl;
113     else cout<<"不存在路径"<<endl;
114
115
116     for(i=1;i<m+1;i++)
117     {
118         delete [] pos[i];
119         delete [] book[i];
120     }
121     delete [] pos;
122     delete [] book;
123     return 0;
124 }
  1 /**********************
  2 BFS
  3 *************/
  4 #include<iostream>
  5 using namespace std;
  6
  7 struct node
  8 {
  9     int x;
 10     int y;
 11     int f;//记录路径
 12     int s;//记录步长
 13 };
 14
 15 struct stack
 16 {
 17     int st[100];
 18     int top;
 19 };
 20 int main()
 21 {
 22     node queue[2500];
 23
 24     bool book[51][51]={false};
 25     int m,n,sx,sy,ex,ey;
 26     cout<<"row and column:";
 27     cin>>m>>n;
 28     int i,j;
 29     int **pos=new int*[m+1];
 30     for(i=0;i<m+1;i++)
 31         pos[i]=new int[n+1];
 32
 33     cout<<"screat map:"<<endl;
 34     for(i=1;i<m+1;i++)
 35         for(j=1;j<n+1;j++)
 36             cin>>pos[i][j];
 37     cout<<"start and end:";
 38     cin>>sx>>sy>>ex>>ey;
 39     book[sx][sy]=1;
 40     int head=0, tail=0;
 41     queue[head].x=sx;  //定义后初始化只能以这种方式,出发点
 42     queue[head].y=sy;
 43     queue[head].f=head;
 44     queue[head].s=0;
 45     tail++;//tail超前队列最后一个元素一位
 46
 47     int move[][2]={
 48         {0,1},{-1,0},{0,-1},{1,0}
 49     };
 50
 51     int goal=0;
 52     while(head!=tail)
 53     {
 54         if( queue[head].x==ex && queue[head].y==ey)
 55         {
 56             goal=head;
 57             cout<<"最短路径:"<<queue[head].s<<endl;
 58             head++;
 59             break; //广度优先搜索最先找到的就是最短的
 60         }
 61         for(i=0;i<4;i++)
 62         {
 63             node t={queue[head].x+move[i][0],queue[head].y+move[i][1],head,queue[head].s+1};
 64             //遍历四个方向如果合法且没被标记则入队
 65             if(t.x>m || t.x<1 || t.y>n || t.y<1)
 66                 continue;
 67             if(pos[t.x][t.y]==0 && book[t.x][t.y]==false)
 68             {
 69                 queue[tail]=t;//结构体可整体复制
 70                 tail++;
 71                 book[t.x][t.y]=true;//注意走过的路要标记
 72             }
 73         }
 74         head++;
 75
 76     }
 77     //打印路径
 78     cout<<"队列中的位置是:"<<endl;
 79     i=goal;cout<<goal<<endl;
 80     stack re={{0},0};
 81     while(queue[i].s>=0)//直到回溯到起始点
 82     {
 83         re.st[re.top++]=i;//反着从终点到起点入栈
 84         i=queue[i].f;//这里错了,不要i--,i记录上一个位置的前任(在队列中的位置)
 85         if(i==0)//起始点的前任是它自己,要标记退出,不然死循环
 86             break;
 87
 88     }
 89     while(re.top>=0)
 90     {
 91         cout<<queue[re.st[re.top]].x<<" "<<queue[re.st[re.top]].y;//先进后出,出栈,正着打印
 92         if(re.top!=0)
 93             cout<<"->";
 94         re.top--;
 95     }
 96     cout<<endl;
 97
 98     for(i=0;i<m+1;i++)
 99         delete [] pos[i];
100     delete [] pos;
101     return 0;
102 }
时间: 2024-10-27 01:36:31

迷宫问题(DFS,BFS)的相关文章

FZU1205/SDUT1157_小鼠迷宫问题(DFS+BFS)

解题报告 http://blog.csdn.net/juncoder/article/details/38146041 题目传送门 题意 求最短路和最短路的路数. 思路: BFS+DFS,先求出最短路.在DFS搜等于最短路的条数. 不加优化SDUTOJ过了,数据就是水. 确定了最短路的长度,加上奇偶剪枝FOJ也过了. #include <queue> #include <cmath> #include <cstdio> #include <cstring>

【DFS/BFS】NYOJ-58-最少步数(迷宫最短路径问题)

[题目链接:NYOJ-58] 经典的搜索问题,想必这题用广搜的会比较多,所以我首先使的也是广搜,但其实深搜同样也是可以的. 不考虑剪枝的话,两种方法实践消耗相同,但是深搜相比广搜内存低一点. 我想,因为广搜需要的就是队列,所以相比递归队列更耗内存? 当然DFS并不像上图所说,需要用栈,而是运用递归即可. BFS: 因为BFS是要一个接一个的遍历,所以用到了结构体,来保存坐标和当前所走步数 1.每走一步,通过定义的结构体,从队列中提取a(即上一步的坐标.步数(步数每次累加)) 2.在a的基础上进行

poj3083——dfs+bfs综合题

POJ 3083   dfs+bfs+模拟 Children of the Candy Corn Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10564   Accepted: 4539 Description The cornfield maze is a popular Halloween treat. Visitors are shown the entrance and must wander through

POJ 3083:Children of the Candy Corn(DFS+BFS)

Children of the Candy Corn Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9311 Accepted: 4039 Description The cornfield maze is a popular Halloween treat. Visitors are shown the entrance and must wander through the maze facing zombies, ch

POJ 3083 -- Children of the Candy Corn(DFS+BFS)TLE

POJ 3083 -- Children of the Candy Corn(DFS+BFS) 题意: 给定一个迷宫,S是起点,E是终点,#是墙不可走,.可以走 1)先输出左转优先时,从S到E的步数 2)再输出右转优先时,从S到E的步数 3)最后输出S到E的最短步数 解题思路: 前两问DFS,转向只要控制一下旋转方向就可以 首先设置前进方向对应的数字 向上--N--0 向右--E--1 向下--S--2 向左--W--3 比如说右转优先,即为向右,向前,向左,向后,即逆时针方向for(int i

Dearboy&#39;s Puzzle (poj 2308 搜索 dfs+bfs)

Language: Default Dearboy's Puzzle Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 1202   Accepted: 208 Description Dearboy is a game lover. Recently, he loves playing the game Lian Lian Kan. This game is played on a board with N*M grids

HDU 4771 (DFS+BFS)

Problem Description Harry Potter has some precious. For example, his invisible robe, his wand and his owl. When Hogwarts school is in holiday, Harry Potter has to go back to uncle Vernon's home. But he can't bring his precious with him. As you know,

HDU 4771 Stealing Harry Potter&#39;s Precious dfs+bfs

Stealing Harry Potter's Precious Problem Description Harry Potter has some precious. For example, his invisible robe, his wand and his owl. When Hogwarts school is in holiday, Harry Potter has to go back to uncle Vernon's home. But he can't bring his

SDUT 走迷宫(DFS)

走迷宫 Time Limit: 1000ms   Memory limit: 65536K  有疑问?点这里^_^ 题目描述 一个由n * m 个格子组成的迷宫,起点是(1, 1), 终点是(n, m),每次可以向上下左右四个方向任意走一步,并且有些格子是不能走动,求从起点到终点经过每个格子至多一次的走法数. 输入 第一行一个整数T 表示有T 组测试数据.(T <= 110) 对于每组测试数据: 第一行两个整数n, m,表示迷宫有n * m 个格子.(1 <= n, m <= 6, (n

HDU1728 逃离迷宫 【方向BFS】

逃离迷宫 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 15120    Accepted Submission(s): 3650 Problem Description 给定一个m × n (m行, n列)的迷宫,迷宫中有两个位置,gloria想从迷宫的一个位置走到另外一个位置,当然迷宫中有些地方是空地,gloria可以穿越,有些地