优先队列广搜,有人说用SPFA,不知道怎么做的

#include <cstdio>
#include <queue>
#include <cmath>
#include <cstring>
#include <cstdlib>
using namespace std; #define MAX_COORDINATE 205
#define inf 0x3f3f3f3f
#define EDGE 200 struct Grid
{
int left, right, up, down;
}grid[MAX_COORDINATE][MAX_COORDINATE]; // using down-left coner point to present a gird struct Point
{
int x, y;
int door_cnt;
Point()
{}
Point(int xx, int yy, int dd):x(xx), y(yy), door_cnt(dd)
{}
}nemo; int wall_num, door_num;
bool vis[MAX_COORDINATE][MAX_COORDINATE]; bool operator < (const Point &a, const Point &b)
{
return a.door_cnt > b.door_cnt;
} void make_grids(int x, int y, int direction, int value)
{
if (direction == )
{
grid[x][y].down = value;
grid[x][y - ].up = value;
}else
{
grid[x][y].left = value;
grid[x - ][y].right = value;
}
} void build_wall(int x, int y, int direction, int length)
{
for (int i = ; i < length; i++)
{
int current_x = x;
int current_y = y;
if (direction == )
current_x += i;
else
current_y += i;
make_grids(current_x, current_y, direction, inf);
}
} void init_grids()
{
for (int i = ; i < EDGE; i++)
{
grid[i][].down = inf;
grid[i][EDGE - ].up = inf;
grid[][i].left = inf;
grid[EDGE - ][i].right = inf;
}
} void input()
{
memset(grid, , sizeof(grid));
init_grids();
for (int i = ; i < wall_num; i++)
{
int x, y, direction, length;
scanf("%d%d%d%d", &x, &y, &direction, &length);
build_wall(x, y, direction, length);
}
for (int i = ; i < door_num; i++)
{
int x, y, direction;
scanf("%d%d%d", &x, &y, &direction);
make_grids(x, y, direction, );
}
double x, y;
scanf("%lf%lf", &x, &y);
nemo.x = floor(x);
nemo.y = floor(y);
} int work()
{
if (nemo.x < || nemo.x >= EDGE || nemo.y < || nemo.y >= EDGE)
return ;
priority_queue<Point> pq;
pq.push(Point(, , ));
memset(vis, , sizeof(vis));
vis[][] = true;
while (!pq.empty())
{
Point u = pq.top();
if (u.x == nemo.x && u.y == nemo.y)
return u.door_cnt;
pq.pop();
if (grid[u.x][u.y].up != inf && !vis[u.x][u.y + ])
{
pq.push(Point(u.x, u.y + , u.door_cnt + grid[u.x][u.y].up));
vis[u.x][u.y + ] = true;
}
if (grid[u.x][u.y].down != inf && !vis[u.x][u.y - ])
{
pq.push(Point(u.x, u.y - , u.door_cnt + grid[u.x][u.y].down));
vis[u.x][u.y - ] = true;
}
if (grid[u.x][u.y].left != inf && !vis[u.x - ][u.y])
{
pq.push(Point(u.x - , u.y, u.door_cnt + grid[u.x][u.y].left));
vis[u.x - ][u.y] = true;
}
if (grid[u.x][u.y].right != inf && !vis[u.x + ][u.y])
{
pq.push(Point(u.x + , u.y, u.door_cnt + grid[u.x][u.y].right));
vis[u.x + ][u.y] = true;
}
}
return -;
} int main()
{
while (scanf("%d%d", &wall_num, &door_num), !(wall_num == - && door_num == -))
{
input();
printf("%d\n", work());
}
return ;
}

