深度优先算法(DFS)

  1. 前言:英文缩写为DFS,可笑的是,我最早去HP公司面试做笔试题的时候, 竟然没看出来是什么意思,当然即使知道是这个算法,当时也不太会写。本科都是学过的,就是后来忘记了。
  2. 思路说明:理解深度优先算法的关键在于解决“当下该如何做”。至于下一步应该如何做,和当下应该如何做是一样的,对!就是用递归!自己调用自己!
  3. 递归函数模型:
void dfs(int step)
{
    判断边界,如果到了边界当然直接返回啦
    尝试每一种可能结果for(i=0;i<n;i++)
    {
        处理当前步
        继续下一步dfs(step + 1)
    }
    返回
}

4.应用于图的遍历:
这里我们用二位数组储存图,叫做图的邻接矩阵存储法。
图的邻接矩阵表示法

#include<stdio.h>

int graph[100][100];
int book[100];
int m,n,sum;

int que[10000],head,tail;

void dfs(int cur)
{
    int i;
    printf("%d ",cur);
    sum++;
    if(sum == n)
    {
        return;
    }
    for(i=0;i<n;i++)
    {
        if(graph[cur][i] == 1 && book[i] == 0)
        {
            book[i] = 1;
            dfs(i);
        }
    }
    return;
}
int main()
{
    int i,j,m,a,b;
    printf("Please input n lines and columns and m links to build a matrix!\n");
    scanf("%d %d",&n,&m);
    for(i=0;i<n;i++)
        for(j=0;j<n;j++)
            if(i == j)
                graph[i][j] = 0;
            else
                graph[i][j] = 99999999;
    for(i=0;i<m;i++)
    {
        printf("Set linked line between nodes!\n");
        scanf("%d %d",&a,&b);
        graph[a][b] = graph[b][a] = 1;
    }
    book[0] = 1;
    dfs(0);
    //bfs(0);
    return 0;
}

运行结果:

Please input n lines and columns and m links to build a matrix!
5 5
Set linked line between nodes!
0 1
Set linked line between nodes!
0 2
Set linked line between nodes!
0 4
Set linked line between nodes!
1 3
Set linked line between nodes!
2 4
0 1 3 2 4
Press any key to continue

发布了26 篇原创文章 · 获赞 13 · 访问量 6万+
展开阅读全文

【求助】BFS 类似迷宫问题

12-26

![图片说明](https://img-ask.csdn.net/upload/201612/26/1482741735_984127.png) 运行没有结果 附上我自己写的代码 有人能帮忙看看吗 麻烦啦 #include<stdio.h> #include<iostream> #include<algorithm> #include<queue> using namespace std; struct node { int x; int y; char dir; }pre[105][105]; int dx[] = {1, -1, 0, 0}; int dy[] = {0, 0, 1, -1}; int visit[105][105]; int map[105][105]; node sta, end; int flag = 0; int m, n; void bfs() { queue<int>p; queue<int>q; int i; visit[sta.x][sta.y] = 1; node temp,next; p.push(sta.x); q.push(sta.y); while(!p.empty()) { temp.x = p.front(); p.pop(); temp.y = q.front(); q.pop(); for(i = 0; i < 4; i++) { if((i == 0) || (i == 1)) next.dir = 'h'; else next.dir = 's'; next.x = temp.x + dx[i]; next.y = temp.y + dy[i]; if((map[next.x][next.y] == 0) && (!visit[next.x][next.y])) { visit[next.x][next.y] = 1; p.push(next.x); q.push(next.y); pre[next.x][next.y].x = temp.x; pre[next.x][next.y].y = temp.y; pre[next.x][next.y].dir = next.dir; if( ((next.x == end.x - 1) &&(next.y == end.y)) || ((next.x == end.x + 1) &&(next.y == end.y)) ) { pre[end.x][end.y].x = next.x; pre[end.x][end.y].y = next.y; pre[end.x][end.y].dir = 's'; flag = 1; break; } else if( ((next.x == end.x) &&(next.y == end.y - 1)) ||((next.x == end.x) &&(next.y == end.y + 1)) ) { pre[end.x][end.y].x = next.x; pre[end.x][end.y].y = next.y; pre[end.x][end.y].dir = 'h'; flag = 1; break; } } } if(flag) break; } } int main() { int i, j, k, p, q, count = 1; char direction[105]; memset(direction,'\0',sizeof(direction)); memset(map,0,sizeof(map)); memset(visit, 0, sizeof(visit)); scanf("%d %d", &m, &n); for(i = 0; i < m; i++) for(j = 0; j < n; j++) scanf("%d", &map[i][j]); scanf("%d %d",&sta.x, &sta.y); scanf("%d %d",&end.x, &end.y); bfs(); k = 0; if(flag) { i = end.x; j = end.y; while((pre[i][j].x != sta.x) && (pre[i][j].y != sta.y)) { direction[k++] = pre[i][j].dir; p = pre[i][j].x; q = pre[i][j].y; i = p; j = q; } for(i = 0; direction[i + 1] != '\0'; i++) { if(direction[i + 1] != direction[i]) count++; } if(count <= 3) printf("TRUE\n"); else printf("FALSE\n"); } else printf("FALSE\n"); return 0; } 问答

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览