bfs问题。
题意是说爬楼梯的时候,有些楼梯是 “ | ”,有些是“ - ”。而且每隔一分钟就互相变化形态。
“ | ”只能上下,“ - ”只能左右。
爬楼梯的过程中,会变的楼梯不能停留,其他的可以停留。
爬楼梯需要一个单位时间,假如是“ - ”表明一个单位时间从它左边到它右边或者 右边到左边。
楼梯停留多次没有意义,特殊楼梯只有2种旋转状态,多一个wait[][] 检查就可以。
#include<cstdio>
#include<cstring>
#include<string>
#include<queue>
#include<algorithm>
#include<map>
#include<stack>
#include<iostream>
#include<list>
#include<set>
#include<vector>
#include<cmath>
#define INF 0x7fffffff
#define eps 1e-8
#define LL long long
#define PI 3.141592654
#define CLR(a,b) memset(a,b,sizeof(a))
#define FOR(i,a,n) for(int i= a;i< n ;i++)
#define debug puts("==fuck==")
#define acfun std::ios::sync_with_stdio(false)
#define SIZE 20+10
using namespace std;
int xx[]={0,0,-1,1};
int yy[]={-1,1,0,0};
int n,m;
char g[SIZE][SIZE];
struct lx
{
int x,y,lv;
void init(int xx,int yy,int llv)
{
x=xx,y=yy,lv=llv;
}
friend bool operator< (lx a,lx b)
{
return a.lv>b.lv;
}
};
lx start;
void bfs()
{
priority_queue<lx>q;
bool vis[SIZE][SIZE];
bool wait[SIZE][SIZE];
CLR(vis,0);
CLR(wait,0);
vis[start.x][start.y]=1;
q.push(start);
while(!q.empty())
{
lx tmp=q.top();
q.pop();
// printf("%d %d==%d\n",tmp.x,tmp.y,tmp.lv);
// system("pause");
if(g[tmp.x][tmp.y]=='T')
{
printf("%d\n",tmp.lv);
return ;
}
FOR(k,0,4)
{
int x=tmp.x+xx[k];
int y=tmp.y+yy[k];
if(x<0||y<0||x>=n||y>=m||g[x][y]=='*'||vis[x][y])
continue;
lx now;
if(g[x][y]=='.'||g[x][y]=='T')
{
vis[x][y]=1;
now.init(x,y,tmp.lv+1);
q.push(now);
}
else
{
bool flag=tmp.lv%2;
if(((k==0||k==1)&&((g[x][y]=='|'&&flag)||(g[x][y]=='-'&&!flag)))||((k==2||k==3)&&((g[x][y]=='|'&&!flag)||(g[x][y]=='-'&&flag))))
{
x=x+xx[k];
y=y+yy[k];
if(x<0||y<0||x>=n||y>=m||g[x][y]=='*'||vis[x][y])
continue;
vis[x][y]=1;
now.init(x,y,tmp.lv+1);
q.push(now);
}
if(wait[tmp.x][tmp.y])continue;
wait[tmp.x][tmp.y]=1;
now.init(tmp.x,tmp.y,tmp.lv+1);
q.push(now);
}
}
}
}
int main()
{
while(~scanf("%d%d",&n,&m))
{
char str[SIZE];
FOR(i,0,n)
{
scanf("%s",str);
FOR(j,0,m)
{
g[i][j]=str[j];
if(g[i][j]=='S')
start.init(i,j,0);
}
}
bfs();
}
}