【BFS】 计蒜客T1214

 计蒜客T1214

  • 就是一个带状态的简单搜索。
  • 我真的吐了,比赛的时候MLE了n发,结果就差一个inque数组我就过了……因为没有判断相同点的相同状态有没有在队列中,所以爆了。我吐了我吐了我吐了我真的吐了……qaq
  • 总而言之我太菜了。这个题疯狂MLE。想到是队列出了问题。但是愣死就是没想到再加一个数组。太久没做搜索了。
  • 队长说的没错,连大一的都不如。
#include <iostream>
#include <cstdio>
#include <cmath>
#include <string>
#include <cstring>
#include <algorithm>
#include <limits>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#define INF 0x3f3f3f3f
#define lowbit(x) x & (-x)

#define MID (l + r ) >> 1
#define lsn rt << 1
#define rsn rt << 1 | 1
#define Lson lsn, l, mid
#define Rson rsn, mid + 1, r
#define QL Lson, ql, qr
#define QR Rson, ql, qr
#define eps  1e-6

using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int maxN = 200;
inline int read()
{
    int x = 0, f = 1; char c = getchar();
    while(c < '0' || c > '9') { if(c == '-') f = -f; c = getchar(); }
    while(c >= '0' && c <= '9') { x = x * 10 + c - '0'; c = getchar(); }
    return x * f;
}
int m, n, t;
char mp[maxN][maxN];
int sx, sy;
const int dir[4][2] = {
        1, 0,
        0, 1,
        -1, 0,
        0, -1
};
struct node{
    int x, y, dis, state;
    node() {}
    node(int a, int b, int c, int d): x(a), y(b), dis(c), state(d) {}
};
bool vis[maxN][maxN][10], inque[maxN][maxN][10];
bool InMap(int x, int y)
{
    return x >= 0 && x < m && y >= 0 && y < n;
}
int bfs(int x, int y)
{
    memset(vis, 0, sizeof(vis));
    queue<node>q;
    q.push(node(x, y, 0,  t)); inque[x][y][t] = 1;
    while(!q.empty())
    {
        node tmp = q.front(); q.pop();
        for(int i = 0; i < 4; i ++ )
        {
            int xx = tmp.x + dir[i][0];
            int yy = tmp.y + dir[i][1];
            if(!InMap(xx, yy) || (xx == sx && yy == sy))
                continue;
            else if(mp[xx][yy] == '#' && tmp.state == 0)
                continue;
            else if(mp[xx][yy] == '#' && tmp.state && !vis[xx][yy][tmp.state - 1] && !inque[xx][yy][tmp.state - 1])
            {
                q.push(node(xx, yy, tmp.dis + 1, tmp.state - 1));
                inque[xx][yy][tmp.state - 1] = 1;
            }
            else if(mp[xx][yy] == '*' && !vis[xx][yy][tmp.state] && !inque[xx][yy][tmp.state])
            {
                q.push(node(xx, yy, tmp.dis + 1, tmp.state));
                inque[xx][yy][tmp.state] = 1;
            }
            else if(mp[xx][yy] == '+')
                return tmp.dis + 1;
        }
        vis[tmp.x][tmp.y][tmp.state] = 1;
    }
    return -1;
}
int main()
{
    m = read(); n = read(); t = read();
    for(int i = 0; i < m; i ++ )
    {
        for(int j = 0; j < n; j ++ )
        {
            scanf("%c", &mp[i][j]);
            if(mp[i][j] == '@')
            {
                sx = i;
                sy = j;
            }
        }
        getchar();
    }
    printf("%d\n", bfs(sx, sy));
    return 0;
}

 

我也真的有毒,纠缠烂打?没有自知之明,自找没趣,自作多情……原谅我词穷了。反正就是一个很不堪的人。

我从小就对自己说:xlq,一定要活成自己想要的模样。

一年一年过去,只有在tear中才想起自己说的话,有的时候就算想到也还是对某些人某些事某些东西有一种执念,然后放任自己浪荡,放任自己颓废。我希望自己happy,也希望大家happy。希望大家心想事成。至于我,我心想成不了。不喜欢随遇而安,不相信命中注定,不屈服于命运,那个在梦里si掉的xlq就真的si了。最强悍的第三者不是别人,而是命运。JJ说的,但是你有你的XiaoZhang,我也有我的Arrogant.  至于命运,等命运真的快要毁掉世界的时候再说。推荐一首歌——嚣张。挺好听的,起码我觉得是这样。

2020年加油。没有tear,只有laughter. 这是我唯一的愿望。

