HDU 1180 诡异的楼梯
集训第二天练习宽搜 写的最郁闷的一题 一个晚上的时间都花在这里了
题目是最普通的宽搜加一个 会变换的楼梯 竖着只能上下走 横着只能左右走
允许停留 跨越楼梯不需要时间
对我来说 题目最大的坑点在于 停留的时候 其他结点都在扩散 如果这时候 直接把结点加入队列 会打散扩散顺序
最后我用了优先队列 还是太辣鸡了啊 唉
AC Code
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <queue>
using namespace std;
struct node
{
int x,y;
int step;
node(int a,int b,int c):x(a),y(b),step(c) {}
friend bool operator < (const node &a, const node &b)
{
return a.step > b.step;
}
};
int row,col;
int st_x,st_y,en_x,en_y;
char mat[23][23];
bool vis[23][23];
int dx[]= {1,0,-1,0};
int dy[]= {0,-1,0,1};
bool judge(int xx,int yy)
{
return xx>0&&xx<=row&&yy>0&&yy<=col&&!vis[xx][yy]&&mat[xx][yy]!='*'&&mat[xx][yy]!='-'&&mat[xx][yy]!='|';
}
void bfs()
{
priority_queue<node> que;
que.push(node(st_x,st_y,0));
vis[st_x][st_y]=true;
bool flag;
while(!que.empty())
{
node temp=que.top();
que.pop();
int x=temp.x,y=temp.y;
int step=temp.step;
flag=step%2;
if(x==en_x&&y==en_y)
{
printf("%d\n",step);
return ;
}
for(int i=0; i<4; i++)
{
int xx=x+dx[i];
int yy=y+dy[i];
if(mat[xx][yy]=='|'||mat[xx][yy]=='-')
{
if((mat[xx][yy]=='|'&&!flag)||(mat[xx][yy]=='-'&&flag))
{
if(dy[i]==0)
{
if(judge(xx+dx[i],yy))
xx+=dx[i];
}
else if(judge(xx,yy+dy[i]))
{
step++;
yy+=dy[i];
}
}
else if((mat[xx][yy]=='-'&&!flag)||(mat[xx][yy]=='|'&&flag))
{
if(dx[i]==0)
{
if(judge(xx,yy+dy[i]))
yy+=dy[i];
}
else if(judge(xx+dx[i],yy))
{
step++;
xx+=dx[i];
}
}
}
if(judge(xx,yy))
{
que.push(node(xx,yy,step+1));
vis[xx][yy]=true;
}
}
}
return ;
}
int main()
{
while(scanf("%d%d",&row,&col)!=EOF)
{
getchar();
for(int i=1; i<=row; i++)
{
for(int j=1; j<=col; j++)
{
scanf("%c",&mat[i][j]);
if(mat[i][j]=='S') st_x=i,st_y=j;
if(mat[i][j]=='T') en_x=i,en_y=j;
}
getchar();
}
memset(vis,false,sizeof vis);
bfs();
}
return 0;
}