BFS: 刷题专用

# BFS 刷题

看了一天的图方面的东西,这个东西,真的是,思路都懂,但是实现起来,哈哈哈哈哈哈哈,一直处于懵逼的状态,所以就找点题刷吧,加强理解与应用,突然有点理解高中的应试教育了。

POJ 3984

题目描述

它表示一个迷宫,其中的1表示墙壁,0表示可以走的路,只能横着走或竖着走,不能斜着走,要求编程序找出从左上角到右下角的最短路线。
题目链接:http://poj.org/problem?id=3984

这个题,其实就是一个基本的BFS搜索,找最短路线,对于BFS。从出发点开始,第一次遍历到终点时过的那条路径就是最短的路径。因为这条路径没有多绕一个不相关节点啊,所以它是最短的。但是这个题并不是简单的去求这条路有多长,他得求出路径,所以需要存储遍历时候的路径的顺序。

#include <stdio.h>
#include <stdbool.h>

struct Node{
    int x, y;
    int pre;
    int level;
};
int pos[4][2] = {{0,1},{0,-1},{1,0},{-1,0}}; //右,左,上,下
int main() {
    int i, j;
    int num[5][5] = {0};
    struct Node queue[30]; //建立队列
    struct Node result[30];
    int rear = 0;
    int head = 0;
    int visit[5][5] = {0};
    struct Node root;
    int cnt = 0;
    bool flag = false;
   // printf("你好");
    for (i = 0; i < 5; i++)
        for (j = 0; j < 5; j++) {
            scanf("%d", &num[i][j]);
        }
    queue[rear].x = 0; //第一个元素入队
    queue[rear].y = 0;
    queue[rear].level = 0;
    queue[rear].pre = -1;
    result[cnt++] = queue[rear];
    rear += 1;
    visit[0][0] = 1;
    int parent;
    while(rear != head){ //队不为空;
        root = queue[head++]; //出队;
        i = -1;
        parent = root.level; //获取层次
        if(root.x == 4 && root.y == 4){
            flag = true;
            break;
        }
        while (i < 3){//四个方向
            i += 1;
            int x = root.x + pos[i][0];
            int y = root.y + pos[i][1];
            if(x < 0 || y< 0 || y > 4 || x > 4 || visit[x][y] == 1 || num[x][y] == 1)
                continue;
            //printf("%d ", rear);
            queue[rear].x = x;
            queue[rear].y = y;
            queue[rear].pre = parent;
            queue[rear].level = cnt; //他的层次;
            result[cnt++] = queue[rear];
            visit[x][y] = 1;
            rear += 1;
        }
    }
    cnt = parent;
    i = 0;
    while(cnt != -1){
        queue[i++] = result[cnt];
        cnt = result[cnt].pre;
    }
    for(i = i - 1; i > 0; i--)
     printf("(%d, %d)\n",queue[i].x,queue[i].y);
     printf("(4, 4)");

}

原文地址:https://www.cnblogs.com/xmxj0707/p/9678309.html

时间: 2024-08-27 05:03:34

BFS: 刷题专用的相关文章

7、8月刷题总结

准备开学了囧,7.8月刷题记录,以后好来复习,并且还要好好总结! 数据结构: splay: [BZOJ]1503: [NOI2004]郁闷的出纳员(Splay) [BZOJ]1269: [AHOI2006]文本编辑器editor(Splay) [BZOJ]1507: [NOI2003]Editor(Splay) treap: [BZOJ]1862: [Zjoi2006]GameZ游戏排名系统 & 1056: [HAOI2008]排名系统(treap+非常小心) [BZOJ]3224: Tyvj

刷题方法论

[转自一亩三分田]谈谈面试官在面试coding题目时的考察终点与心理活动 本人简介: 曾经微软dev, 35+, 10年经验, 有FLG offer.  去年加入一个start up 公司, 最近前景不明, 在犹豫要不要去个稳定点的大公司.  我从sde开始面试其他人, 到现在估计面试过100+人次的面试和debrief. 我面过coding, problem solving, design, behavior.  本帖子只谈论纯粹coding, 视情况讨论要不要再开帖子讨论其他方面. 本文涉及

用js刷题的一些坑

leecode可以用js刷题了,我大js越来越被认可了是吧.但是刷题中会因为忽略js的一些特性掉入坑里.我这里总结一下我掉过的坑. 坑1:js中数组对象是引用对象 js中除了object还有数组对象也是引用对象,这点常常被忽视,所以在递归的时候传递数组要用arr.slice(0)这样复制一个一样的新数组,不然会出现你传入的数组会被同级的递归改变,结果就不对了. 所以只要数组复制的地方最好都要这么写,除非你真的想引用.而且注意是slice不是splice这两个方法差别很大,你如果用splice(0

LeetCode刷题之一:寻找只出现一次的数字

投简历的时候看到了个刷题网站,http://www.nowcoder.com/527604,就做了一套题,现记录下来. 题目为: Given an array of integers, every element appears twice except for one. Find that single one. Note: Your algorithm should have a linear runtime complexity. Could you implement it withou

【leetcode刷题笔记】Sum Root to Leaf Numbers

Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number. An example is the root-to-leaf path 1->2->3 which represents the number 123. Find the total sum of all root-to-leaf numbers. For example, 1 / 2 3 T

BZOJ第一页刷题计划

BZOJ第一页刷题计划 已完成:1 / 100 BZOJ1000:A+B

刷题记录

刷题啦,刷题啦,咱也刷算法题. 先从牛客网的JS方面刷起,接着刷数据结构和算法,然后去刷leetcode,这儿记载自己从出错的地方. 1.题目描述 移除数组 arr 中的所有值与 item 相等的元素,直接在给定的 arr 数组上进行操作,并将结果返回 . 没有认真思考,写下了如下的答案 function removeWithoutCopy(arr, item) { for(i = 0; i < arr.length; i++) { if( arr[i] === item) { arr.spli

停课刷题总结-给自己一点鼓励吧

嗯,我已经停了四五天课在家刷BZOJ准备复赛了,感觉压力好大.但是,实际上感觉效率并不高,每天就是7-8题的样子,而且并不是每题都有质量.而且这几天刷下来,我貌似因为刷了太多水题的关系,打字写题的速度变慢了,有一点悠闲没有紧迫感了,要赶快把这个习惯给改掉!今天去学校做题被虐了,竟然一个简单的Hash没有调对[虽然我现在还是不知道为什么会死循环QAQ.]感觉吧,可能因为刷题有点不在状态了.[其实也因为刷题的间隙玩了几盘LOL,游戏这东西QAQ]被虐了,感觉很不爽,有点难受,毕竟我付出了那么多努力,

leetcode 刷题之路 63 Binary Tree Zigzag Level Order Traversal

Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from left to right, then right to left for the next level and alternate between). For example: Given binary tree {3,9,20,#,#,15,7}, 3 / 9 20 / 15 7 return its zig