hdu 1180 诡异的楼梯

来源:互联网 发布:java实现snmp原理 编辑:程序博客网 时间:2024/06/09 19:09

//楼梯有多个  第一次申请了一个NODE LOUTI 来保存一直没找出哪里错
#include <iostream>
#include <queue>
#include<cstdio>
using namespace std;
char map[21][21];
struct  node{
    int x,y;
    int time;
    char c;
};
int sx,sy,dx,dy;
//node louti;
bool hash[21][21];
int dir[4][2] = {{1,0},{-1,0},{0,1},{0,-1}};
int M,N;
void bfs(int x,int y)
{
    memset(hash,false,sizeof(hash));
    queue<node> q;
    node t,p;
    t.x = x;
    t.y = y;
    t.time = 0;
    q.push(t);
    hash[t.x][t.y]=true;
    while (!q.empty())
    {
        t=q.front();
        q.pop();
        if (dx==t.x&&dy==t.y)
        {
            cout<<t.time<<endl;
            break;
        }

        int i;
        for (i=0;i<4;i++)
        {          
            p.x = t.x + dir[i][0];
            p.y = t.y + dir[i][1];
            if (p.x >=0 && p.x < M && p.y >= 0 && p.y < N && map[p.x][p.y] != '*' && !hash[p.x][p.y])
            {
                if (map[p.x][p.y]=='.'||map[p.x][p.y]=='T')
                {
                    p.time=t.time+1;
                    q.push(p);   
                    hash[p.x][p.y]=true;
                }

                if (map[p.x][p.y] == '-' &&(t.time&1)==0 || map[p.x][p.y]=='|' && t.time&1)
                {
                   
                        if(p.x==t.x)
                        {
                            p.y = p.y + p.y-t.y;
                            p.time=t.time+1;                           
                            if (p.x>=0&&p.x<M&&p.y>=0&&p.y<N&&map[p.x][p.y]!='*'&&!hash[p.x][p.y])
                            {
                                q.push(p);
                                hash[p.x][p.y]=true;
                               
                            }
                        }
                        else
                        {
                            p.x = p.x + p.x - t.x;
                            p.y= t.y;
                            p.time=t.time+2;
                            q.push(p);
                        }
                   
                  
                }

                if (map[p.x][p.y]=='|' && (t.time&1)==0 || map[p.x][p.y] == '-' &&t.time&1)
                {
                   
                       
                   
                        if(p.y==t.y)
                        {
                            p.x=p.x+p.x-t.x;
                            p.time=t.time+1;
                            if (p.x>=0&&p.x<M&&p.y>=0&&p.y<N&&map[p.x][p.y]!='*'&&!hash[p.x][p.y])
                            {
                                q.push(p);
                                hash[p.x][p.y]=true;
                                hash[t.x][t.y]=true;
                            }
                        }
                        else
                        {
                            p.x=t.x;
                            p.y = p.y + p.y-t.y;
                            p.time=t.time + 2;
                            q.push(p);
                        }
                   

                }
            }
        }   
    }
 

}

int main()
{
   
    int i,j;
 while(cin>>M>>N)
 {
  for(i = 0; i < M; i++)
  for(j = 0; j < N; j++)
  {
   cin>>map[i][j];
   if(map[i][j] == 'S')
   {
    sx = i;
    sy = j;
   }
   else if(map[i][j] == 'T')
   {
    dx = i;
    dy = j;
   }
  
   //hash[i][j] = true;
  }
  bfs(sx,sy);
 }

    return 0;
}

原创粉丝点击