【BFS】CODE[VS] 2059 逃出克隆岛(奴隶岛)

点击进入冲绳奴隶岛


魔兽系列题目第一弹

BFS典型例题,遇到传送门只走一次且将走每一个传送门的情况都搜一下,碰到终点直接退出输出结果即可

PS:最近超喜欢压行!我也不知道为什么

#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <queue>

const int maxn = 5010;

using namespace std;

int n,m,cc;
int tot; 
int sx,sy,ex,ey;
bool used[maxn][maxn];
char map[maxn][maxn];
int dx[5] = {0,0,1,0,-1};
int dy[5] = {0,1,0,-1,0};

struct node{
    int x,y,cost;
};

struct data{
    int x,y; 
}tp[maxn]; 

queue<node >q; 

inline bool check(int xx,int yy)
{
    if(xx <= 0||xx > n||yy <= 0||yy > m||used[xx][yy] == 1||map[xx][yy] == '#')return false;
    return true; 
}

inline void movein(node now)
{
    node ttt;
    for(int i = 1;i <= tot;i++)
    {
        if(tp[i].x != now.x||tp[i].y != now.y)
        {
            ttt.x = tp[i].x;
            ttt.y = tp[i].y;
            ttt.cost = now.cost;
            q.push(ttt);
            map[ttt.x][ttt.y] = '#';
        }
    }
} 

inline void bfs()
{
    node start;
    start.x = sx;
    start.y = sy;
    start.cost = 0;
    used[sx][sy] = 1;
    q.push(start);
    while(!q.empty())
    {
        node head = q.front();
        q.pop();
        for(int i = 1;i <= 4;i++)
        {
            node now;
            now.x = head.x + dx[i];
            now.y = head.y + dy[i];
            now.cost = head.cost;
            if(map[now.x][now.y] == 'C'){printf("%d\n",now.cost); return;}
            if(check(now.x,now.y) == true)
            {
                used[now.x][now.y] = 1;
                //printf("%d\n",now.cost);
                //cout<<head.cost<<endl;
                //cout<<map[now.x][now.y]<<endl;
                if(map[now.x][now.y] == '*'){now.cost = head.cost + cc; q.push(now); continue;}
                //cout<<now.x<<" "<<now.y<<" "<<now.cost<<endl; 
                if(map[now.x][now.y] == 'P'){movein(now); continue;}    
            }
        }
    }
printf("screw you!\n");
return;
} 

int main()
{
    scanf("%d%d%d",&n,&m,&cc);
    //cout<<cc<<endl;
    for(int i = 1; i <= n;i++)
        for(int j = 1;j <= m;j++)
        {
            cin>>map[i][j];
            if(map[i][j] == 'Y') {sx = i; sy = j;}  
            if(map[i][j] == 'C') {ex = i; ey = j;}
            if(map[i][j] == 'P') {tot++;tp[tot].x = i;tp[tot].y = j;}
        }
        bfs();
return 0;
}
在Java中,使用广度优先搜索(BFS)遍历图或树的数据结构通常涉及到队列数据结构。以下是一个简单的BFS遍历算法的Java代码示例,用于查找给定起点到图中的所有节点: ```java import java.util.*; class Graph { private int vertices; private LinkedList<Integer>[] adj; // 构造函数,初始化邻接表 public Graph(int v) { vertices = v; adj = new LinkedList[v]; for (int i = 0; i < v; ++i) adj[i] = new LinkedList(); } // 添加边的方法 void addEdge(int v, int w) { adj[v].add(w); } // 广度优先搜索 void bfs(int start) { boolean[] visited = new boolean[vertices]; // 标记数组,初始时都未访问 Queue<Integer> queue = new LinkedList<>(); // 使用LinkedList作为队列 // 将起始节点添加到队列和标记为已访问 visited[start] = true; queue.add(start); while (!queue.isEmpty()) { // 弹出队首元素 int node = queue.poll(); System.out.print(node + " "); // 遍历当前节点的所有邻居 Iterator<Integer> it = adj[node].listIterator(); while (it.hasNext()) { int neighbor = it.next(); // 如果邻居未被访问,则将其标记并添加到队列 if (!visited[neighbor]) { visited[neighbor] = true; queue.add(neighbor); } } } } } public class Main { public static void main(String[] args) { Graph g = new Graph(6); // 假设有一个包含6个节点的图 g.addEdge(0, 1); g.addEdge(0, 2); g.addEdge(1, 2); g.addEdge(2, 5); g.addEdge(2, 3); g.addEdge(3, 4); System.out.println("BFS traversal starting from vertex 0:"); g.bfs(0); // 从顶点0开始遍历 } } ``` 在这个例子中,我们首先创建了一个`Graph`类表示图,包含邻接列表`adj`。然后定义了`bfs`方法来进行BFS遍历。在`main`方法中,我们创建了一个图实例,并添加了一些边,最后调用`bfs`方法从指定的起始节点开始遍历。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值