K

这是一道赤裸裸的广搜+路径问题。。。。直接做吧。。。。。
////////////////////////////////
#include<queue>
#include<stdio.h>
#include<string.h>
using namespace std;

#define maxn 10
const int oo = 0xfffffff;

struct node{int x, y;}from[maxn][maxn];
int dir[4][2] = { {0,1},{0,-1},{1,0},{-1,0} };
int G[maxn][maxn], N=5;
node s={0,0}, e={4,4};

void DfsPath(int x, int y)//递归输出路径
{
    if(x == 0 && y == 0)
    {
        printf("(0, 0) ");
        return ;
    }
    DfsPath(from[x][y].x, from[x][y].y);

    printf("(%d, %d) ", x, y);
}
void BfsPath()
{
    queue<node> Q;
    Q.push(s);
    G[0][0] = 1;

    while(Q.size())
    {
        s = Q.front();Q.pop();

        if(s.x==e.x && s.y == e.y)
            return ;
        for(int i=0; i<4; i++)
        {
            node q = s;
            q.x += dir[i][0], q.y += dir[i][1];

            if(q.x>=0&&q.x<N && q.y>=0&&q.y<N && G[q.x][q.y]==0)
            {
                G[q.x][q.y] = 1;
                from[q.x][q.y] = s;
                Q.push(q);
            }
        }
    }
}

int main()
{
    int i, j;

    for(i=0; i<N; i++)
    for(j=0; j<N; j++)
        scanf("%d", &G[i][j]);

    BfsPath();
    DfsPath(N-1, N-1);

    return 0;

}; 

原文地址:https://www.cnblogs.com/liuxin13/p/4650284.html