• 滑雪——DFS,BFS_DP记忆化搜索


    # [SHOI2002] 滑雪

    ## 题目描述

    Michael 喜欢滑雪。这并不奇怪,因为滑雪的确很刺激。可是为了获得速度,滑的区域必须向下倾斜,而且当你滑到坡底,你不得不再次走上坡或者等待升降机来载你。Michael 想知道在一个区域中最长的滑坡。区域由一个二维数组给出。数组的每个数字代表点的高度。下面是一个例子:
    ```
    1   2   3   4   5
    16  17  18  19  6
    15  24  25  20  7
    14  23  22  21  8
    13  12  11  10  9
    ```
    一个人可以从某个点滑向上下左右相邻四个点之一,当且仅当高度会减小。在上面的例子中,一条可行的滑坡为 $24$-$17$-$16$-$1$(从 $24$ 开始,在 $1$ 结束)。当然    $25$-$24$-$23$-$\ldots$-$3$-$2$-$1$ 更长。事实上,这是最长的一条。

    ## 输入格式

    输入的第一行为表示区域的二维数组的行数 $R$ 和列数 $C$。下面是 $R$ 行,每行有 $C$ 个数,代表高度(两个数字之间用 $1$ 个空格间隔)。

    ## 输出格式

    输出区域中最长滑坡的长度。

    ## 样例 #1

    ### 样例输入 #1

    ```
    5 5
    1 2 3 4 5
    16 17 18 19 6
    15 24 25 20 7
    14 23 22 21 8
    13 12 11 10 9
    ```

    ### 样例输出 #1

    ```
    25
    ```

    ## 提示

    对于 $100\%$ 的数据,$1\leq R,C\leq 100$。

    1. #include
    2. #define endl '\n'
    3. #define int long long
    4. #define bug cout << "---------------------" << endl
    5. using namespace std;
    6. constexpr int N = 300;
    7. int res=-1;
    8. int mp[N][N];
    9. int dp[N][N]={1};
    10. bool vis[N][N];
    11. int n, m;
    12. int dir[4][2] = { {-1,0},{0,1},{1,0},{0,-1} };
    13. bool in(int x, int y) {
    14. return x >= 0 && x < n&& y >= 0 && y < m;
    15. }
    16. int dfs(int x,int y) {
    17. if (vis[x][y]) {
    18. return dp[x][y];
    19. }
    20. vis[x][y] = true;
    21. for (int i = 0;i < 4;i++) {
    22. int tx = x + dir[i][0];
    23. int ty = y + dir[i][1];
    24. if (in(tx, ty) && mp[tx][ty] < mp[x][y]) {
    25. dp[x][y] = max(dp[x][y],dfs(tx,ty)+1);
    26. }
    27. }
    28. return dp[x][y];
    29. }
    30. signed main() {
    31. ios_base::sync_with_stdio(0);
    32. cin.tie(0); cout.tie(0);
    33. cin >> n >> m;
    34. for (int i = 0;i < n;i++) {
    35. for (int j = 0;j < m;j++) {
    36. cin >> mp[i][j];
    37. }
    38. }
    39. memset(vis, false, sizeof(vis));
    40. res = -1;
    41. for (int i = 0;i < n;i++) {
    42. for (int j = 0;j < m;j++) {
    43. res = max(res, dfs(i, j));
    44. }
    45. }
    46. cout << res << endl;
    47. }

    第二份代码通过初始化dp数组为-1是否访问过,而且需要注意,如果该点无法走任意的路需要让其为1

    1. #include
    2. #define endl '\n'
    3. #define int long long
    4. #define bug cout << "---------------------" << endl
    5. using namespace std;
    6. constexpr int N = 300;
    7. int res=-1;
    8. int mp[N][N];
    9. int dp[N][N]={1};
    10. int n, m;
    11. int dir[4][2] = { {-1,0},{0,1},{1,0},{0,-1} };
    12. bool in(int x, int y) {
    13. return x >= 0 && x < n&& y >= 0 && y < m;
    14. }
    15. int dfs(int x,int y) {
    16. if (dp[x][y]!=-1) {
    17. return dp[x][y];
    18. }
    19. dp[x][y] = 1;
    20. for (int i = 0;i < 4;i++) {
    21. int tx = x + dir[i][0];
    22. int ty = y + dir[i][1];
    23. if (in(tx, ty) && mp[tx][ty] < mp[x][y]) {
    24. dp[x][y] = max(dp[x][y],dfs(tx,ty)+1);
    25. }
    26. }
    27. return dp[x][y];
    28. }
    29. signed main() {
    30. ios_base::sync_with_stdio(0);
    31. cin.tie(0); cout.tie(0);
    32. cin >> n >> m;
    33. for (int i = 0;i < n;i++) {
    34. for (int j = 0;j < m;j++) {
    35. cin >> mp[i][j];
    36. }
    37. }
    38. res = -1;
    39. memset(dp, -1, sizeof(dp));
    40. for (int i = 0;i < n;i++) {
    41. for (int j = 0;j < m;j++) {
    42. res = max(res, dfs(i, j));
    43. }
    44. }
    45. cout << res << endl;
    46. }

    第三份宽搜代码:实现方式与DFS几乎类似;

    不同之处在于进行了顺序优化,将所有的可能性,以结构体存入队列进行依次访问!

    1. #include //队列的头文件
    2. #include
    3. #include //我写代码都是直接吧这两个头文件直接写上去的不管用没有用
    4. #include //我不想写一个max函数了
    5. using namespace std;
    6. struct Good //对于b数组和队列q i和j都是储存该点的坐,对于数组b来说 num储存的是该座标的值队列q储存的是该坐标元素是从前到后第几个元素
    7. {
    8. int i;
    9. int j;
    10. int num;
    11. }b[10010];
    12. int a[110][110];//正确的储存读入的二维数组
    13. bool cmp(const Good& a, const Good& b)
    14. {
    15. return a.num > b.num;//使该结构体如果要排序的话按照降序方式排序
    16. }
    17. const int lj[4][2] = { {-1,0},{0,1},{1,0},{0,-1} };//上右下左的顺序
    18. int main()
    19. {
    20. queueq;//定义一个队列
    21. int qs = 0;//储存b数组的元素好像可以用其他代数式代替,不过无所谓了
    22. int n, m;
    23. cin >> n >> m;
    24. for (int i = 1;i <= n; i++)
    25. for (int j = 1; j <= m; j++)
    26. {
    27. cin >> a[i][j];//正确的读入
    28. b[qs].i = i;//按顺序读入到一维数组中
    29. b[qs].j = j;
    30. b[qs++].num = a[i][j];
    31. }
    32. int ans = -1;//先初始化最长的长度为-1
    33. sort(b, b + qs, cmp);//排序b数组
    34. for (int i = 0; i < qs / 2 + 1; i++)//这里的qs/2是一个玄学优化,不过洛谷上好像没有数据能卡这个
    35. {
    36. q.push({ b[i].i,b[i].j,1 });//先找最大的一个元素入列,后面的元素依次入列
    37. while (!q.empty())//如果队列不为空
    38. {
    39. Good qa = q.front();//取出队首元素
    40. q.pop();
    41. ans = max(qa.num, ans);//判断长度
    42. for (int j = 0;j < 4; j++)
    43. {
    44. int x = qa.i + lj[j][0];//往四个方向依次查找
    45. int y = qa.j + lj[j][1];
    46. if (x >= 1 && x <= n && y >= 1 && y <= m && a[x][y] < a[qa.i][qa.j])//如果这个数没有出界,并且严格小于上一个元素就入列
    47. {
    48. q.push({ x,y,qa.num + 1 });
    49. }
    50. }
    51. }
    52. }
    53. cout << ans << endl;//输出最大长度
    54. return 0;
    55. }
  • 相关阅读:
    php在header增加key,sign,timestamp,实现鉴权
    python基础之字典的遍历
    【人生苦短,我学 Python】(1)初识 Python
    mvvm讲解
    【牛客网-在线编程-Python入门篇】——开篇介绍
    Vue3 —— 常用 Composition API(二)(hook 函数、toRef 和 toRefs)
    去除社区版本 idea 没有添加括号的报红
    设计模式之代理模式
    RHCE(逻辑卷LVM,NFS服务)
    在Vs-Code中配置“@”路径提示的插件
  • 原文地址:https://blog.csdn.net/m0_72522122/article/details/127630931