您的位置:首页 > 移动开发 > IOS开发

hdu 1180 诡异的楼梯

2014-08-04 20:47 267 查看


诡异的楼梯

                                                                               Time Limit: 2000/1000 MS (Java/Others)    Memory
Limit: 131072/65536 K (Java/Others)

                                                                                                        Total Submission(s):
8775    Accepted Submission(s): 2169


少有的中文广搜题意思不多做解释

解题:除去楼梯的变化就是一个极其简单的广搜模板题  此题加入楼梯的横竖变化也只是多一重判断 注意走楼梯是不计算时间的还有楼梯上是不能停留的只能一步直接“跨”过去所以要特别注意走楼梯的时候的边界(楼梯可以靠着墙也可以直接摆在边界上这种些情况下很容易越界。。)问题
#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#include<stack>
#include<algorithm>
using namespace std;
struct node{
friend bool operator<(const node a,const node b)
{
return a.depth>b.depth;
}
int x,y,depth;
};
char map[25][25];
int sx,sy,ex,ey,n,m;
int vis[25][25];
int dx[4]={1,0,-1,0};
int dy[4]={0,1,0,-1};
int bfs(int x,int y)
{
int i;
priority_queue<node> q;
node a,b,c;
a.x=x;
a.y=y;
a.depth=0;
q.push(a);
vis[x][y]=1;
while(!q.empty())
{
b=q.top();
q.pop();
for(i=0;i<4;i++)
{
c.x=b.x+dx[i];
c.y=b.y+dy[i];
c.depth=b.depth+1;
if(!vis[c.x+dx[i]][c.y+dy[i]]&&(map[c.x][c.y]=='|'||map[c.x][c.y]=='-')&&c.x+dx[i]<n&&c.x+dx[i]>=0&&c.y+dy[i]>=0&&c.y+dy[i]<m&&map[c.x+dx[i]][c.y+dy[i]]!='*')
{
if(b.depth%2)
{
if(map[c.x][c.y]=='|')
{
if(dx[i]==0)
{
c.x=c.x+dx[i];
c.y=c.y+dy[i];

}
else
{
c.x=c.x+dx[i];
c.y=c.y+dy[i];
c.depth+=1;

}

}
else if(map[c.x][c.y]=='-')
{

if(dy[i]==0)
{
c.x=c.x+dx[i];
c.y=c.y+dy[i];
}
else
{
c.depth+=1;
c.x=c.x+dx[i];
c.y=c.y+dy[i];

}
}

}
else
{
if(map[c.x][c.y]=='|')
{
if(dx[i]==0)
{
c.x=c.x+dx[i];
c.y=c.y+dy[i];
c.depth+=1;
}
else
{
c.x=c.x+dx[i];
c.y=c.y+dy[i];
}

}
else if(map[c.x][c.y]=='-')
{

if(dy[i]==0)
{
c.depth+=1;
c.x=c.x+dx[i];
c.y=c.y+dy[i];
}
else
{
c.x=c.x+dx[i];
c.y=c.y+dy[i];
}
}

}
vis[c.x][c.y]=1;
if(c.x==ex&&c.y==ey)
return c.depth;
q.push(c);

}
else if((map[c.x][c.y]=='.'||map[c.x][c.y]=='T')&&!vis[c.x][c.y]&&c.x<n&&c.x>=0&&c.y<m&&c.y>=0)
{

vis[c.x][c.y]=1;
if(c.x==ex&&c.y==ey)
return c.depth;
q.push(c);

}
}

}
return -1;
}
int main()
{
// freopen("in.txt","r",stdin);
int i,j;
while(cin>>n>>m)
{
memset(map,0,sizeof map);
memset(vis,0,sizeof vis);
for(i=0;i<n;i++)
cin>>map[i];
for(i=0;i<n;i++)
for(j=0;j<m;j++)
{
if(map[i][j]=='S')
{
sx=i;sy=j;
}
if(map[i][j]=='T')
{
ex=i;ey=j;
}
if(map[i][j]=='*')
{
vis[i][j]==1;
}
}
int ans=bfs(sx,sy);
cout<<ans<<endl;
}

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  namespace iostream bfs