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)的更多相关文章

  1. 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 ...

  2. ZOJ 649 Rescue(优先队列+bfs)

    Rescue Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Sub ...

  3. 【POJ3635】Full Tank 优先队列BFS

    普通BFS:每个状态只访问一次,第一次入队时即为该状态对应的最优解. 优先队列BFS:每个状态可能被更新多次,入队多次,但是只会扩展一次,每次出队时即为改状态对应的最优解. 且对于优先队列BFS来说, ...

  4. Codeforces 677D - Vanya and Treasure - [DP+优先队列BFS]

    题目链接:http://codeforces.com/problemset/problem/677/D 题意: 有 $n \times m$ 的网格,每个网格上有一个棋子,棋子种类为 $t[i][j] ...

  5. POJ 2449 - Remmarguts' Date - [第k短路模板题][优先队列BFS]

    题目链接:http://poj.org/problem?id=2449 Time Limit: 4000MS Memory Limit: 65536K Description "Good m ...

  6. 【UESTC 482】Charitable Exchange(优先队列+bfs)

    给你n个物品交换,每个交换用r,v,t描述,代表需要用r元的东西花费t时间交换得v元的东西.一开始只有1元的东西,让你求出交换到价值至少为m的最少时间代价.相当于每个交换是一条边,时间为边权,求走到价 ...

  7. cdoj 482 优先队列+bfs

    Charitable Exchange Time Limit: 4000/2000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Othe ...

  8. hdu - 1242 Rescue && hdu - 2425 Hiking Trip (优先队列+bfs)

    http://acm.hdu.edu.cn/showproblem.php?pid=1242 感觉题目没有表述清楚,angel的朋友应该不一定只有一个,那么正解就是a去搜索r,再用普通的bfs就能过了 ...

  9. hdu 1026 Ignatius and the Princess I(优先队列+bfs+记录路径)

    以前写的题了,现在想整理一下,就挂出来了. 题意比较明确,给一张n*m的地图,从左上角(0, 0)走到右下角(n-1, m-1). 'X'为墙,'.'为路,数字为怪物.墙不能走,路花1s经过,怪物需要 ...

随机推荐

  1. Android table布局开发的一个简单的计算器

    结果如图: XML文件如下: <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" ...

  2. block的作用

    ios高效开发--blocks相关   1.替换delegate       如果我们有2个viewController,a和b,当我们从a界面push到b后,在b上面触发了一些事件,这些时间又会影响 ...

  3. [Luogu] P4366 [Code+#4]最短路

    题目背景 在北纬 91° ,有一个神奇的国度,叫做企鹅国.这里的企鹅也有自己发达的文明,称为企鹅文明.因为企鹅只有黑白两种颜色,所以他们的数学也是以二进制为基础发展的. 比如早在 1110100111 ...

  4. java导出word的6种方式(转发)

    来自: http://www.cnblogs.com/lcngu/p/5247179.html 最近做的项目,需要将一些信息导出到word中.在网上找了好多解决方案,现在将这几天的总结分享一下. 目前 ...

  5. buf.readIntBE()

    buf.readIntBE(offset, byteLength[, noAssert]) buf.readIntLE(offset, byteLength[, noAssert]) offset { ...

  6. Linear and Logistic Regression in TensorFlow

    Linear and Logistic Regression in TensorFlow Graphs and sessions TF Ops: constants, variables, funct ...

  7. POJ 1655 Balancing Act && POJ 3107 Godfather

    题目大意: 根据题目的图很好理解意思,就是记录每一个点的balance,例如 i 的balance就是把 i 从这棵树中除去后得到的森林中含有结点数最多 的子树中的节点个数,然后找到所有节点中对应的b ...

  8. HDU 1542 Atlantics 线段树+离散化扫描

    将 x 轴上的点进行离散化,扫描线沿着 y 轴向上扫描 每次添加一条边不断找到当前状态有效边的长度 , 根据这个长度和下一条边形成的高度差得到一块合法的矩形的面积 #include<iostre ...

  9. [HDU3586]Information Disturbing(DP + 二分)

    传送门 题意:给定一个带权无向树,要切断所有叶子节点和1号节点(总根)的联系,每次切断边的费用不能超过上限limit,问在保证总费用<=m下的最小的limit 二分答案,再 DP,看看最终结果是 ...

  10. Python函数基础---参数、变量

    函数:指将一组语句的集合通过一个名字(函数名)封装起来,要想执行这个函数,只需调用其函数名即可. def sayhi( ): # 函数名 print('hello world') sayhi( ) # ...