HDOJ1180(诡异的楼梯)

//1169766 2009-03-22 14:51:30 Accepted 1180 0MS 240K 2063 B C++ 小C 
#include <iostream>
#include 
<queue>
using namespace std;

const int MAX = 30
;

char
 map[MAX][MAX];
bool
 visit[MAX][MAX];
int
 m,n;
int
 sx,sy;
int dir[4][2]={{-1,0},{1,0},{0,-1},{0,1}}
;

typedef 
struct

{
    
int
 x,y;
    
int cost;//花费时间

}
Point;

void
 init()
{
    
int
 i,j;
    
for(i = 0; i < m; i++
)
        
for(j = 0; j < n; j++
)
            visit[i][j] 
= false
;
}

void findse()
{
    
bool flag = false
;
    
for(int i = 0; i < m; i++
)
    
{
        
for(int j = 0; j < n; j++
)
            
if(map[i][j] == 'S'
)
            
{
                sx 
= i;sy =
 j;
                flag 
= true
;
                
break
;
            }

        
if(flag) break;
    }

}


bool isBound(int x, int y)
{
    
if(x < 0 || y < 0
)
        
return false
;
    
if(x >= m || y >=
 n)
        
return false
;
    
return true
;
}


int bfs()
{
    queue
<Point>
 Q;
    Point a, b;
    
int
  i;
    a.x 
= sx; a.y = sy; a.cost = 0
;
    visit[a.x][a.y] 
= true
;
    Q.push(a);
    
while(!
Q.empty())
    
{//while
        a = Q.front();
        Q.pop();
        
for(i = 0; i < 4; i++
)
        
{//for
            b.x = a.x + dir[i][0];
            b.y 
= a.y + dir[i][1
];
            b.cost 
= a.cost + 1
;
            
if(isBound(b.x, b.y) && map[b.x][b.y] != '*'
)
            
{//此题中的bfs第一次搜到的应当为最短时间
                if(map[b.x][b.y] == 'T')
                    
return
 b.cost;
                
else

                
{
                    
if(map[b.x][b.y] == '-'
)
                    
{//考虑4种情况
                        if(a.x == b.x && a.cost % 2)
                            b.cost
++
;
                        
else if(a.y == b.y && a.cost % 2 == 0
)
                            b.cost
++
;
                        
//=======遇到电梯则直接滑过去=========

                        b.x += dir[i][0];
                        b.y 
+= dir[i][1
];
                        
if(!isBound(b.x,b.y) || map[b.x][b.y] == '*'
)
                            
continue
;
                    }
else  if( map[b.x][b.y] == '|')
                    
{//考虑4种情况
                        if(a.y == b.y && a.cost % 2)
                            b.cost
++
;
                        
else if(a.x == b.x && a.cost % 2 == 0
)
                            b.cost
++
;
                        b.x 
+= dir[i][0
];
                        b.y 
+= dir[i][1
];
                        
if(!isBound(b.x, b.y) || map[b.x][b.y] == '*'
)
                            
continue
;
                    }

                    
if(!visit[b.x][b.y])
                    
{
                        
if(map[b.x][b.y] == 'T'
)
                            
return
 b.cost;
                        Q.push(b);
                        visit[b.x][b.y] 
= true
;
                    }

                }

            }
//if
        }
//for
    }
//while
    return 0;
}
//bfs

int main()
{//题中未说明此路径是否一定存在,权且当做一定存在
    
    
while(cin>>m>>
n)
    
{
        
for(int i = 0; i < m; i++
)
            cin
>>
map[i];
        findse();
//寻找起始点

        init();
        cout
<<bfs()<<
endl;
    }

    
return 0;
}

posted on 2009-03-22 15:03  Xredman  阅读(419)  评论(1编辑  收藏  举报

导航