zoukankan      html  css  js  c++  java
  • BFS和DFS搜索

    BFS算法求迷宫的最短路径问题

    • 'S'是起点 'G'是终点
    • '#'是墙壁
    • '.'是可走的路径

    样例输入

    10 10
    .#S#######
    .#........
    ..#..#....
    ...#......
    .....#....
    ..#....##.
    .....##...
    ..##......
    ....#.....
    ..G.......
    

    样例输出

    15
    
    #include <iostream>
    #include <queue>
    using namespace std;
    struct point {
    	int x, y;
    	
    	point(int xx, int yy)  {
    		x = xx;
    		y = yy;
    	}
    };
    const int INF = 100000;
    
    int n, m;//地图大小
    
    int endx, endy;
    int sx, sy;
    int dir[4][2] = { {1, 0}, {0, -1}, {-1, 0}, {0 , 1} };//D L U R
    char map[105][105];
    int len[105][105];
    queue<point> q;
    
    bool in(int x, int y) {
    	if (x >= 0 && x < n && y >= 0 && y < m) return true;
    	return false;
    }
    void dfs() {
    	q.push(point(sx, sy));
    
    	len[sx][sy] = 0;
    
    	while (!q.empty()) {
    		point p = q.front(); q.pop();
    
    		for (int i = 0; i < 4; i++) {
    			int newx = p.x + dir[i][0];
    			int newy = p.y + dir[i][1];
    			if (in(newx, newy) && map[newx][newy] != '#' && len[newx][newy] == INF) {
    				
    				q.push(point(newx, newy));
    				len[newx][newy] = len[p.x][p.y] + 1;
    				if (newx == endx && newy == endy) {
    					return;
    				}
    			}
    
    		}
    
    	}
    
    }
    
    int main() {
    	cin >> n >> m;
    	for (int i = 0; i < n; i++) {
    		for (int j = 0; j < m; j++) {
    
    			len[i][j] = INF;
    			cin >> map[i][j];
    			if (map[i][j] == 'S') {
    				sx = i;
    				sy = j;
    			}
    			else if (map[i][j] == 'G') {
    				endx = i;
    				endy = j;
    			}
    		}
    	}
    	dfs();
    	printf("%d
    ", len[endx][endy]);
    	
    
    	return 0;
    }
    
    
    
    

    DFS代码框架范例

    #include <stdio.h>
    int n, m, p, q, min = 99999999; // n,m代表地图边界, p,q代表终点的坐标, min 代表最小步数
    int a[51][51], v[51][51]; // a是map, v代表地图上的点位是否访问过
    int next[4][2] = { // 下一步要走的四个方向
        {0, 1},
        {1, 0},
        {0, -1},
        {-1, 0}
    }
    bool in(int x, int y) { //是否在地图里面
        if (x >= 0 && y >= 0 && x < n && y <m) {
            return true;
        }
        return false;
    }
    void dfs(int x, int y, int step) { // 深度优先搜索
        if (x == p && y == q) { // 每一种解决方案的递归出口
            //更新最小值
            if (min > step) {
                min = step;
            }
            return;
        }
    
        for (int i = 0; i < 4 ;i++) { // 向四个方向寻找
            int dx = x + next[i][0];
            int dy = y + next[i][1];
            if (in(dx, dy) && v[dx][dy] == 0 && a[x][y] == 0) { // 满足在地图内且没有被访问过并且不是障碍物
                v[dx][dy] = 1; // 标记访问过
                dfs(tx, ty, step + 1);//走下一步 步数+1
                v[dx][dy] = 0; // 标记没有访问过 因为要求最短路径 可能第一次经过这个点并不是最优解
            }
        }
        return;
    } 
    int main() {
        int i,j, startx,starty; // startx 和 starty是起点坐标
        scanf("%d%d",&n, &m);
        for (i = 0; i < n; i++) {
            for (j = 0; j < n; j++) {
                scanf("%d", &a[i][j]);
            }
        }
        scanf("%d%d%d%d", &startx,&starty, &p, &q);
        v[startx][starty]= 1;//标记起点访问过
        dsf(startx, starty, 0);//从起点开始递归
        printf("%d
    ", min);
        
        return 0;
    }
    
  • 相关阅读:
    《活在恩典中》第一章 人类的两难困境
    《真正的修行》把你内心的一切都呈现出来
    Mysql:Plugin:clone=mysql_clone:as of 8.0.17
    Mysql:--init-file && --init-connect
    Mysql:8.0.19:Upgrading Mysql:升级
    Android开发自定义View
    Android控制UI界面
    Android的视图(View)组件
    对Android应用签名
    Android Application的基本组件介绍
  • 原文地址:https://www.cnblogs.com/DengSchoo/p/12568512.html
Copyright © 2011-2022 走看看