再加一个:成为 可 爱 的人儿。【并非字面意思】

 

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
### 回答1: 以下是 Python 代码实现 BFS 计算最短路径: ```python from collections import deque def bfs(graph, start, end): # 使用队列来进行 BFS queue = deque() queue.append(start) # 用于记录已经访问过的节点 visited = set() visited.add(start) while queue: # 取出队列中的第一个节点 node = queue.popleft() # 如果该节点是目标节点,结束搜索 if node == end: return True # 遍历相邻节点,并将其加入队列中 for neighbor in graph[node]: if neighbor not in visited: queue.append(neighbor) # 标记已经访问过的节点 visited.add(neighbor) # 没有找到目标节点,返回 False return False # 构建图 graph = { 'A': ['B', 'C'], 'B': ['A', 'D', 'E'], 'C': ['A', 'F'], 'D': ['B'], 'E': ['B', 'F'], 'F': ['C', 'E'] } # 测试 print(bfs(graph, 'A', 'F')) # 输出 True print(bfs(graph, 'D', 'C')) # 输出 False ``` 注意,在上述代码中,我们只是判断了起点能否到达终点,如果需要计算最短路径,需要将 BFS 程序进行适当的修改。比如,我们可以使用一个字典来记录每个节点的前一个节点,然后从终点往回遍历这个字典,就可以得到最短路径了。 ### 回答2: 广度优先搜索(BFS)是一种图遍历算法,用于在无权图中计算最短路径。以下是一个使用Python实现BFS算法来计算最短路径的简单示例: ```python from collections import deque def bfs(graph, start, end): # 创建一个队列,用于存储待访问的节点 queue = deque() # 创建一个集合,用于记录已访问的节点 visited = set() # 存储每个节点的父节点,用于最后回溯路径 parent = {} # 将起始节点加入队列和已访问集合 queue.append(start) visited.add(start) while queue: node = queue.popleft() # 判断是否找到目标节点 if node == end: break # 遍历当前节点的邻居节点 for neighbor in graph[node]: if neighbor not in visited: queue.append(neighbor) visited.add(neighbor) # 记录邻居节点的父节点 parent[neighbor] = node # 回溯最短路径 shortest_path = [] current_node = end while current_node != start: shortest_path.append(current_node) current_node = parent[current_node] shortest_path.append(start) # 反转路径列表 shortest_path = shortest_path[::-1] return shortest_path # 定义一个无向图,使用字典表示邻接表 graph = { 'A': ['B', 'C'], 'B': ['A', 'D'], 'C': ['A', 'E'], 'D': ['B', 'E', 'F'], 'E': ['C', 'D', 'F'], 'F': ['D', 'E'] } start_node = 'A' end_node = 'F' shortest_path = bfs(graph, start_node, end_node) print("最短路径:", shortest_path) ``` 上述代码通过字典表示的邻接表来表示一个无向图。bfs函数接受三个参数:图graph、起始节点start和目标节点end。函数使用队列来实现BFS,首先将起始节点加入队列和已访问集合,然后对队列中的节点进行遍历,依次访问其邻居节点。当找到目标节点时,结束遍历。然后通过回溯父节点的方式,找到最短路径。最后将路径列表进行反转,即可得到从起始节点到目标节点的最短路径。最后将最短路径打印出来。 运行上述代码,将输出以下结果: 最短路径: ['A', 'C', 'E', 'F'] ### 回答3: BFS(Breadth First Search)即广度优先搜索算法,在计算最短路径中应用较广。下面将使用Python编程语言来说明BFS计算最短路径的过程。 首先,我们需要创建一个图来表示路径的关系。可以用字典来表示,字典的键表示节点的名字,值表示与该节点相连的其他节点的列表。例如,如果有两个节点A和B相连,我们可以使用字典`graph = {'A': ['B'], 'B': ['A']}`来表示。 接下来,我们需要定义一个函数来计算最短路径。函数使用一个队列来存储待处理的节点。开始时,我们将起始节点放入队列中,并创建一个集合来存储已经访问过的节点。然后,我们开始一个循环,直到队列为空。在每次循环中,我们从队列中取出一个节点,并检查该节点是否为目标节点。如果是,我们找到了最短路径,直接返回。否则,我们将该节点标记为已访问,并将与之相连的尚未访问过的节点加入队列。 最后,如果循环结束时仍然没有找到目标节点,则表示不存在从起始节点到目标节点的路径。 下面是一个使用BFS算法计算最短路径的示例代码: ``` def bfs(graph, start, end): queue = [[start]] visited = set() while queue: path = queue.pop(0) node = path[-1] if node == end: return path if node not in visited: neighbors = graph[node] for neighbor in neighbors: new_path = list(path) new_path.append(neighbor) queue.append(new_path) visited.add(node) return None graph = {'A': ['B', 'C'], 'B': ['A', 'D', 'E'], 'C': ['A', 'F'], 'D': ['B'], 'E': ['B', 'F'], 'F': ['C', 'E']} start_node = 'A' end_node = 'F' shortest_path = bfs(graph, start_node, end_node) if shortest_path: print('最短路径为:', shortest_path) else: print('找不到最短路径') ``` 在上述示例中,我们定义了一个名为`bfs`的函数,它使用了一个`graph`字典来表示图的结构。函数将起始节点和目标节点作为参数传入,并返回最短路径。 希望这段代码能够帮助你理解如何使用BFS算法来计算最短路径。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值