• UVa 10285【搜索】


    UVa 10285

    哇,竟然没超时!看网上有人说是记忆化搜索,其实不太懂是啥。。。感觉我写的就是毫无优化的dfs暴力。。。。。。。

    建立一个坐标方向结构体数组,每个节点dfs()往下搜就好了。

     1 #include<iostream>
     2 #include<cstdio>
     3 #include<cstring>
     4 #include<algorithm>
     5 #define MOD 100000
     6 using namespace std;
     7 const int maxn = 105;
     8 struct pos {
     9     int x, y;
    10 };
    11 pos dir[4] = { 1,0,-1,0,0,1,0,-1 };
    12 int Map[maxn][maxn];
    13 int vis[maxn][maxn] = { 0 };
    14 int r, c, ans;
    15 
    16 bool in(int x, int y, int val)
    17 {
    18     if (Map[x][y]<val && !vis[x][y] && x >= 1 && x <= r&&y >= 1 && y <= c) return true;
    19     return false;
    20 }
    21 
    22 void dfs(int x, int y, int lev, int dep)
    23 {
    24     ans = max(ans, dep);
    25     for (int i = 0; i<4; i++) {
    26         int nowx=x, nowy=y;
    27         nowx += dir[i].x, nowy += dir[i].y;
    28         if (in(nowx, nowy, lev)) {
    29             vis[nowx][nowy] = 1;
    30             int t = dep + 1;
    31             dfs(nowx, nowy, Map[nowx][nowy], t);
    32             vis[nowx][nowy] = 0;
    33         }
    34     }
    35     return;
    36 }
    37 
    38 int main()
    39 {
    40     int N;
    41     cin >> N;
    42     char s[100];
    43     while (N--)
    44     {
    45         scanf("%s", s);
    46         scanf("%d%d", &r, &c);
    47         memset(vis, 0, sizeof(vis));
    48         for (int i = 1; i <= r; i++)
    49             for (int j = 1; j <= c; j++) {
    50                 cin >> Map[i][j];
    51             }
    52         ans = 1;
    53         for(int i=1;i<=r;i++)
    54             for (int j = 1; j <= c; j++) {
    55                 vis[i][j] = 1;
    56                 dfs(i, j, Map[i][j], 1);
    57                 vis[i][j] = 0;
    58             }
    59         cout << s << ": " << ans << endl;
    60     }
    61     return 0;
    62 }
  • 相关阅读:
    leetcode-14
    贪心算法
    MySQL索引
    leetcode-13
    leetcode-12
    leetcode-11
    深度和广度优先搜索
    CentOS出错You don't have permission to access on this server
    linux给文件或目录添加apache权限
    让CentOS在同一个窗口打开文件夹
  • 原文地址:https://www.cnblogs.com/zxhyxiao/p/7339055.html
Copyright © 2020-2023  润新知