Three Kingdoms(优先队列+bfs)
http://acm.hdu.edu.cn/showproblem.php?pid=3442
题意:ABCD有各自的攻击力与攻击范围,刘备只能走"C"与".",问刘备从"s" 走到"!"受到的最小攻击力。ps(受过一次攻击的下次将不会再对刘备造成伤害)
思路:用优先队列,每次搜索攻击力最小的,并对当前的状态标记。。。
#include <stdio.h>
#include <iostream>
#include <string.h>
#include <queue>
#include <stdlib.h>
using namespace std;
const int N=;
char Map[N][N];
int a[N][N][],vis[N][N][];
int dir[][] = {{,-},{,},{-,},{,}};
int n,m,sx,sy,ex,ey;
struct node
{
int x,y,hp,state;
friend bool operator < (node a,node b)
{
return b.hp < a.hp;//在这跪了n遍。。要想优先队列里的值从小到大排列,则比较函数应该从大到小排
}
};
bool in(int x,int y)
{
if (x >= &&x <= n&&y >= &&y <= m)
return true;
return false;
}
bool judge(int x1,int y1,int x2,int y2,int c)
{
if(abs(x1-x2)+abs(y1-y2) <= c)
return true;
return false;
} void deal(int x,int y,char ch)
{
if (ch=='A')
{
for (int i = x-; i <= x+; i++)
{
for (int j = y-; j <= y+; j++)
{
if(in(i,j)&&judge(x,y,i,j,))
a[i][j][] = ;
}
}
}
else if (ch=='B')
{
for (int i = x-; i <= x+; i++)
{
for (int j = y-; j <= y+; j++)
{
if (in(i,j)&&judge(x,y,i,j,))
a[i][j][] = ;
}
}
}
else if (ch=='C')
a[x][y][] = ;
else if (ch=='D')
{
for (int i = x-; i <= x+; i++)
{
for (int j = y-; j <= y+; j++)
{
if (in(i,j)&&judge(x,y,i,j,))
a[i][j][] = ;
}
}
}
else if (ch=='E')
{
for (int i = x-; i <= x+; i ++)
{
for (int j = y-; j <= y+; j ++)
{
if (in(i,j)&&judge(x,y,i,j,))
a[i][j][] = ;
}
}
}
}
int bfs()
{
priority_queue<node>q;
while(!q.empty()) q.pop();
q.push((struct node)
{
sx,sy,,
});
vis[sx][sy][] = ;
while(!q.empty())
{
node temp,t = q.top();
q.pop();
printf("%d %d %d\n",t.x,t.y,t.hp);
if (t.x==ex&&t.y==ey)
return t.hp;
for (int i = ; i < ; i++)
{
temp.x = t.x+dir[i][];
temp.y = t.y+dir[i][];
temp.hp = t.hp;
temp.state = t.state;
if (in(temp.x,temp.y)&&(Map[temp.x][temp.y]=='C'||Map[temp.x][temp.y]=='$'||Map[temp.x][temp.y]=='.'||Map[temp.x][temp.y]=='!'))
{
for (int k = ; k < ; k++)
{
if((temp.state&(<<k))==&&a[temp.x][temp.y][k])
{
temp.hp+=a[temp.x][temp.y][k];
temp.state+=(<<k);
}
}
if (!vis[temp.x][temp.y][temp.state])
{
vis[temp.x][temp.y][temp.state] = ;
q.push(temp);
}
}
}
}
return -;
}
int main()
{
int t,o = ;
cin>>t;
while((t--))
{
o++;
cin>>n>>m;
memset(a,,sizeof(a));
memset(vis,,sizeof(vis));
for (int i = ; i <= n; i++)
{
for (int j = ; j <= m; j++)
{
cin>>Map[i][j];
if (Map[i][j]=='A'||Map[i][j]=='B'||Map[i][j]=='C'||Map[i][j]=='D'||Map[i][j]=='E')
deal(i,j,Map[i][j]);
if (Map[i][j]=='$')
{
sx = i;
sy = j;
}
if (Map[i][j]=='!')
{
ex = i;
ey = j;
}
}
}
int ans = bfs();
printf("Case %d: %d\n",o,ans);
}
return ;
}
Three Kingdoms(优先队列+bfs)的更多相关文章
- hdu 1026 Ignatius and the Princess I【优先队列+BFS】
链接: http://acm.hdu.edu.cn/showproblem.php?pid=1026 http://acm.hust.edu.cn/vjudge/contest/view.action ...
- ZOJ 649 Rescue(优先队列+bfs)
Rescue Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Sub ...
- 【POJ3635】Full Tank 优先队列BFS
普通BFS:每个状态只访问一次,第一次入队时即为该状态对应的最优解. 优先队列BFS:每个状态可能被更新多次,入队多次,但是只会扩展一次,每次出队时即为改状态对应的最优解. 且对于优先队列BFS来说, ...
- Codeforces 677D - Vanya and Treasure - [DP+优先队列BFS]
题目链接:http://codeforces.com/problemset/problem/677/D 题意: 有 $n \times m$ 的网格,每个网格上有一个棋子,棋子种类为 $t[i][j] ...
- POJ 2449 - Remmarguts' Date - [第k短路模板题][优先队列BFS]
题目链接:http://poj.org/problem?id=2449 Time Limit: 4000MS Memory Limit: 65536K Description "Good m ...
- 【UESTC 482】Charitable Exchange(优先队列+bfs)
给你n个物品交换,每个交换用r,v,t描述,代表需要用r元的东西花费t时间交换得v元的东西.一开始只有1元的东西,让你求出交换到价值至少为m的最少时间代价.相当于每个交换是一条边,时间为边权,求走到价 ...
- cdoj 482 优先队列+bfs
Charitable Exchange Time Limit: 4000/2000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Othe ...
- hdu - 1242 Rescue && hdu - 2425 Hiking Trip (优先队列+bfs)
http://acm.hdu.edu.cn/showproblem.php?pid=1242 感觉题目没有表述清楚,angel的朋友应该不一定只有一个,那么正解就是a去搜索r,再用普通的bfs就能过了 ...
- hdu 1026 Ignatius and the Princess I(优先队列+bfs+记录路径)
以前写的题了,现在想整理一下,就挂出来了. 题意比较明确,给一张n*m的地图,从左上角(0, 0)走到右下角(n-1, m-1). 'X'为墙,'.'为路,数字为怪物.墙不能走,路花1s经过,怪物需要 ...
随机推荐
- Ztree加载完成默认选中根节点右侧生成表格
需求:页面加载完成之后,默认选中ztree的根节点,并执行其点击方法,右侧生成表格: 效果:如下图所示: 思路:在节点点击事件clickNode方法中根据节点的部门code查询这个部门下的所有员工,并 ...
- ConcurrentHashMap笔记
概览: 内部存储的数据结构为:数组+链表+红黑树,图示: 重要的属性(内部类): //存放元素的数组 transient volatile Node<K,V>[] table; //数组中 ...
- Go:内置函数
一.内置函数 close // 主要用来关闭channel len // 用来求长度,比如string.array.slice.map.channel new // 用来分配内存,主要用来分配值类型, ...
- 省市区json结构
[ { "label": "北京市", "value": "北京市", "children": [ ...
- AOP基础
[Why AOP ?] 1.代码混乱:越来越多的非业务需求(日志和验证等)加入后,原有的业务方法急剧膨胀.每个方法在处理核心逻辑的同时还必须兼顾其他多个关注点. 2.代码分散:以日志需求为例,知识为了 ...
- hibernate的QBC查询之Criteria用法
//return (DeliverCost) super.getSession().createCriteria(getMyClass()).add(Restrictions.eq("isd ...
- reids桌面管理工具:RedisDesktopManager下载、使用
概要:一款好用的Redis桌面管理工具,支持命令控制台操作,以及常用,查询key,rename,delete等操作. 下载软件,请点击下面链接,进入下载页,选择对应版本: https://redisd ...
- 20180725关于quartz的初识
请参照: https://www.ibm.com/developerworks/cn/opensource/os-cn-quartz/ https://www.w3cschool.cn/quartz_ ...
- redis sentinel集群配置及haproxy配置
ip分布情况: sentinel-1/redis 主 10.11.11.5 sentinel-2/redis 从 10.11.11.7 sentinel-3/redis 从 10.11.11.8 ha ...
- 【ora10,4】oracle后台进程介绍:
一.SMON(System MONitor) 系统监控进程: 在数据库启动过程中,SMON排在CKPT进程之后,在Oracle9i中排在第六号的位置: PMON started with p ...