标签:des style http color java os strong io
5 5 **..T **.*. ..|.. .*.*. S....
7地图如下:HintHint
—————————————————————分割线———————————————————————————————
题目的注意点:
楼梯每一分钟会改变方向(可以根据走的步数的奇偶来判断当前方向)
如果碰到楼梯,方向不对,可以停留在原地等下一分钟楼梯改变方向
#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#define M 25
using namespace std;
int vis[M][M],n,m,sx,sy;
int dir[4][2]={{0,1},{0,-1},{1,0},{-1,0}};//右左下上
char map[M][M];
struct node
{
int x,y,st;
};
bool ck(int x,int y)
{
if(x>=0&&x<n&&y>=0&&y<m&&map[x][y]!='*'&&!vis[x][y]) return true;
//判断是否合法
return false;
}
int jd(int x,int y,int d,int st)
{
if(map[x][y]=='|'&&(d==0||d==1)&&(st%2==0)) return 1;//走不了
if(map[x][y]=='|'&&(d==2||d==3)&&(st&1)) return 1;
if(map[x][y]=='|'&&(d==2||d==3)&&(st%2==0)) return 0;//能走
if(map[x][y]=='|'&&(d==0||d==1)&&(st&1)) return 0;
if(map[x][y]=='-'&&(d==2||d==3)&&(st%2==0)) return 1;
if(map[x][y]=='-'&&(d==0||d==1)&&(st&1)) return 1;
if(map[x][y]=='-'&&(d==0||d==1)&&(st%2==0)) return 0;
if(map[x][y]=='-'&&(d==2||d==3)&&(st&1)) return 0;
}
int bfs(int x,int y)
{
memset(vis,0,sizeof vis);
queue<node> q;
node a,b;
a.x=x,a.y=y,a.st=0;
vis[x][y]=1;
q.push(a);
while(!q.empty()){
a=q.front();q.pop();
if(map[a.x][a.y]=='T'){
return a.st;
}
for(int i=0;i<4;++i){
b.x=a.x+dir[i][0];
b.y=a.y+dir[i][1];
b.st=a.st+1;
if(!ck(b.x,b.y)) continue;
if(map[b.x][b.y]=='.'||map[b.x][b.y]=='T'){
vis[b.x][b.y]=1;
q.push(b);
}
else{//碰到楼梯
if(!jd(b.x,b.y,i,a.st)){//能走
b.x+=dir[i][0];
b.y+=dir[i][1];
if(!ck(b.x,b.y)) continue;
else{
vis[b.x][b.y]=1;
q.push(b);
}
}
else{//不能走,保留在原地
b.x=a.x;
b.y=a.y;
q.push(b);
}
}
}
}
return -1;
}
int main()
{
//freopen("input.txt","r",stdin);
//freopen("output.txt","w",stdout);
while(scanf("%d%d",&n,&m)!=EOF){
for(int i=0;i<n;++i){
for(int j=0;j<m;++j){
cin>>map[i][j];
if(map[i][j]=='S'){
sx=i,sy=j;
}
}
}
printf("%d\n",bfs(sx,sy));
}
return 0;
}
HDU 1180——诡异的楼梯( BFS),布布扣,bubuko.com
标签:des style http color java os strong io
原文地址:http://blog.csdn.net/u014141559/article/details/38297077