矩阵搜索、图相关算法整理

dfs ,求连通块等

/*
5 5
****@
*@@*@
*@**@
@@@*@
@@**@
*/
#include 
#include 
#include 
using namespace std;
const int maxn = 105;

char pic[maxn][maxn];
int m, n, idx[maxn][maxn];

void dfs(int r, int c, int id)
{
    if (r < 0 || c >= m || r >= n || c < 0)
        return;
    if (idx[r][c] > 0 || pic[r][c] != '@')
        return;
    idx[r][c] = id;
    for (int dr = -1; dr <= 1; ++dr) {
        for (int dc = -1; dc <= 1; ++dc) {
            if (dr != 0 || dc != 0)
                dfs(r + dr, c + dc,id);
        }
    }
}

int main()
{
    cin >> m >> n;
    for (int i = 0; i < m; ++i) {
        cin >> pic[i];
    }
    memset(idx,0,sizeof(idx));
    int cnt = 0;
    for (int i = 0; i < m; ++i) {
        for (int j = 0; j < n; ++j) {
            if (idx[i][j] == 0 && pic[i][j] == '@') {
                dfs(i,j,++cnt);
            }
        }
    }
    printf("%d\n", cnt);
    return 0;
}

dfs ,指定路径搜索

/*
5 5 3
hello help high
p a b h m
f h e c p
o i l l h
b g h o n
h x c m l
*/
#include 
#include 
#include 
#include 
#include 
using namespace std;
const long MAXN = 1005;
char dic[MAXN][MAXN];
int idx[MAXN][MAXN];
int M, N, K;

bool dfs(int r, int c, const string &word,int& pathlen)
{
    if (pathlen >= word.size())
        return true;
    bool haspath = false;
    if (r >= 0 && r < M && c >= 0 && c <= N &&
        idx[r][c] != 1 && dic[r][c] == word[pathlen]) {
        ++pathlen;
        idx[r][c] = 1;
        haspath = dfs(r - 1, c, word, pathlen) || dfs(r + 1, c, word, pathlen) || 
                  dfs(r, c - 1, word, pathlen) || dfs(r , c + 1, word, pathlen);
        if (!haspath) {
            --pathlen;
            idx[r][c] = 0;
        }
    }
    return haspath;
}

bool search(const string &word)
{
    int pathlen = 0;
    for (int i = 0; i < M; ++i) {
        for (int j = 0; j < N; ++j) {
            if (dfs(i,j,word,pathlen)) {
                return true;
            }
        }
    }
    return false;
}

int main()
{
    
    cin >> M >> N >> K;
    vector words;
    string inli;
    for (int i = 0; i < K; ++i) {
        cin >> inli;
        words.push_back(inli);
    }
    char c;
    for (int i = 0; i < M; ++i) {
        for (int j = 0; j < N; ++j) {
            cin >> c;
            dic[i][j] = c;
        }
    }
    

    for (int l = 0; l < K; ++l) {
        memset(idx, 0, sizeof(idx));
        if (search(words[l]))
            cout << words[l] << endl;
    }
    

    return 0;
}

BFS求迷宫距离

/*
//M,N表示迷宫的大小; #表示无法阻塞块;S为开始位置;G为终点;.表示可以通过的块。
10 10
#S######.#
......#..#
.#.##.##.#
.#........
##.##.####
....#....#
.#######.#
....#.....
.####.###.
....#...G#
*/
#include 
#include 
#include 

using namespace std;
const int MAX_N = 100;
const int MAX_M = 100;
const int INF = 0x3f3f3f3f;

typedef pair P;
char maze[MAX_N][MAX_M + 1];
int N, M;
int sx, sy; //begin pointer
int gx, gy; //end pointer

int d[MAX_N][MAX_M];//
int dx[4] = { 1,0,-1,0 },
    dy[4] = { 0,1,0,-1 }; //

void bfs()
{
    queue

que; for (int i = 0; i < N; i++){ for (int j = 0; j < M; j++){ d[i][j] = INF; } } que.push(P(sx, sy)); d[sx][sy] = 0; while (que.size()) { P p = que.front(); que.pop(); if (p.first == gx && p.second == gy) break; for (int i = 0; i < 4; i++){ int nx = p.first + dx[i]; int ny = p.second + dy[i]; if (nx >= 0 && nx < N && ny >= 0 && ny < M && maze[nx][ny] != '#' && d[nx][ny] == INF) { que.push(P(nx, ny)); d[nx][ny] = d[p.first][p.second] + 1; } } } } int main() { scanf("%d %d", &N, &M); for (int n = 0; n < N; n++) scanf("%s", &maze[n]); for (int i = 0; i < N; i++){ for (int j = 0; j < M; j++){ if (maze[i][j] == 'S'){ sx = i; sy = j; } if (maze[i][j] == 'G'){ gx = i; gy = j; } } } bfs(); for (int i = 0; i < N; i++){ for (int j = 0; j < M; j++){ if(d[i][j] == INF) printf("#, "); else printf("%d, ",d[i][j]); } printf("\n"); } printf("%d\n", d[gx][gy]); return 0; }

你可能感兴趣的:(矩阵搜索、图相关算法整理)