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. ...
随机推荐
- Alpha冲刺——day9
Alpha冲刺--day9 作业链接 Alpha冲刺随笔集 github地址 团队成员 031602636 许舒玲(队长) 031602237 吴杰婷 031602220 雷博浩 031602634 ...
- Redis 总结精讲
本文围绕以下几点进行阐述 1.为什么使用redis2.使用redis有什么缺点3.单线程的redis为什么这么快4.redis的数据类型,以及每种数据类型的使用场景5.redis的过期策略以及内存淘汰 ...
- js 判断是否选中
js的方法 window.onload = function(){ var oDiv = document.getElementById('div1'); alert(oDiv.checked) } ...
- 如何将adoquery中的数据复制到 Ttable 中
Delphi 7.0 控件: adoquery1:Tadoquery table1 :Ttable adoquery1 open 后 如何将数据复制到t ...
- BZOJ2431 HAOI2009逆序对数列(动态规划)
对于排列计数问题一般把数按一个特定的顺序加入排列.这个题做法比较显然,考虑将数从小到大加入排列即可. #include<iostream> #include<cstdio> # ...
- Day23-Model操作,Form操作和序列化操作
参考源出处:http://blog.csdn.net/fgf00/article/details/54629502 1. 搭建环境请参考:http://www.cnblogs.com/momo8238 ...
- luogu2038 [NOIp2014]无线网络发射器选址 (前缀和)
貌似不用做前缀和也能过? #include<bits/stdc++.h> #define pa pair<int,int> #define CLR(a,x) memset(a, ...
- CAP定理为什么只能同时满足两个
Consistency(一致性), 数据一致更新,所有数据变动都是同步的 Availability(可用性), 好的响应性能 Partition tolerance(分区容忍性) 可靠性 在网上看了很 ...
- 调试技巧 ------ printf 的使用技巧
编译器宏:__FUNCTION__,__FILE__,__LINE__ #define __debug #ifdef __debug //#define debug(format,...) print ...
- Spark记录-Scala类和对象
本章将介绍如何在Scala编程中使用类和对象.类是对象的蓝图(或叫模板).定义一个类后,可以使用关键字new来创建一个类的对象. 通过对象可以使用定义的类的所有功能. 下面的图通过一个包含成员变量(n ...