hdu1180 诡异的楼梯
来源:互联网 发布:python ip欺骗 编辑:程序博客网 时间:2024/05/19 01:07
这道题目我第二次写,根据当前步数判断楼梯的朝向,但是一直没有AC,后来想到广搜期间的点优先级不同,因为等待楼梯转向和直接过楼梯是同时入队的,因此普通队列不能区分则会导致错误,故要用到优先队列(之前AC的代码没有用优先队列,不知道是怎么办到的)
附代码如下:
#include <iostream>
#include <cstring>
#include <cstdio>
#include <queue>
using namespace std;
const int Go[4][2] = {0,-1,0,1,-1,0,1,0};//右上下左
struct Node
{
int x, y;
int step;
bool operator <(const Node t)const
{
return step > t.step;
}
}start;
int n, m;
char map[21][21];
bool visited[21][21];
int ex, ey;
bool check(int x, int y)
{
return (x>=0&&x<n&&y>=0&&y<m);
}
bool judge(int step, int xx, int yy, int i)
{
if (step%2){
if (map[xx][yy] == '|'){
if (i == 2||i==3){
return true;
}
}
if (map[xx][yy] == '-'){
if (i == 0||i==1){
return true;
}
}
}
else {
if (map[xx][yy] == '-'){
if (i == 2||i== 3){
return true;
}
}
if (map[xx][yy] == '|'){
if (i == 0||i==1){
return true;
}
}
}
return false;
}
void Bfs()
{
priority_queue <Node>Que;
memset(visited, 0, sizeof(visited));
Que.push(start);
visited[start.x][start.y] = 1;
while (!Que.empty()){
Node pre = Que.top();
Que.pop();
//printf("%d %d/n", pre.x, pre.y);
if (map[pre.x][pre.y] == 'T'){
printf("%d/n", pre.step);
return ;
}
for (int i = 0; i < 4; i ++){
Node next;
next. x = pre.x + Go[i][0];
next. y = pre.y + Go[i][1];
next. step = pre.step;
if (check(next.x, next.y)&&map[next.x][next.y] != '*'&&!visited[next.x][next.y]){
if (map[next.x][next.y] == '.' || map[next.x][next.y] == 'T'){
next.step ++;
visited[next.x][next.y] = 1;
Que.push(next);
}
else {
if (judge(next.step, next.x, next.y, i)){
next.step += 2;
}
else next.step ++;
next.x += Go[i][0];
next.y += Go[i][1];
if (check(next.x, next.y)&&map[next.x][next.y] != '*'&&!visited[next.x][next.y]){
visited[next.x][next.y] = 1;
Que.push(next);
}
}
}
}
}
}
int main()
{
while (scanf("%d %d", &n, &m) != EOF ){
for (int i = 0; i < n; i ++){
scanf("%s", map[i]);
for (int j = 0; j < m; j ++){
if (map[i][j] == 'S'){
start.x = i;
start.y = j;
start.step = 0;
}
}
}
Bfs();
}
return 0;
}
- hdu1180 诡异的楼梯
- HDU1180 诡异的楼梯
- hdu1180 诡异的楼梯
- hdu1180诡异的楼梯
- HDU1180 诡异的楼梯
- HDU1180-诡异的楼梯
- HDU1180 诡异的楼梯--BFS
- hdu1180诡异的楼梯 bfs
- hdu1180诡异的楼梯(bfs)
- HDU1180 诡异的楼梯[dfs]
- hdu1180 诡异的楼梯--BFS
- HDU1180-诡异的楼梯(bfs)
- HDU1180 诡异的楼梯(BFS)
- hdu1180——诡异的楼梯
- hdu1180诡异的楼梯(BFS+优先队列)
- hdu1180诡异的楼梯(广搜)
- Hdu1180 - 诡异的楼梯 - 广度优先搜索
- 【搜索-BFS】HDU1180诡异的楼梯
- 欺骗
- 欺骗
- flash builder生成的文件
- Ubuntu右键显示终端选项
- 玩笑程序
- hdu1180 诡异的楼梯
- 数据库中sql语句执行效率之议
- dll调试成功和成功拦截窗口创建
- 如何学好c++
- 软件管理
- 小议函数调用—关于调用后的值保存问题
- 附件程序
- TaobaoWeb
- Spring+Servlet整合(如何向Servlet注入属性(转),servlet获取spring容器中的bean)