搜索(深度与广度优先)

/*
搜索  : 农夫约翰找牛的问题
bfs
*/

#include<queue>
#include<cstdio>
#include<iostream>
using namespace std;

const int MAXN =10001;

struct  Status {
 int n ,t ;
 Status ( int n , int t) : n(n) ,t(t){}
};
bool visit[MAXN];
int  BFS( int  n , int  k){
queue<Status> myQueue ;
myQueue.push(Status(n,0));
// 一定记得将 状态设置为 visit[true]了
visit[n]=true;
while(!myQueue.empty()){
    Status current= myQueue.top();
    myQueue.pop();
    // 使用 queue的数据结构
    if(current.n==k){
     return   current.t;
    }
    else {
        //产生的新的
        Status next(current.n, current.t+1);
        for( int  i=0;i<3;i++){
            if(i==0){
                //是0的话
                next.n++;
            }
            else if(i==1) {
                next.n--;
            }
            else {
                next.n=next.n*2;
            }
          myQueue.push(next);
          visit[next.n]=true;  
        }
    }
}

}

int main(){
int  n ,k;
cin>>n>>k;

cout<<BFS(n,k);
 
}

这个题目比较简单,使用了队列的数据结构

下面在看一个题目, 也是比较简单的。 题目的大意是求一个的数的倍数,另外这个数只是由0 或者1组成的。

/*
find the multiple
找倍数
*/
#include<queue>
#include<cstdio>
#include<iostream>
using namespace std;

int bfs( int n )  // bfs 广度优先遍历
{
    queue<long long > myQueue;
    myQueue.push(1);
    long long current ;
    while(!myQueue.empty())
    {
        // 不为空的时候就进行的东西
        current=myQueue.front();
        if(current%n==0)
        {

            return  current;
        }
        else
        {
            myQueue.pop();
            myQueue.push(current*10);
            myQueue.push(current*10+1);

        }

    }

}


int main()
{
    int n ; //def 存储 n
while(scanf("%d",&n)!=EOF){
    bfs(n);

}
}

下面接着有一个例子:

/*
DFS : 判断是否可以将木棍围成正方形 !
*/
#include<queue>
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#define MAXN 100
using namespace std;
 int m ;
int  stick_len[MAXN];
int  _size; //边长
bool visit[MAXN]; // 是否访问的标记
bool DFS( int sum, int  number, int position ) //分别表示:current的边的长度. 围成的边,数组中的位置
{

    if(number==3)
        return true;
//  需要将 stick_len降序排列
    visit[position]=true;
    int sample;
    for( int  i =position; i<m; i++)
    {
        if(sum+stick_len[i]>_size||visit[i]||sample==stick_len[i])
        {
            continue;
        }
        visit[i]=true;
        if(sum+stick_len[i]==_size)
        {
            if( DFS(0,number+1,0))
            {
                return true;
            }
            else
            {
                sample=stick_len[i];
            }

        }
        else
        {
            if(DFS(sum+stick_len[position],number,position+1))
            {
                return true;
            }
            else
            {
                sample=stick_len[i];
            }
        }
        visit[i]=false;
    }
    return false;
}
bool Compare( int x, int y)
{
//升序比较
    return x>y;
}
int main()
{
    int len=0;
    cin>>m;
    for(int i=0; i<m; i++)
    {
        cin>>stick_len[i];
        len=len+stick_len[i];
    }
    memset(visit,false, sizeof(visit));
    if(len%4!=0)
    {
        cout<<"No!";
    }
    else
    {
        _size=len/4;
    }
    sort(stick_len,stick_len+m,Compare);
    if(stick_len[0]>_size)
    {
        cout<<"No!!"<<endl;
    }   
    if(DFS(0,0,0)) //不要将 visit[0]置为true !! 
    {
        cout<<"ok"<<endl;
    }
    else
    {
        cout<<"No!!"<<endl;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值