HDU - 3345 War Chess 广搜+优先队列
In this game, there is an N * M battle map, and every player has his own Moving Val (MV). In each round, every player can move in four directions as long as he has enough MV. To simplify the problem, you are given your position and asked to output which grids you can arrive.
In the map:
'Y' is your current position (there is one and only one Y in the given map).
'.' is a normal grid. It costs you 1 MV to enter in this gird.
'T' is a tree. It costs you 2 MV to enter in this gird.
'R' is a river. It costs you 3 MV to enter in this gird.
'#' is an obstacle. You can never enter in this gird.
'E's are your enemies. You cannot move across your enemy, because once you enter the grids which are adjacent with 'E', you will lose all your MV. Here “adjacent” means two grids share a common edge.
'P's are your partners. You can move across your partner, but you cannot stay in the same grid with him final, because there can only be one person in one grid.You can assume the Ps must stand on '.' . so ,it also costs you 1 MV to enter this grid.
InputThe first line of the inputs is T, which stands for the number of test cases you need to solve.
Then T cases follow:
Each test case starts with a line contains three numbers N,M and MV (2<= N , M <=100,0<=MV<= 65536) which indicate the size of the map and Y's MV.Then a N*M two-dimensional array follows, which describe the whole map.OutputOutput the N*M map, using '*'s to replace all the grids 'Y' can arrive (except the 'Y' grid itself). Output a blank line after each case.Sample Input
5
3 3 100
...
.E.
..Y 5 6 4
......
....PR
..E.PY
...ETT
....TT 2 2 100
.E
EY 5 5 2
.....
..P..
.PYP.
..P..
..... 3 3 1
.E.
EYE
...
Sample Output
...
.E*
.*Y ...***
..**P*
..E*PY
...E**
....T* .E
EY ..*..
.*P*.
*PYP*
.*P*.
..*.. .E.
EYE
.*.
bfs扩展四个方向,消耗mv最少优先级最高,加入优先队列,优先扩展mv最高点。保证*扩展到最远边界。
代码150+。。写出来还蛮有成就感的。。
| Status | Accepted |
|---|---|
| Time | 15ms |
| Memory | 1620kB |
| Length | 3170 |
| Lang | G++ |
| Submitted | 2017-07-21 00:33:46 |
| Shared | |
| RemoteRunId | 21228675 |
#include<stdio.h>
#include<string.h>
#include<queue>
using namespace std;
char a[][];
int b[][];
int t[][]={{,},{,},{-,},{,-}};
struct Node{
int x,y,mv;
friend bool operator<(Node a,Node b)
{
return a.mv<b.mv;
}
}node;
int main()
{
int t1,n,m,mvp,f,i,j,k,l;
priority_queue<Node> q;
scanf("%d",&t1);
while(t1--){
scanf("%d%d%d",&n,&m,&mvp);
for(i=;i<n;i++){
getchar();
scanf("%s",a[i]);
}
memset(b,,sizeof(b));
for(i=;i<n;i++){
for(j=;j<m;j++){
if(a[i][j]=='Y'){
b[i][j]=;
node.x=i;
node.y=j;
node.mv=mvp;
q.push(node);
while(q.size()){
for(k=;k<;k++){
int tx=q.top().x+t[k][];
int ty=q.top().y+t[k][];
if(tx<||ty<||tx>=n||ty>=m) continue;
if(a[tx][ty]=='.'&&b[tx][ty]==){
if(q.top().mv-<) continue;
f=;
for(l=;l<;l++){
int ttx=tx+t[l][];
int tty=ty+t[l][];
if(ttx<||tty<||ttx>=n||tty>=m) continue;
if(a[ttx][tty]=='E'){
b[tx][ty]=;
a[tx][ty]='*';
f=;
continue;
}
}
if(f==) continue;
b[tx][ty]=;
a[tx][ty]='*';
node.x=tx;
node.y=ty;
node.mv=q.top().mv-;
q.push(node);
}
else if(a[tx][ty]=='T'&&b[tx][ty]==){
if(q.top().mv-<) continue;
f=;
for(l=;l<;l++){
int ttx=tx+t[l][];
int tty=ty+t[l][];
if(ttx<||tty<||ttx>=n||tty>=m) continue;
if(a[ttx][tty]=='E'){
b[tx][ty]=;
a[tx][ty]='*';
f=;
continue;
}
}
if(f==) continue;
b[tx][ty]=;
a[tx][ty]='*';
node.x=tx;
node.y=ty;
node.mv=q.top().mv-;
q.push(node);
}
else if(a[tx][ty]=='R'&&b[tx][ty]==){
if(q.top().mv-<) continue;
f=;
for(l=;l<;l++){
int ttx=tx+t[l][];
int tty=ty+t[l][];
if(ttx<||tty<||ttx>=n||tty>=m) continue;
if(a[ttx][tty]=='E'){
b[tx][ty]=;
a[tx][ty]='*';
f=;
continue;
}
}
if(f==) continue;
b[tx][ty]=;
a[tx][ty]='*';
node.x=tx;
node.y=ty;
node.mv=q.top().mv-;
q.push(node);
}
else if(a[tx][ty]=='#'&&b[tx][ty]==){
b[tx][ty]=;
continue;
}
else if(a[tx][ty]=='E'&&b[tx][ty]==){
b[tx][ty]=;
continue;
}
else if(a[tx][ty]=='P'&&b[tx][ty]==){
if(q.top().mv-<=) continue;
f=;
for(l=;l<;l++){
int ttx=tx+t[l][];
int tty=ty+t[l][];
if(ttx<||tty<||ttx>=n||tty>=m) continue;
if(a[ttx][tty]=='E'){
b[tx][ty]=;
f=;
continue;
}
}
if(f==) continue;
b[tx][ty]=;
node.x=tx;
node.y=ty;
node.mv=q.top().mv-;
q.push(node);
}
}
q.pop();
}
}
}
}
for(i=;i<n;i++){
printf("%s\n",a[i]);
}
printf("\n");
}
return ;
}
HDU - 3345 War Chess 广搜+优先队列的更多相关文章
- hdu 3345 War Chess
War Chess Time Limit : 2000/1000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other) Total Sub ...
- hdu 1242:Rescue(BFS广搜 + 优先队列)
Rescue Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other) Total Submis ...
- Combine String HDU - 5707 dp or 广搜
Combine String HDU - 5707 题目大意:给你三个串a,b,c,问a和b是不是恰好能组成c,也就是a,b是不是c的两个互补的子序列. 根据题意就可以知道对于c的第一个就应该是a第一 ...
- HDU 5652(二分+广搜)
题目链接:http://acm.hust.edu.cn/vjudge/contest/128683#problem/E 题目大意:给定一只含有0和1的地图,0代表可以走的格子,1代表不能走的格 子.之 ...
- HDU 1253 (简单三维广搜) 胜利大逃亡
奇葩!这么简单的广搜居然爆内存了,而且一直爆,一直爆,Orz 而且我也优化过了的啊,尼玛还是一直爆! 先把代码贴上睡觉去了,明天再来弄 //#define LOCAL #include <ios ...
- hdu 1175 连连看 (广搜,注意解题思维,简单)
题目 解析见代码 #define _CRT_SECURE_NO_WARNINGS //这是非一般的最短路,所以广搜到的最短的路不一定是所要的路线 //所以应该把所有的路径都搜索出来,找到最短的转折数, ...
- hdu 1495 非常可乐 (广搜)
题目链接 Problem Description 大家一定觉的运动以后喝可乐是一件很惬意的事情,但是seeyou却不这么认为.因为每次当seeyou买了可乐以后,阿牛就要求和seeyou一起分享这一瓶 ...
- HDU 1072 Nightmare (广搜)
题目链接 Problem Description Ignatius had a nightmare last night. He found himself in a labyrinth with a ...
- hdu 1240(三维广搜)
题意: 有一个n*n*n的三维空间. 给你起始坐标和终点坐标.要你从起点到终点,问最少需要多少步走出去.如果走不出去则输出"NO ROUTE". 空间中 'O' 表示这个点可以走, ...
随机推荐
- C递归算法与栈的分析,非全然二叉树遍历分析---ShinePans
对于递归,这里面的分析最好当然是用图形的方式来分析了.这里来总结一下 1.首先对于栈的理解: 先进后出,后进先出 先进后出 2.在进行非全然二叉树的存储之后,我们要做的是对其 ...
- mysql连接超时的问题
使用Hibernate + MySQL数据库开发,链接超时问题: com.mysql.jdbc.CommunicationsException: The last packet successfull ...
- asp.net core 初探 二
今天用@宇内流云大大的jexus 体验一下生产环境的发布,运行. 生产环境: centos 7 jexus 5.8.1 独立版 包含了mono (mono安装真心痛苦……) 开发环境就是昨天的Ubun ...
- IOS版App的控件元素定位
前言 Android版App的控件元素可以通过Android studio自带的工具uiautomatorviewer来协助定位! IOS版App的控件元素可以通过Appium来实现(未实现),或ap ...
- springboot实战--笔记
由于这本书看过一遍,所以这里是二次复习,记录的东西比较少,就不分章节了. 共12章,524页,预计时间是18h 第一章 spring基础: 第二章 spring常用配置: bean的Scope:sin ...
- 按照eslint 规范写代码 [eslint] 'flag' is assigned to itself. (no-self-assign)
按照eslint 规范写代码 [eslint] 'flag' is assigned to itself. (no-self-assign)
- 使用官方Android-support-v7在低版本上使用ActionBarActivity
昨天晚上更新了下Android SDK Manager,发现Extras下的Android Support Library已经更新到19.1了,上网一查原来是sdk\extras\android\su ...
- RequireJS 加载 js 执行顺序
初次接触RequireJS 对文档理解不很透彻,自己通过测试测到的执行顺序: 文档结构: |-amaze | -js | -amazeui.js | -jquery.min.js | -main.js ...
- DDD领域驱动之干货 (一)
说道DDD不得不说传统的架构与DDD的架构区别. 传统的架构不外乎就是三层,而在这三层里面又不断的细分,始终没有达到想要的效果,那么为什么当时还是采用三层. 当然在DDD没有提出的时候三层是大多数人的 ...
- MPEG学习
Mpeg:moving picture experts group 移动图片专家组 导入:Mpeg技术在我理解就是我们对音视频信息的一个输出标准.主要包括MPEG-1.MPEG-2.MPEG-4.MP ...