2019-08-03 课堂总结

2019-08-03  本文已影响0人  saploser

BFS 广(宽)度优先搜索

1. luogu P1135 奇怪的电梯

主体思路:
#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 ;
}
#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马的遍历 (没过)

思路:
上一篇 下一篇

猜你喜欢

热点阅读