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经过,怪物需要 ...
随机推荐
- springmvc学习及源码地址
http://jinnianshilongnian.iteye.com/blog/1634096
- Daydreaming Stockbroker(2016 NCPC 贪心)
题目: Gina Reed, the famous stockbroker, is having a slow day at work, and between rounds of solitaire ...
- Bet(The 2016 ACM-ICPC Asia China-Final Contest 思路题)
题目: The Codejamon game is on fire! Fans across the world are predicting and betting on which team wi ...
- WebSocket客户端学习
1. WebSocket是一种网络通讯协议 参考文档:http://www.ruanyifeng.com/blog/2017/05/websocket.html https://github.com/ ...
- DESEncrypt对称加密解密
分享一个很好用的DESEncrypt对称加密解密的类 using System; using System.Security.Cryptography; using System.Text; usin ...
- 使用NamedParameterJdbcTemplate
[在JDBC模板中使用具名参数] 1.在经典的JDBC用法中,SQL参数使用占位符?表示,并且受到位置的限制.定为参数的问题在于,一旦参数的顺序发生变化,就必须改变参数绑定. 2.在Spring JD ...
- 根据判断数组不为空然后取他的值----数组不会为空---只能判断其size是否大于0
private List<Integer> classId=new ArrayList<Integer>(); business.getClassId()!=null 以上为错 ...
- HDU 1059 多重背包问题
问题大意: 有价值1-6的六种物品,分别规定其数目,问是否存在一种方法能使这些物品不拆分就能平均分给两个人 #include <cstdio> #include <cstring&g ...
- Windows中更新python模块的命令
最近写爬虫,突然发现自己的动态的User-Agent用不了了,所以想可能是新版本出来了,旧的版本用不了了,坏掉了. 一时间想不起用什么命令了,网上查了一下,发现很简单,所以记录一下方便以后忘了的时候快 ...
- BAT经典面试题,深入理解Java内存模型JMM
Java 内存模型 Java 内存模型(JMM)是一种抽象的概念,并不真实存在,它描述了一组规则或规范,通过这组规范定义了程序中各个变量(包括实例字段.静态字段和构成数组对象的元素)的访问方式.试图屏 ...