zoukankan      html  css  js  c++  java
  • P1605 迷宫题解

    题目传送门

    感悟与总结

    1、典型的深度优先搜索,记录路线条数一般是深度优先搜索。
    2、没了,就这些。

    #include <bits/stdc++.h>
    
    using namespace std;
    typedef long long LL;
    const int N = 10;
    
    int n, m, t;        //n为行,m为列,t为障碍总数
    int sx, sy, fx, fy; //起点坐标sx,sy,终点坐标fx,fy。
    int a[N][N];        // 地图
    //上下左右
    int dx[4] = {0, 0, -1, 1};
    int dy[4] = {1, -1, 0, 0};
    int cnt;
    
    //不能走回头路
    bool st[N][N];
    
    //深度优先搜索
    void dfs(int x, int y) {
        //如果到达终点,那么算是多了一组解,并return返回
        if (x == fx && y == fy) {
            cnt++;
            return;
        }
        for (int i = 0; i < 4; i++) {
            //可能走到哪个位置
            int x1 = x + dx[i];
            int y1 = y + dy[i];
            //1、不出界
            //2、没走过
            //3、没有障碍物
            if (x1 >= 1 && x1 <= n && y1 <= m && y1 >= 1 && !st[x1][y1] && !a[x1][y1]) {
                st[x1][y1] = true;
                dfs(x1, y1);
                st[x1][y1] = false;
            }
        }
    }
    
    int main() {
        //输入
        cin >> n >> m >> t >> sx >> sy >> fx >> fy;
        while (t--) {
            int x, y;
            cin >> x >> y;
            a[x][y] = -1;
        }
        //出发点走过了
        st[sx][sy] = true;
        // 从sx,sy出发,想要到达fx,fy,中间有障碍,深度优先搜索
        dfs(sx, sy);
        //输出方案数
        printf("%d", cnt);
        return 0;
    }
    
  • 相关阅读:
    反射-基础方法-java
    排序-插入-java
    排序-选择-java
    决策树
    python基础2 -画图
    python基础1
    如何实现用户的历史记录功能(最多n条)
    如何让字典保持有序
    如何快速找到多个字典中的公共键(key)
    如何根据字典中值的大小, 对字典中的项排序
  • 原文地址:https://www.cnblogs.com/littlehb/p/15066410.html
Copyright © 2011-2022 走看看