• HDU1180+BFS


    bfs
    思路:三维标记状态 && 处理好 - | 和时刻的关系即可

      1 /*
      2 bfs
      3 思路:三维标记状态 && 处理好 - | 和时刻的关系即可
      4 */
      5 #include<algorithm>  
      6 #include<iostream>  
      7 #include<string.h>  
      8 #include<stdlib.h>  
      9 #include<stdio.h>  
     10 #include<math.h>  
     11 #include<queue>  
     12 #include<stack>  
     13 #include<map>  
     14 #include<set>  
     15 using namespace std;  
     16 typedef long long int64;  
     17 //typedef __int64 int64;  
     18 typedef pair<int64,int64> PII;  
     19 #define MP(a,b) make_pair((a),(b))   
     20 const int inf = 0x3f3f3f3f;  
     21 const double pi=acos(-1.0);  
     22 const int dx[]={1,-1,0,0};  
     23 const int dy[]={0,0,1,-1};  
     24 const double eps = 1e-8;  
     25 const int maxm = 1005;  
     26 const int maxn = 25;  
     27 
     28 struct Point {
     29     int x,y,t;
     30 };
     31 Point s,e;
     32 
     33 bool vis[ maxn ][ maxn ][ 2 ];
     34 char mat[ maxn ][ maxn ];
     35 queue<Point>q;
     36 
     37 int bfs( int n,int m ){
     38     memset( vis,false,sizeof( vis ) );
     39     while( !q.empty() )
     40         q.pop();
     41     Point cur = s;
     42     vis[ s.x ][ s.y ][ s.t%2 ] = true;
     43     q.push( cur );
     44     while( !q.empty() ){
     45         cur = q.front();
     46         q.pop();
     47         //printf("
    cur:[%d,%d] t = %d
    ",cur.x,cur.y,cur.t);
     48         if( cur.x==e.x && cur.y==e.y ){
     49             e.t = min( e.t,cur.t );
     50         }
     51         if( cur.t>=e.t ) continue;
     52         for( int i=0;i<4;i++ ){
     53             Point nxt;
     54             nxt.x = cur.x + dx[ i ];
     55             nxt.y = cur.y + dy[ i ];
     56             nxt.t = cur.t + 1;
     57             if( nxt.x<0||nxt.x>=n||nxt.y<0||nxt.y>=m ) continue;
     58             if( mat[ nxt.x ][ nxt.y ]=='*' ) continue;
     59             if( mat[ nxt.x ][ nxt.y ]=='.' ){
     60                 if( vis[ nxt.x ][ nxt.y ][ nxt.t%2 ]==true ) continue;
     61                 vis[ nxt.x ][ nxt.y ][ nxt.t%2 ] = true;
     62                 q.push( nxt );
     63                 //printf("0 nxt:[%d,%d] t = %d
    ",nxt.x,nxt.y,nxt.t);
     64                 continue;
     65             }//
     66             nxt.x = cur.x + dx[ i ];
     67             nxt.y = cur.y + dy[ i ];
     68             nxt.t = cur.t + 1;
     69             if(( mat[ nxt.x ][ nxt.y ]=='-'&&cur.t%2==0 )||( mat[ nxt.x ][ nxt.y ]=='|'&&cur.t%2==1 )){
     70                 if( cur.x==nxt.x&&nxt.y>cur.y ) nxt.y = cur.y + 2;
     71                 if( cur.x==nxt.x&&nxt.y<cur.y ) nxt.y = cur.y - 2;
     72                 if( nxt.x>=0&&nxt.x<n&&nxt.y>=0&&nxt.y<m&&mat[ nxt.x ][ nxt.y ]=='.' ){
     73                     if( vis[ nxt.x ][ nxt.y ][ nxt.t%2 ]==false ){
     74                         vis[ nxt.x ][ nxt.y ][ nxt.t%2 ] = true;
     75                         q.push( nxt );
     76                         //printf("1 nxt:[%d,%d] t = %d
    ",nxt.x,nxt.y,nxt.t);
     77                         continue;
     78                     }
     79                 }
     80             }//1 ‘-’ 上下无需等待时间
     81             nxt.x = cur.x + dx[ i ];
     82             nxt.y = cur.y + dy[ i ];
     83             nxt.t = cur.t + 1;
     84             /*恢复nxt很重要*/
     85             if(( mat[ nxt.x ][ nxt.y ]=='-'&&cur.t%2==1 )||( mat[ nxt.x ][ nxt.y ]=='|'&&cur.t%2==0 )){
     86                 nxt.t = cur.t + 2;
     87                 if( cur.x==nxt.x&&nxt.y>cur.y ) nxt.y = cur.y + 2;
     88                 if( cur.x==nxt.x&&nxt.y<cur.y ) nxt.y = cur.y - 2;
     89                 if( nxt.x>=0&&nxt.x<n&&nxt.y>=0&&nxt.y<m&&mat[ nxt.x ][ nxt.y ]=='.' ){
     90                     if( vis[ nxt.x ][ nxt.y ][ nxt.t%2 ]==false ){
     91                         vis[ nxt.x ][ nxt.y ][ nxt.t%2 ] = true;
     92                         q.push( nxt );
     93                         //printf("2 nxt:[%d,%d] t = %d
    ",nxt.x,nxt.y,nxt.t);
     94                         continue;
     95                     }
     96                 }
     97             }//2 ‘-’ 上下需要等待时间
     98             nxt.x = cur.x + dx[ i ];
     99             nxt.y = cur.y + dy[ i ];
    100             nxt.t = cur.t + 1;
    101             /*恢复nxt很重要*/
    102             if(( mat[ nxt.x ][ nxt.y ]=='|'&&cur.t%2==0 )||( mat[ nxt.x ][ nxt.y ]=='-'&&cur.t%2==1 )){
    103                 if( cur.y==nxt.y&&nxt.x>cur.x ) nxt.x = cur.x + 2;
    104                 if( cur.y==nxt.y&&nxt.x<cur.x ) nxt.x = cur.x - 2;
    105                 if( nxt.x>=0&&nxt.x<n&&nxt.y>=0&&nxt.y<m&&mat[ nxt.x ][ nxt.y ]=='.' ){
    106                     if( vis[ nxt.x ][ nxt.y ][ nxt.t%2 ]==false ){
    107                         vis[ nxt.x ][ nxt.y ][ nxt.t%2 ] = true;
    108                         q.push( nxt );
    109                         //printf("3 nxt:[%d,%d] t = %d
    ",nxt.x,nxt.y,nxt.t);
    110                         continue;
    111                     }
    112                 }
    113             }//3 ‘|’ 左右无需等待时间
    114             nxt.x = cur.x + dx[ i ];
    115             nxt.y = cur.y + dy[ i ];
    116             nxt.t = cur.t + 1;
    117             /*恢复nxt很重要*/
    118             if(( mat[ nxt.x ][ nxt.y ]=='|'&&cur.t%2==1 )||( mat[ nxt.x ][ nxt.y ]=='-'&&cur.t%2==0 )){
    119                 nxt.t = cur.t + 2;
    120                 if( cur.y==nxt.y&&nxt.x>cur.x ) nxt.x = cur.x + 2;
    121                 if( cur.y==nxt.y&&nxt.x<cur.x ) nxt.x = cur.x - 2;
    122                 if( nxt.x>=0&&nxt.x<n&&nxt.y>=0&&nxt.y<m&&mat[ nxt.x ][ nxt.y ]=='.' ){
    123                     if( vis[ nxt.x ][ nxt.y ][ nxt.t%2 ]==false ){
    124                         vis[ nxt.x ][ nxt.y ][ nxt.t%2 ] = true;
    125                         q.push( nxt );
    126                         //printf("4 nxt:[%d,%d] t = %d
    ",nxt.x,nxt.y,nxt.t);
    127                         continue;
    128                     }
    129                 }
    130             }//4 ‘|’ 左右需要等待时间
    131         }
    132     }
    133     return e.t;
    134 }
    135      
    136 int main(){
    137     int n,m;
    138     //freopen( "in.txt","r",stdin );
    139     while( scanf("%d%d",&n,&m)==2 ){
    140         for( int i=0;i<n;i++ ){
    141             scanf("%s",mat[ i ]);
    142             for( int j=0;j<m;j++ ){
    143                 if( mat[ i ][ j ]=='S' ){
    144                     s.x = i;
    145                     s.y = j;
    146                     s.t = 0;
    147                     mat[ i ][ j ] = '.';
    148                 }
    149                 else if( mat[ i ][ j ]=='T' ){
    150                     e.x = i;
    151                     e.y = j;
    152                     e.t = inf;
    153                     mat[ i ][ j ] = '.';
    154                 }
    155             }
    156         }
    157         printf("%d
    ",bfs( n,m ));
    158     }
    159     return 0;
    160 }
    View Code
    keep moving...
  • 相关阅读:
    11-基于CPCI的中频功率放大收发板
    10-基于TMS320C6678+XC7K325T的6U CPCI Full Camera Link图像处理平台
    141-FMC141-4路 250Msps/16bits ADC, FMC板卡
    125-FMC125-两路125Msps AD,两路160Msps DA FMC子卡模块
    164-基于TI DSP TMS320C6455和Altera FPGA EP2S130的Full CameraLink PDS150接口板卡
    北京太速科技有限公司 layout 事业部
    20-基于 DSP TMS320C6455的6U CPCI高速信号处理板卡
    64-基于TMS320C6455、XC5VSX95T 的6U CPCI无线通信处理平台
    18-基于双TMS320C6678 DSP的3U VPX的信号处理平台
    202-基于TI DSP TMS320C6678、Xilinx K7 FPGA XC72K325T的高速数据处理核心板
  • 原文地址:https://www.cnblogs.com/xxx0624/p/3397485.html
Copyright © 2020-2023  润新知