美文网首页
2019-08-03 课堂总结

2019-08-03 课堂总结

作者: saploser | 来源:发表于2019-08-03 16:42 被阅读0次

BFS 广(宽)度优先搜索

1. luogu P1135 奇怪的电梯

主体思路:
  • dfs plus 剪枝 (在显然不可能是最优解时,不继续dfs)
    代码如下:
#include<iostream>
#include<algorithm>
#include<ctime>
#include<cstdlib>
using namespace std ;

int n , a , b , cmin = 1000000 , c ;
int num[210] ;
bool flag[210] ;
void dfs(int x , int y)
{
    
    if ( flag[x] == 1 ) return ;
    if ( x == b ) 
    {
        cmin = min(y , cmin) ;
        y = 0 ;
        return ;
    }
    if ( y >= cmin) return ;  //剪枝
    flag[x] = 1 ;
    if ( x > num[x] ) dfs(x - num[x] , y + 1) ;
    if ( x + num[x] <= n ) dfs( x + num[x] , y + 1) ;
    flag[x] = 0 ;
    return ; 
} 
int main()
{
    cin >> n >> a >> b ;
    for ( int i = 1 ; i <= n ; i++)
        cin >> num[i] ;
    dfs(a , 0) ; 
    if ( cmin == 1000000) cmin = -1 ;
    cout << cmin << endl ;
    return 0 ;
}
  • bfs的思路:通过一个队列以及结构体,记录到达的步数以及当前数字。
    bfs主体思路:
    每次先有序用上一次生出的节点生成子节点,再继续向下逐层重复以上操作即可得到最优解。
    代码:
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>


using namespace std ;

struct elevator
{
    int f , d ;
};




elevator floor ;
int n , s , t ;
int num[205] ;
bool flag[205] ;
queue<elevator> q ;



int bfs()
{
    q.push((elevator){s , 0}) ;
    while ( q.size())
    {
        floor = q.front() ;
        if ( floor.f == t) return floor.d ;
        q.pop() ;
        flag[floor.f] = true ;
        if ( floor.f + num[floor.f] <= n && !flag[floor.f + num[floor.f]])
        {
            q.push((elevator){floor.f + num[floor.f] , floor.d + 1}) ;
        } 
        if ( floor.f - num[floor.f] >= 1 && !flag[floor.f + num[floor.f]])
        {
            q.push((elevator){floor.f - num[floor.f] , floor.d + 1}) ;
        }
    }
    return -1 ;
}




int main()
{
    scanf("%d%d%d" , &n , &s , &t) ;
    for ( int i = 1 ; i <= n ; i++ )
        scanf("%d" , &num[i]) ;
    printf("%d\n" , bfs()) ;
}

洛谷P1443马的遍历 (没过)

思路:
  • bfs直接广搜即可
  • dfs感觉也可以,可写完后样例过不去,没查出那里错

相关文章

  • 2019-08-03 课堂总结

    BFS 广(宽)度优先搜索 1. luogu P1135 奇怪的电梯 主体思路: dfs plus 剪枝 (在显然...

  • Lan的ScalersTalk第四轮新概念朗读持续力训练Day

    练习材料: [Day 1772 2019-08-03] Lesson 28-3 Patients and doct...

  • 2019-08-05

    2019-08-03 毛雅亭 字数 552 · 阅读 14 2019-06-02 18:39 ...

  • 想《解题的步骤》

    2019-08-03,2020-02-01,2020-02-122020-06-01,2021-02-25 解题的...

  • forward函数实现

    Time: 2019-08-03视频地址:https://youtu.be/MasG7tZj-hw?list=PL...

  • 课堂总结

    这节课梁老师让我们老了一张测试卷,我基本上都是做对的。就是有一两题是不会的,然后就梁老师说过之后我就懂了

  • 课堂总结

    Http://www.imiker.com 1.Classify your handing issues 2. M...

  • 课堂总结

    在今天,我们又扮演了小老师的角色,开始讲自已组的题目。 时间慢慢地走远,可不知什么时候,老师让我朋友去讲7的倍数特...

  • 课堂总结

    这是第二节小丁老师的科学课,这次小丁老师让我们把纸折成八分份。把第一单元的八个标题抄上去想一想为什么要按照这样的顺...

  • 课堂总结

    老师教给我们不可不知道的8个知识点; .1,准备工作(微商的定位微商的形象.朋友圈的精装修.各种工具包准 2.如何...

网友评论

      本文标题:2019-08-03 课堂总结

      本文链接:https://www.haomeiwen.com/subject/ssgjdctx.html