笔试题整理 第一题

#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;
}

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

猜你喜欢

转载自www.cnblogs.com/CJT-blog/p/10686280.html