• 笔试题整理 第一题


    #include <iostream>
    #include <vector>
    #include "cmath"
    
    using namespace std;
    int Pathnum = 0;
    void num_path(vector<vector<int>>, vector<vector<int>>, int, int, int, int, int, int);
    
    int main()
    {
        int n, m;
        cin >> n;
        cin >> m;
        vector<vector<int>>haiba(n, vector<int>(m, 0));//注意这里的二维数组建立
        vector<vector<int>>mask(n, vector<int>(m, 0));
        for (int i = 0; i < n; i++)
        {
            for (int j = 0; j < m; j++)
            {
                cin >> haiba[i][j];
                mask[i][j] = 0;
            }
        }
        int ax,ay,bx,by;
        cin >> ax;
        cin >> ay;
        cin >> bx;
        cin >> by;
    
        num_path(haiba, mask, n, m, ax, ay, bx, by);
    
        cout << Pathnum % int(pow(10, 9)) << endl;
        system("pause");
    }
    
    void num_path(vector<vector<int>> haiba, vector<vector<int>> mask, int n, int m, int ax, int ay, int bx, int by)
    {
        if (ax == bx && ay == by)
        {
            Pathnum++;
            return;
        }
    
        mask[ax][ay] = 1;
        if (ax + 1 < n && mask[ax + 1][ay] != 1 && haiba[ax + 1][ay] > haiba[ax][ay])
            num_path(haiba, mask, n, m, ax+1, ay, bx, by);
        if (ax - 1 > 0 && mask[ax - 1][ay] != 1 && haiba[ax - 1][ay] > haiba[ax][ay])
            num_path(haiba, mask, n, m, ax - 1, ay, bx, by);
        if (ay + 1 < m && mask[ax][ay + 1] != 1 && haiba[ax][ay + 1] > haiba[ax][ay])
            num_path(haiba, mask, n, m, ax, ay + 1, bx, by);
        if (ay - 1 > 0 && mask[ax][ay - 1] != 1 && haiba[ax][ay - 1] > haiba[ax][ay])
            num_path(haiba, mask, n, m, ax, ay - 1, bx, by);
        mask[ax][ay] = 0;
    }

    分析:就是在二维数组那里卡了一下,不然也不会整理这个题。

  • 相关阅读:
    UVa 482
    UVa 10360
    UVa 10812
    UVa 10420
    UVa 573
    UVa 357
    UVa 147
    UVa 674
    郑厂长系列故事——逃离迷宫 HDU4524
    威威猫系列故事——过生日 HDU 4523
  • 原文地址:https://www.cnblogs.com/CJT-blog/p/10686280.html
Copyright © 2020-2023  润新知