zoukankan      html  css  js  c++  java
  • 广度优先(BFS) ------- 模板1:-----模板2:--------模板3:

    //使用数组queue[ ]存放结点队列
    void   BFS( ) 
    {   head=0; tail=1;  queue[head]=首结点的值;
         while (head<tail) //队列不空
         {    temp=tail;
              for (k=head; k<=tail; k++)  //对当前层扩展
              {      if  ( 到达目的状态 )    {      输出结果;   return;   }
                     for (i=1; i<=m; i++) //每个结点的m种扩展可能
                          if (可以扩展) 
                          {      处理每种可能情况; 
                                  queue[temp++]=扩展出的结点值;
                          } 
             }
             head=tail;   tail=temp;
    }
    View Code

    //使用数组queue[ ]存放结点队列
    void   BFS( )
    {   head=0; tail=1;  queue[head]=首结点的值;
         while (head<tail) //队列不空
         {    temp=tail;
              for (k=head; k<=tail; k++)  //对当前层扩展
              {      if  ( 到达目的状态 )    {      输出结果;   return;   }
                     for (i=1; i<=m; i++) //每个结点的m种扩展可能
                          if (可以扩展)
                          {      处理每种可能情况;
                                  queue[temp++]=扩展出的结点值;
                          }
             }
             head=tail;   tail=temp;
    }

    //使用数组queue[ ]存放结点队列
    void   BFS( ) 
    {    head=0; tail=1;  
         queue[head]=首结点的值;
         while (head<tail)   //队列不空
         {     if  ( 到达目的状态 )    {      输出结果;   break;   }
                head++;
               for (i=1; i<=m; i++) //结点head的m种扩展可能
                          if (可以扩展) 
                          {      处理每种可能情况; 
                                  queue[tail++]=扩展出的结点值;
                          } 
             }
    }
    View Code

    //使用数组queue[ ]存放结点队列
    void   BFS( )
    {    head=0; tail=1; 
         queue[head]=首结点的值;
         while (head<tail)   //队列不空
         {     if  ( 到达目的状态 )    {      输出结果;   break;   }
                head++;
               for (i=1; i<=m; i++) //结点head的m种扩展可能
                          if (可以扩展)
                          {      处理每种可能情况;
                                  queue[tail++]=扩展出的结点值;
                          }
             }
    }

    //使用STL中的队列
    void   BFS( ) 
    {   首结点入队列Q;
         while ( ! Q.empty() ) //队列不空
         {    temp=Q.front();    
              if  ( 到达目的状态 )     {      输出结果;   break;   } 
              Q.pop( );  
              for (i=1; i<=m; i++) //扩展结点temp的m种可能
                      if (可以扩展) 
                      {      处理每种可能情况; 
                             扩展出结点入队列;
                      } 
             }
    }
    View Code

    //使用STL中的队列
    void   BFS( )
    {   首结点入队列Q;
         while ( ! Q.empty() ) //队列不空
         {    temp=Q.front();   
              if  ( 到达目的状态 )     {      输出结果;   break;   }
              Q.pop( ); 
              for (i=1; i<=m; i++) //扩展结点temp的m种可能
                      if (可以扩展)
                      {      处理每种可能情况;
                             扩展出结点入队列;
                      }
             }
    }

  • 相关阅读:
    [Leetcode] Two Sum
    [Leetcode] 4Sum
    [Leetcode] Word Break
    [Leetcode] Evaluate Reverse Polish Notation
    [Leetcode] Distinct Subsequences
    [Leetcode] Triangle
    [Leetcode] Single Number II
    [Leetcode] Convert Sorted Array to Binary Search Tree
    一起手写吧!Promise!
    一起手写吧!sleep函数!
  • 原文地址:https://www.cnblogs.com/2014acm/p/3888869.html
Copyright © 2011-2022 走看看