HDU1180 诡异的楼梯
2016-04-07 12:51
295 查看
点击打开题目链接
题意就是要从S点走到T点 问最少的步数
一看题就知道应该用bfs
难就难在如何判断当前楼梯的方向
可以用从起点到楼梯的步数来判断楼梯的方向
如果从起点到楼梯的步数step%2==0 说明当前楼梯的状态与开始时是一样的 直接过就可以了 否则 则需要等一分钟
但是在判断楼梯的同时也要判断过楼梯之后所到达的那个点是否访问过 访问过 则说明 有其他路径耗费更少的时间经过那一点 就不走了
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
using namespace std;
char map[30][30];
bool vis[30][30];
int n, m, sx, sy, ex, ey;
int go[4][2] = {{1,0},{-1,0},{0,1},{0,-1}};
struct node
{
int x,y,step;
};
int judge(char a,int x,int y,int i,int time)
{
int x1,y1;
x1 = x + go[i][0];
y1 = y + go[i][1];
if(vis[x1][y1])
return 0;
if(time%2==0)
{
if(map[x][y]=='|')
{
if(go[i][0] == 0)
time++;
}
else
{
if(go[i][1] == 0)
time++;
}
}
else
{
if(map[x][y]=='|') /// 原本楼梯是竖的 当前楼梯是横的
{
if(go[i][1]==0)
time++;
}
if(map[x][y]=='-')/// 原本是横的 当前楼梯是竖的
{
if(go[i][0]==0)
time++;
}
}
return time + 1;
}
void bfs()
{
int flag;
node p;
memset(vis,0,sizeof(vis));
p.x = sx, p.y = sy, p.step = 0;
queue<node>q;
vis[sx][sy] = 1;
node now, next;
q.push(p);
while(!q.empty())
{
now = q.front();
q.pop();
if(map[now.x][now.y]=='T')
{
printf("%d\n",now.step);
return;
}
for(int i = 0; i < 4; i++)
{
next.x = now.x + go[i][0];
next.y = now.y + go[i][1];
next.step = now.step;
if(next.x>=0&&next.x<n&&next.y>=0&&next.y<m&&map[next.x][next.y]!='*'&&!vis[next.x][next.y])
{
if(map[next.x][next.y]=='.'||map[next.x][next.y]=='T')
{
vis[next.x][next.y] = 1;
next.step = now.step + 1;
q.push(next);
}
if(map[next.x][next.y]=='|'||map[next.x][next.y]=='-')
{
flag = judge(map[next.x][next.y],next.x,next.y,i,now.step);
if(flag == 0)
continue;
next.x+=go[i][0];
next.y+=go[i][1];
next.step = flag;
q.push(next);
}
}
}
}
}
int main()
{
while(~scanf("%d%d",&n,&m))
{
int i, j;
getchar();
for(i = 0; i < n; i++)
{
gets(map[i]);
for(j = 0; j < m; j++)
{
if(map[i][j] == 'S')
{
sx = i;
sy = j;
//cout << sx << sy << endl;
}
/*if(map[i][j] == 'T')
{
ex = i;
ey = j;
//cout <<ex << ey << endl;
}*/
}
}
bfs();
}
return 0;
}
题意就是要从S点走到T点 问最少的步数
一看题就知道应该用bfs
难就难在如何判断当前楼梯的方向
可以用从起点到楼梯的步数来判断楼梯的方向
如果从起点到楼梯的步数step%2==0 说明当前楼梯的状态与开始时是一样的 直接过就可以了 否则 则需要等一分钟
但是在判断楼梯的同时也要判断过楼梯之后所到达的那个点是否访问过 访问过 则说明 有其他路径耗费更少的时间经过那一点 就不走了
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
using namespace std;
char map[30][30];
bool vis[30][30];
int n, m, sx, sy, ex, ey;
int go[4][2] = {{1,0},{-1,0},{0,1},{0,-1}};
struct node
{
int x,y,step;
};
int judge(char a,int x,int y,int i,int time)
{
int x1,y1;
x1 = x + go[i][0];
y1 = y + go[i][1];
if(vis[x1][y1])
return 0;
if(time%2==0)
{
if(map[x][y]=='|')
{
if(go[i][0] == 0)
time++;
}
else
{
if(go[i][1] == 0)
time++;
}
}
else
{
if(map[x][y]=='|') /// 原本楼梯是竖的 当前楼梯是横的
{
if(go[i][1]==0)
time++;
}
if(map[x][y]=='-')/// 原本是横的 当前楼梯是竖的
{
if(go[i][0]==0)
time++;
}
}
return time + 1;
}
void bfs()
{
int flag;
node p;
memset(vis,0,sizeof(vis));
p.x = sx, p.y = sy, p.step = 0;
queue<node>q;
vis[sx][sy] = 1;
node now, next;
q.push(p);
while(!q.empty())
{
now = q.front();
q.pop();
if(map[now.x][now.y]=='T')
{
printf("%d\n",now.step);
return;
}
for(int i = 0; i < 4; i++)
{
next.x = now.x + go[i][0];
next.y = now.y + go[i][1];
next.step = now.step;
if(next.x>=0&&next.x<n&&next.y>=0&&next.y<m&&map[next.x][next.y]!='*'&&!vis[next.x][next.y])
{
if(map[next.x][next.y]=='.'||map[next.x][next.y]=='T')
{
vis[next.x][next.y] = 1;
next.step = now.step + 1;
q.push(next);
}
if(map[next.x][next.y]=='|'||map[next.x][next.y]=='-')
{
flag = judge(map[next.x][next.y],next.x,next.y,i,now.step);
if(flag == 0)
continue;
next.x+=go[i][0];
next.y+=go[i][1];
next.step = flag;
q.push(next);
}
}
}
}
}
int main()
{
while(~scanf("%d%d",&n,&m))
{
int i, j;
getchar();
for(i = 0; i < n; i++)
{
gets(map[i]);
for(j = 0; j < m; j++)
{
if(map[i][j] == 'S')
{
sx = i;
sy = j;
//cout << sx << sy << endl;
}
/*if(map[i][j] == 'T')
{
ex = i;
ey = j;
//cout <<ex << ey << endl;
}*/
}
}
bfs();
}
return 0;
}
相关文章推荐
- 搜狗百度360市值齐跌:搜索引擎们陷入集体焦虑?
- 本人即将筹备败家日志,敬请期待!
- IE:使用搜索助手
- C++深度优先搜索的实现方法
- 基于文本的搜索
- php实现搜索一维数组元素并删除二维数组对应元素的方法
- 使用Sphinx对索引进行搜索
- asp 多关键词搜索的简单实现方法
- C#使用foreach语句搜索数组元素的方法
- WordPress中用于获取搜索表单的PHP函数使用解析
- JavaScript中数组的排序、乱序和搜索实现代码
- jquery ztree实现树的搜索功能
- C#编程实现Excel文档中搜索文本内容的方法及思路
- sqlserver中在指定数据库的所有表的所有列中搜索给定的值
- 可以用来搜索当前页面内容的js代码
- 全文搜索和替换
- javascript搜索自动提示功能的实现第1/3页
- iOS应用中UISearchDisplayController搜索效果的用法
- mysql 模糊搜索的方法介绍
- C#搜索文字在文件及文件夹中出现位置的方法