poj2049
优先队列广搜,有人说用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的更多相关文章
- 网格中的BFS,逆向(POJ2049)
题目链接:http://poj.org/problem?id=2049 解题报告: 网格中的BFS,最主要的是边界问题. 1.这里在左右,上下两个方向上,分别判断墙,和门,细节是,向上有t个墙,for ...
- poj分类 很好很有层次感。
初期: 一.基本算法: (1)枚举. (poj1753,poj2965) (2)贪心(poj1328,poj2109,poj2586) (3)递归和分治法. ( ...
- 【转】POJ题目分类推荐 (很好很有层次感)
OJ上的一些水题(可用来练手和增加自信) (poj3299,poj2159,poj2739,poj1083,poj2262,poj1503,poj3006,poj2255,poj3094)初期: 一. ...
- 【转】ACM训练计划
[转] POJ推荐50题以及ACM训练方案 -- : 转载自 wade_wang 最终编辑 000lzl POJ 推荐50题 第一类 动态规划(至少6题, 和 必做) 和 (可贪心) (稍难) 第二类 ...
- POJ 题目分类(转载)
Log 2016-3-21 网上找的POJ分类,来源已经不清楚了.百度能百度到一大把.贴一份在博客上,鞭策自己刷题,不能偷懒!! 初期: 一.基本算法: (1)枚举. (poj1753,poj2965 ...
- (转)POJ题目分类
初期:一.基本算法: (1)枚举. (poj1753,poj2965) (2)贪心(poj1328,poj2109,poj2586) (3)递归和分治法. (4)递推. ...
- acm常见算法及例题
转自:http://blog.csdn.net/hengjie2009/article/details/7540135 acm常见算法及例题 初期:一.基本算法: (1)枚举. (poj17 ...
- poj分类
初期: 一.基本算法: (1)枚举. (poj1753,poj2965) (2)贪心(poj1328,poj2109,poj2586) (3)递归和分治法. ( ...
- 转载 ACM训练计划
leetcode代码 利用堆栈:http://oj.leetcode.com/problems/evaluate-reverse-polish-notation/http://oj.leetcode. ...
随机推荐
- [转帖]VMware Vsphere 6.0安装部署 (一) 总体部署架构
(一)总体部署架构本教程用于学习目的,力求详尽的介绍安装部署过程和各组件之间的关系,部署过程从最简单的模型开始,系列文章按时间顺序依次展开,每篇介绍一个组件. 开始阶段,按照一台物理服务器,部署所有V ...
- Jira 的 数据库备份恢复 简单过程
1 发现jira的备份恢复很简单, 只需要导入导出一个zip包即可 导出 选择系统 管理员入口登录 选择导入导出 进行备份系统数据 选择一个文件名就能备份 备份结果 将文件copy到上一一级目录的 i ...
- OneZero第四周第一次站立会议(2016.4.11)
1. 时间: 15:10--15:25 共计15分钟. 2. 成员: X 夏一鸣 * 组长 (博客:http://www.cnblogs.com/xiaym896/), G 郭又铭 (博客:http ...
- Spring MVC的路径匹配规则 Ant-style
Spring默认的策略实现了 org.springframework.util.AntPathMatcher,即Apache Ant的样式路径,Apache Ant样式的路径有三种通配符匹配方法(在下 ...
- maven基础知识汇总
maven的dependency中scope=compile和provided的区别 对于scope=compile的情况(默认scope),也就是说这个项目在编译,测试,运行阶段都需要这个artif ...
- 想转C++了
暑假想学一学C++ 早退役了高考后才转C++的oier——我真是太蒻了
- 【Java并发编程】之十五:并发编程中实现内存可见的两种方法比较:加锁和volatile变量
转载请注明出处:http://blog.csdn.net/ns_code/article/details/17290021 在http://blog.csdn.net/ns_code/article/ ...
- 【大数据】SparkCore学习笔记
第1章 RDD概述 1.1 什么是RDD RDD(Resilient Distributed Dataset)叫做分布式数据集,是Spark中最基本的数据抽象.代码中是一个抽象类,它代表一个不可变.可 ...
- Libre 6006 「网络流 24 题」试题库 / Luogu 2763 试题库问题 (网络流,最大流)
Libre 6006 「网络流 24 题」试题库 / Luogu 2763 试题库问题 (网络流,最大流) Description 问题描述: 假设一个试题库中有n道试题.每道试题都标明了所属类别.同 ...
- web开发中的跨域整理
1.springboot通过CROS实现跨域: https://www.cnblogs.com/520playboy/p/7306008.html springboot下各种跨域方式: http:// ...