首页 > 其他分享 >AOJ0558(cheese, BFS)

AOJ0558(cheese, BFS)

时间:2023-08-23 10:05:11浏览次数:32  
标签:cheese temp int first BFS step AOJ0558 include SIZE


typical BFS usage:
first use 2 array, one for record map, the other for shortest path/flag visit.

BFS feature:
the shortest path from one to the other, just one point!!!!(单源路)

//#define LOCAL

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

#define SIZE 1010

using namespace std;

char map[SIZE][SIZE];
int dir[][2] = {0, 1, 0, -1, 1, 0, -1, 0};

int bfs(int &x, int &y, int h, int w, int target){
    int step[SIZE][SIZE];
    for(int i = 0; i < h; i++)
        fill(step[i], step[i] + w, -1);
    step[x][y] = 0;

    queue<pair<int, int> > q;
    q.push(make_pair(x, y));

    while(!q.empty()){
        pair<int, int> temp = q.front();
        q.pop();

        if(map[temp.first][temp.second] == target + '0'){
            x = temp.first;
            y = temp.second;
            return step[x][y];
        }

        for(int i = 0; i < 4; i++){
            int newx = temp.first + dir[i][0];
            int newy = temp.second + dir[i][1];

            if(0 <= newx && newx < h && 0 <= newy && newy < w
                && step[newx][newy] == -1 
                && map[newx][newy] != 'X'){
                q.push(make_pair(newx, newy));
                step[newx][newy] = step[temp.first][temp.second] + 1;
            }
        }
    }
}

int main(void){
#ifdef LOCAL
    freopen("data.in", "r", stdin);
#endif
    int n, w, h;
    int x, y;

    scanf("%d%d%d", &h, &w, &n);
    getchar();
    for(int i = 0; i < h; i++){
        for(int j = 0; j < w; j++){
            scanf("%c", &map[i][j]);

            if(map[i][j] == 'S'){
                x = i;
                y = j;
            }
        }
        getchar();
    }

    int result = 0;
    for(int i = 1; i <= n; i++)
        result += bfs(x, y, h, w, i);

    printf("%d\n", result);

    return 0;
}


标签:cheese,temp,int,first,BFS,step,AOJ0558,include,SIZE
From: https://blog.51cto.com/u_8999467/7199310

相关文章

  • perlapp BFS格式分析
    perlappBFS格式分析1、加载资源中加密的BFSLoadResource_BFS_406670LPVOID*__fastcallLoadResource_BFS_406670(char*Source){//[COLLAPSEDLOCALDECLARATIONS.PRESSKEYPADCTRL-"+"TOEXPAND]v2=(BFS**)malloc(0x28ui64);v3=v2;if(!v2)r......
  • bfs 双向宽搜
     1、迷宫问题,找最短路:可以同时从起点和终点进行bfs,两个方向搜索的新节点分别存在不同的队列中的,若新节点在对面的状态集合中出现过,说明相遇了。2、很多bfs问题,都可以用双向宽搜,提高效率。3、分油问题,能不能用双向宽搜呢?3个无刻度的油瓶的容量是1073,其中分别有油10,0,0......
  • LeetCode 周赛上分之旅 # 37 多源 BFS 与连通性问题
    ⭐️本文已收录到AndroidFamily,技术和职场问题,请关注公众号[彭旭锐]和BaguTreePro知识星球提问。学习数据结构与算法的关键在于掌握问题背后的算法思维框架,你的思考越抽象,它能覆盖的问题域就越广,理解难度也更复杂。在这个专栏里,小彭与你分享每场LeetCode周赛的解题报告,一......
  • 搜索(DFS和BFS)
    深搜是我最早学的算法,当然现在还没有信手拈来就是了。。。为了更好地学树和图,只能回来刷搜索了。。。。。我已经搜了一天了啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊(疯癫)首先是今天去刷的八皇后问题,特别经典的搜索题,我记得我有一天深夜学算法就看了这个八皇后问题,其实深搜和广搜......
  • bfs专题
    //最少点路径#include<bits/stdc++.h>usingnamespacestd;intn,s,t,a[11][11],xwz[11];structnode{intwz;stringlj;};intvis[11];stringxlj[11];queue<node>q;voidbfs(intx){q.push({x,""});vis[x]=1;while(!q......
  • 细胞(bfs)
    #include<bits/stdc++.h>usingnamespacestd;intdx[4]={1,-1,0,0},dy[4]={0,0,1,-1};intbz[100][100]={1},num=0;chars[100][100],ch;boolvis[100][100];intm,n;voidbfs(intp,intq){intx,y,t,w,i;inth[1000][3];num++;bz[p][q]=0......
  • 搜索(DFS/BFS)
    广度优先搜索(BFS)基本要点: -利用队列(先进先出) -一层一层搜索 -适合于连通块的搜索 -任何的BFS都可以转化为对树的广搜基本流程: -选择搜索的起点,起点入队,起点标记为已访问 -队列非空时,循环出队,每次出队将与出队元素连通的且未访问过的元素依次入队,并......
  • BFS和DFS基础
    BFS和DFS基础搜索简介搜索是"暴力法"算法的具体实现,是一种吧所有可能的情况都罗列出来,然后逐一检查,从中找到答案的方法。一般步骤找到所有可能的数据,并且永数据结构表示和存储。优化:尽量多的排除不符合条件的数据,以减少搜索空间。用某个算法快速检索这些数据。搜索算法的......
  • abc088 <bfs 最短路>
    题目D-GridRepainting思路bfs找到从起点到终点的最短路,+1(起点),即为至少留下的白色块的个数则答案=总白色块数-(最短路+1)代码Code//https://atcoder.jp/contests/abc088/tasks/abc088_d#include<iostream>#include<algorithm>#include<vector>#incl......
  • luogu1_dfsbfs
    普及练习场知识点汇总:DFS、BFS、☆杨辉三角P1118USACO06FEB数字三角形☆求解的个数用深搜,求最优解用广搜。DFSP1219八皇后弱智一样的我,还建立NxN的矩阵来模拟。结果呢,检查(check)时要遍历整个棋盘,最终导致只能过部分。根本不用二维矩阵。dfs(i),因为传进来的i是行号,......