bfs最短路径



http://paste.ubuntu.com/14663693/#include

<iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
 
using namespace std;
 
char mp[55][55];
int dis[4][2] = { {-1,0},{1,0},{0,1},{0,-1}};
int n,m;
struct I
{
    int x,y;
}s,e;
 
struct P
{
    int w;
    int num;
}res[55][55];
 
bool Isleagal( I a )
{
    if( a.x < 1 || a.x > n || a.y < 1 || a.y > m || mp[a.x][a.y] == '#')
        return false;
    return true;
}
 
void bfs()
{
    for( int i = 1; i <= n; i++ )
        for( int j = 1; j <= m; j++ )
            {
                res[i][j].w = 99999;
                res[i][j].num = 0;
            }
    queue<I> q;
    I now,next;
    q.push( s );
    res[s.x][s.y].w = 0;
    res[s.x][s.y].num = 1;
    while( !q.empty() )
    {
        now = q.front();
        q.pop();
        if( now.x == e.x && now.y == e.y )
            continue;
        for( int i = 0; i < 4; i++ )
        {
            next.x = now.x + dis[i][0];
            next.y = now.y + dis[i][1];
            if( Isleagal(next) )
            {
                if( res[now.x][now.y].w + 1 < res[next.x][next.y].w)
                    {
                        res[next.x][next.y].w = res[now.x][now.y].w + 1;
                        res[next.x][next.y].num = (res[now.x][now.y].num) % 10086;
                        q.push(next);
                    }
                else if( res[now.x][now.y].w + 1 == res[next.x][next.y].w )
                    {
                        res[next.x][next.y].num = (res[now.x][now.y].num + res[next.x][next.y].num)%10086;
                    }
            }
        }
    }                    
}
 
int main()
{
    int t;
    scanf("%d",&t);
    while( t-- )
    {
        scanf("%d %d",&n,&m);
        for( int i = 1; i <= n; i++ )
            {
                getchar();
                for( int j = 1; j <= m; j++ )
                {
                    scanf("%c",&mp[i][j]);
                    if( mp[i][j] == 'S')
                        {
                            s.x = i;
                            s.y = j;
                        }
                    if( mp[i][j] == 'N')
                        {
                            e.x = i;
                            e.y = j;
                        }
                }
            }
        bfs();
        printf("%d\n",res[e.x][e.y].num);
    }
    return 0;
}
/**************************************************************
    Problem: 1080
    User: 105032014065
    Language: C++
    Result: Accepted
    Time:0 ms
    Memory:1640 kb
****************************************************************/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
下面是BFS最短路径算法的Java实现,假设图的节点编号从0开始: ```java import java.util.*; public class ShortestPathBFS { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int n = scanner.nextInt(); //节点个数 int m = scanner.nextInt(); //边的个数 int[][] edges = new int[m][2]; //存储边信息 for (int i = 0; i < m; i++) { edges[i][0] = scanner.nextInt(); edges[i][1] = scanner.nextInt(); } int start = scanner.nextInt(); //起点 int end = scanner.nextInt(); //终点 int[] dist = shortestPathBFS(n, edges, start); //计算最短路径 System.out.println(dist[end]); //输出最短路径长度 } //BFS计算最短路径 public static int[] shortestPathBFS(int n, int[][] edges, int start) { List<List<Integer>> graph = new ArrayList<>(); //邻接表存储图 for (int i = 0; i < n; i++) { graph.add(new ArrayList<>()); } for (int[] edge : edges) { graph.get(edge[0]).add(edge[1]); graph.get(edge[1]).add(edge[0]); } int[] dist = new int[n]; //记录起点到各节点的最短距离 Arrays.fill(dist, -1); dist[start] = 0; Queue<Integer> queue = new LinkedList<>(); //BFS队列 queue.offer(start); while (!queue.isEmpty()) { int curr = queue.poll(); for (int next : graph.get(curr)) { if (dist[next] == -1) { //如果next节点没有被访问过 dist[next] = dist[curr] + 1; queue.offer(next); } } } return dist; } } ``` 在这个例子中,我们先输入节点个数n、边的个数m、m条边的信息、起点start和终点end。然后调用shortestPathBFS函数计算从起点到各个节点的最短距离,并输出终点的最短距离。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值