首页 > 技术文章 > 算法模板

BGM-WCJ 2022-02-26 21:16 原文

1.最短路 https://www.lanqiao.cn/problems/609/learning/
int dijkstra()
{
    // chushihua
    memset(dist, 0x3f, sizeof dist);
    dist[1] = 0;
    for (int i = 1; i < n; i++)
    {
        int t = -1;
        for (int j = 1; j < n; j++)
            if (!found[j] && (t == -1 || dist[j] < dist[t]))
                t = j;
        found[t] = 1;
        for (int j = 1; j < n; j++)
            dist[j] = min(dist[j], dist[t] + w[t - 1][j - 1]);
    }
    // for (int i = 1; i < n; i++)
    //     cout << dist[i] << ' ';
    // cout << endl;
    return dist[n - 1];
}

 2.方向数组 https://www.lanqiao.cn/problems/819/learning/

#include <iostream>
using namespace std;
int dir[5][2] = {{1, 0}, {0, 1}, {1, 1}, {1, -1}, {-1, 1}};
char op[30][51];
int count = 0;
int main()
{
    // for (int i = 0; i < 30; i++)
    //     for (int j = 0; j < 50; j++)
    //         cin >> op[i][j];
    // for (int i = 0; i < 30; i++)
    //     for (int j = 0; j < 50; j++)
    //     {
    //         for (int k = 0; k < 5; k++)
    //         {
    //             int x = i;
    //             int y = j;
    //             while (true)
    //             {
    //                 x += dir[k][0];
    //                 y += dir[k][1];
    //                 if (x < 0 || x >= 30 || y < 0 || y >= 50)
    //                     break;
    //                 else if (op[x][y] > op[i][j])
    //                 {
    //                     count++;
    //                 }
    //             }
    //         }
    //     }
    // cout << count;
    cout << 52800;
    return 0;
}

 3. dfs 

蓝桥全球变暖: https://www.lanqiao.cn/problems/178/learning/

#include <iostream>
using namespace std;
const int N = 1010;
int n;
char mat[N][N];
bool flag;
int notsum, sum;
int d[4][2] = {{0, -1}, {0, 1}, {-1, 0}, {1, 0}};
void dfs(int x, int y);
int main()
{

  cin >> n;
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= n; j++)
      cin >> mat[i][j];
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= n; j++)
    {
      if (mat[i][j] == '#')
      {
        sum++;
        flag = false;
        dfs(i, j);
      }
    }
  cout << sum - notsum;
  return 0;
}
void dfs(int x, int y)
{
  if (flag == false)
  {
    int cnt = 0;
    for (int i = 0; i < 4; i++)
    {
      int dx = x + d[i][0];
      int dy = y + d[i][1];
      if (mat[dx][dy] != '.')
        cnt++;
    }
    if (cnt == 4)
    {
      ++notsum;
      flag = true;
    }
  }
  mat[x][y] = '^';
  for (int i = 0; i < 4; i++)
  {
    int dx = x + d[i][0];
    int dy = y + d[i][1];
    if (mat[dx][dy] == '#' && x >= 1 && x <= n && y >= 1 && y <= n)
      dfs(dx, dy);
  }
}

蓝桥数字三角形: https://www.lanqiao.cn/problems/505/learning/

#include <iostream>
#include <algorithm>
using namespace std;
const int maxn = 110;

int n;
int maxnum;
int note[maxn] = { 0 }, left1 = 0, right1 = 0, pos = 1;
int a[maxn][maxn];

void dfs(int x, int left, int right) {

    if (x == n) { // 递归边界
        if (abs(left - right) > 1) return;

        note[x] = a[x][1 + right]; //
        int sum = 0;
        for (int i = 1; i <= n; i++) {
            sum += note[i];
        }
        if (sum > maxnum) maxnum = sum; // 刷新最大值
        return;
    }

    note[x] = a[x][1 + right]; // 记录第x层的数字
    dfs(x + 1, left + 1, right);
    dfs(x + 1, left, right + 1);
}


int main()
{
    cin >> n;
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= i; j++) {
            cin >> a[i][j];
        }
    }

    dfs(1,0,0); //

    cout << maxnum << endl;

    getchar();
    getchar();
    return 0;
}

 蓝桥跳跃:https://www.lanqiao.cn/problems/553/learning/

#include <iostream>
using namespace std;
int n, m;
const int N = 110;
int mat[N][N];
int _max;
int dir[9][2] = {{1, 0}, {2, 0}, {3, 0}, {0, 1}, {0, 2}, {0, 3}, {1, 1}, {1, 2}, {2, 1}};
void dfs(int x, int y, int Max);
int main()
{
    cin >> n >> m;
    for (int i = 1; i <= n; i++)
        for (int j = 1; j <= m; j++)
            cin >> mat[i][j];
    dfs(1, 1, 0);
    cout << _max;
    return 0;
}
void dfs(int x, int y, int Max)
{
    if (x > n || y > m)
    {
        return;
    }
    Max += mat[x][y];
    if (x == n && y == m)
    {
        if (Max > _max)
        {
            _max = Max;
        }
        return;
    }
    for (int i = 0; i < 8; i++)
    {
        dfs(x + dir[i][0], y + dir[i][1], Max);
    }
}

 

推荐阅读