poj2049的更多相关文章

  1. 网格中的BFS,逆向(POJ2049)

    题目链接:http://poj.org/problem?id=2049 解题报告: 网格中的BFS,最主要的是边界问题. 1.这里在左右,上下两个方向上,分别判断墙,和门,细节是,向上有t个墙,for ...

  2. poj分类 很好很有层次感。

    初期: 一.基本算法:      (1)枚举. (poj1753,poj2965)      (2)贪心(poj1328,poj2109,poj2586)      (3)递归和分治法.      ( ...

  3. 【转】POJ题目分类推荐 (很好很有层次感)

    OJ上的一些水题(可用来练手和增加自信) (poj3299,poj2159,poj2739,poj1083,poj2262,poj1503,poj3006,poj2255,poj3094)初期: 一. ...

  4. 【转】ACM训练计划

    [转] POJ推荐50题以及ACM训练方案 -- : 转载自 wade_wang 最终编辑 000lzl POJ 推荐50题 第一类 动态规划(至少6题, 和 必做) 和 (可贪心) (稍难) 第二类 ...

  5. POJ 题目分类(转载)

    Log 2016-3-21 网上找的POJ分类,来源已经不清楚了.百度能百度到一大把.贴一份在博客上,鞭策自己刷题,不能偷懒!! 初期: 一.基本算法: (1)枚举. (poj1753,poj2965 ...

  6. (转)POJ题目分类

    初期:一.基本算法:     (1)枚举. (poj1753,poj2965)     (2)贪心(poj1328,poj2109,poj2586)     (3)递归和分治法.     (4)递推. ...

  7. acm常见算法及例题

    转自:http://blog.csdn.net/hengjie2009/article/details/7540135 acm常见算法及例题  初期:一.基本算法:     (1)枚举. (poj17 ...

  8. poj分类

    初期: 一.基本算法:      (1)枚举. (poj1753,poj2965)      (2)贪心(poj1328,poj2109,poj2586)      (3)递归和分治法.      ( ...

  9. 转载 ACM训练计划

    leetcode代码 利用堆栈:http://oj.leetcode.com/problems/evaluate-reverse-polish-notation/http://oj.leetcode. ...

随机推荐

  1. Thinkphp3.2 入口绑定问题记录

    // 应用入口文件 // 检测PHP环境if(version_compare(PHP_VERSION,'5.3.0','<')) die('require PHP > 5.3.0 !'); ...

  2. 通信对象 System.ServiceModel.Channels.ServiceChannel 无法用于通信,因为其处于“出错”状态。

    通信对象 System.ServiceModel.Channels.ServiceChannel 无法用于通信,因为其处于“出错”状态. 在 System.ServiceModel.Channels. ...

  3. salt-api安装以及简单实使用

    1.安装说明 操作系统版本:CentOS Linux release 7.5.1804 (Core) saltstack版本:2018.3.2 已经关闭selinux.firewalld服务. 2.配 ...

  4. Invalid format of Import utility nameVerify that ORACLE_HOME is properly oracle11.2g 无法imp,dmp

    1.环境变量 ORACLE_HOME 设置了没  D:\app\product\11.2.0\client_1 2.环境变量 ORACLE_SID  设置为orcl 上面是网上流行的解决方案,然而博主 ...

  5. oracle 获取当前用户下的所有表名与字段信息

     select *from user_col_commentswhere substr(table_name,1,3)<>'BIN' 

  6. Spoj 8372 Triple Sums

    题意:给你n个数字,对于任意s,s满足\(s=u_i+u_j+u_k,i<j<k\),要求出所有的s和对应满足条件的i,j,k的方案数 Solution: 构造一个函数:\(A(x)=\s ...

  7. 【题解】 bzoj1864: [Zjoi2006]三色二叉树 (动态规划)

    bzoj1864,懒得复制,戳我戳我 Solution: 其实想出来了\(dp\)方程推出来了最大值,一直没想到推最小值 \(dp[i][1/0]\)表示\(i\)号节点的子树中的绿色染色最大值,\( ...

  8. 李建20181029课时整理(NOIP考点)

    历年真题 数学题: 数论(exgcd 逆元,CRT,EXCRT,快速幂,线性筛 ,杜教筛)排列组合 概率期望(什么东西) C(n,m) = 逆元? 分解质因数? Ti(大质数的类似物)思考技巧分解质因 ...

  9. 【bzoj4676】 两双手

    http://www.lydsy.com/JudgeOnline/problem.php?id=4767 (题目链接) 题意 求在网格图上从$(0,0)$走到$(n,m)$,其中不经过一些点的路径方案 ...

  10. Hadoop生态圈-Flume的组件之拦截器与选择器

      Hadoop生态圈-Flume的组件之拦截器与选择器 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 本篇博客只是配置的是Flume主流的Interceptors,想要了解更详细 ...