您的位置:首页 > 其它

hdu 1180 诡异的楼梯

2016-07-30 08:08 387 查看
广搜的三维标记,第三维标记来路(即来自的四个方向分别标记),再用sort优先步数最小的,加C的广搜的队列模板 ,ac
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
using namespace std;
const int N=30;
char a

;
int book

[4];
struct f{
int x,y,step;
}b[N*N*4];
bool cmp(f p,f q){return p.step<q.step;}
int main(){
int n,m;
while(~scanf("%d%d",&n,&m)){
for(int i=1;i<=n;i++) scanf("%s",a[i]+1);
int startx=0,starty=0,endx=0,endy=0;
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)
if(a[i][j]=='S'){startx=i,starty=j;}
else if(a[i][j]=='T'){endx=i,endy=j;}
int head=0,tail=1,flag=0;
memset(book,0,sizeof(book));
b[head].x=startx,b[head].y=starty,b[head].step=0;
while(head<tail){
sort(b+head,b+tail,cmp);
int next[4][2]={{0,1},{1,0},{0,-1},{-1,0}};
for(int i=0;i<4;i++)
{
int tx=b[head].x+next[i][0],ty=b[head].y+next[i][1],kk=0;
if(a[tx][ty]=='-'){
if(b[head].step%2!=0) {
if(i==1&&!book[tx][ty][3]){book[tx][ty][3]=1;tx+=1;}
else if(i==3&&!book[tx][ty][1]){book[tx][ty][1]=1;tx-=1;}
else if(i==0&&!book[tx][ty][2]){book[tx][ty][2]=1;ty+=1;kk=1;}
else if(i==2&&!book[tx][ty][0]){book[tx][ty][0]=1;ty-=1;kk=1;}
else continue;
}
else{
if(i==0&&!book[tx][ty][2]){book[tx][ty][2]=1;ty+=1;}
else if(i==2&&!book[tx][ty][0]){book[tx][ty][0]=1;ty-=1;}
else if(i==1&&!book[tx][ty][3]){book[tx][ty][3]=1;tx+=1;kk=1;}
else if(i==3&&!book[tx][ty][1]){book[tx][ty][1]=1;tx-=1;kk=1;}
else continue;
}
}
else if(a[tx][ty]=='|'){
if(b[head].step%2!=0){
if(i==0&&!book[tx][ty][2]){book[tx][ty][2]=1;ty+=1;}
else if(i==2&&!book[tx][ty][0]){book[tx][ty][0]=1;ty-=1;}
else if(i==1&&!book[tx][ty][3]){book[tx][ty][3]=1;tx+=1;kk=1;}
else if(i==3&&!book[tx][ty][1]){book[tx][ty][1]=1;tx-=1;kk=1;}
else continue;
}
else{
if(i==1&&!book[tx][ty][3]){book[tx][ty][3]=1;tx+=1;}
else if(i==3&&!book[tx][ty][1]){book[tx][ty][1]=1;tx-=1;}
else if(i==0&&!book[tx][ty][2]){book[tx][ty][2]=1;ty+=1;kk=1;}
else if(i==2&&!book[tx][ty][0]){book[tx][ty][0]=1;ty-=1;kk=1;}
else continue;
}
}
else {
if(i==0&&!book[tx][ty][2]) book[tx][ty][2]=1;
else if(i==1&&!book[tx][ty][3]) book[tx][ty][3]=1;
else if(i==2&&!book[tx][ty][0]) book[tx][ty][0]=1;
else if(i==3&&!book[tx][ty][1]) book[tx][ty][1]=1;
else continue;
}
if(tx<1||tx>n||ty<1||ty>m||a[tx][ty]=='*') continue;
b[tail].x=tx,b[tail].y=ty,b[tail++].step=b[head].step+1+kk;
if(tx==endx&&ty==endy){flag=1;break;}
}
if(flag) break;
head++;
}
//for(int i=0;i<tail;i++)
// printf("%d %d %d\n",b[i].step,b[i].x,b[i].y);
if(flag) cout<<b[tail-1].step<<endl;
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  广搜