HDU 1180(简单BFS)

题目意思有点不清 其实是可以在原地停留的 注意桥的变化与时间相关

 

用方向数组压缩代码  OK 以下是实现

 

 

 

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

char Map[21][21];
typedef 
struct  
{
    
long mi,mj;
    
long step;
}Node;

Node Start,End;

bool hash[21][21];

long dx[4]={0,0,1,-1};
long dy[4]={1,-1,0,0};

long M,N;

inline 
void BFS()
{
    memset(hash,
0,sizeof(hash));

    queue
<Node> q;

    q.push(Start);

    Node t;

    
long cost=0;

    hash[Start.mi][Start.mj]
=true;

    
while (!q.empty())
    {
        t
=q.front();
        q.pop();

        
if (End.mi==t.mi&&End.mj==t.mj)
        {
            cost
=t.step;
            
break;
        }

        
long i;
        
for (i=0;i<4;++i)
        {
            Node n;
            n.mi
=t.mi+dx[i];
            n.mj
=t.mj+dy[i];

            
if (n.mi>=0&&n.mi<M&&n.mj>=0&&n.mj<N&&Map[n.mi][n.mj]!='*'&&!hash[n.mi][n.mj])
            {
                
if (Map[n.mi][n.mj]=='.'||Map[n.mi][n.mj]=='T')
                {
                    n.step
=t.step+1;
                    q.push(n);    
                    hash[n.mi][n.mj]
=true;
                }

                
if (Map[n.mi][n.mj]=='-')
                {
                    
if ((t.step&1)==0)
                    {
                        
if(n.mi==t.mi)
                        {
                            n.mj
=n.mj+n.mj-t.mj;
                            n.step
=t.step+1;
                            
                            
if(n.mi>=0&&n.mi<M&&n.mj>=0&&n.mj<N&&!hash[n.mi][n.mj]&&Map[n.mi][n.mj]!='*')
                            {
                                q.push(n);
                                hash[n.mi][n.mj]
=true;
                                hash[t.mi][t.mj]
=true;
                            }
                        }
                        
else
                        {
                            n.mi
=t.mi;
                            n.mj
=t.mj;
                            n.step
=t.step+1;
                            q.push(n);
                        }
                    }
                    
else
                    {
                        
if(n.mj==t.mj)
                        {
                            n.mi
=n.mi+n.mi-t.mi;
                            n.step
=t.step+1;        
                            
if(n.mi>=0&&n.mi<M&&n.mj>=0&&n.mj<N&&!hash[n.mi][n.mj]&&Map[n.mi][n.mj]!='*')
                            {
                                q.push(n);
                                hash[n.mi][n.mj]
=true;
                                hash[t.mi][t.mj]
=true;
                            }
                        }
                        
else
                        {
                            n.mi
=t.mi;
                            n.mj
=t.mj;
                            n.step
=t.step+1;
                            q.push(n);
                        }
                    }
                }

                
if (Map[n.mi][n.mj]=='|')
                {
                    
if (t.step&1)
                    {
                        
if(n.mi==t.mi)
                        {
                            n.mj
=n.mj+n.mj-t.mj;
                            n.step
=t.step+1;
                            
                            
if(n.mi>=0&&n.mi<M&&n.mj>=0&&n.mj<N&&!hash[n.mi][n.mj]&&Map[n.mi][n.mj]!='*')
                            {
                                q.push(n);
                                hash[n.mi][n.mj]
=true;
                                hash[t.mi][t.mj]
=true;
                            }
                        }
                        
else
                        {
                            n.mi
=t.mi;
                            n.mj
=t.mj;
                            n.step
=t.step+1;
                            q.push(n);
                        }
                    }
                    
else
                    {
                        
if(n.mj==t.mj)
                        {
                            n.mi
=n.mi+n.mi-t.mi;
                            n.step
=t.step+1;
                            
                            
if(n.mi>=0&&n.mi<M&&n.mj>=0&&n.mj<N&&!hash[n.mi][n.mj]&&Map[n.mi][n.mj]!='*')
                            {
                                q.push(n);
                                hash[n.mi][n.mj]
=true;
                                hash[t.mi][t.mj]
=true;
                            }
                        }
                        
else
                        {
                            n.mi
=t.mi;
                            n.mj
=t.mj;
                            n.step
=t.step+1;
                            q.push(n);
                        }
                    }

                }
            }
        }    
    }
    
//题目保证可达
    printf("%ld\n",cost);

}

int main()
{
    
    
while (scanf("%ld %ld",&M,&N)!=EOF)
    {
        
long i,j;
        getchar();
        
for (i=0;i<M;++i)
        {
            gets(Map[i]);
            
for (j=0;j<N;++j)
            {
                
if (Map[i][j]=='S')
                {
                    Start.mi
=i;
                    Start.mj
=j;
                    Start.step
=0;
                }

                
if (Map[i][j]=='T')
                {
                    End.mi
=i;
                    End.mj
=j;
                }
            }
        }

        BFS();

    }

    
return 0;
}
posted @ 2008-09-13 09:53  Hdu-Lost  阅读(470)  评论(0编辑  收藏  举报