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.
![HDU - 3345 War Chess 广搜+优先队列 HDU - 3345 War Chess 广搜+优先队列](https://image.shishitao.com:8440/aHR0cDovL2Jic21heC5pa2FmYW4uY29tL3N0YXRpYy9MM0J5YjNoNUwyaDBkSEJ6TDNacUxqWTVabUV1WTI0dllUZzNNekUyTnprek5EVTVORFU1WWpNMU5EYzVZMkk1TW1aaVlXTTVZakkvZGoweE5UVXlOekEwT0Rjdy5qcGc%3D.jpg?w=700&webp=1)
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' 表示这个点可以走, ...
随机推荐
-
quartz配置文件详解
quartz配置文件详解(转载) quartz学习总结: 一.关于job: 用Quartz的行话讲,作业是一个执行任务的简单Java类.任务可以是任何Java代码.只需你实现org.qu ...
-
[转]Objective-c中@interface、@implementation、@protocal
原处:http://blog.csdn.net/l271640625/article/details/8393531 以下Objective-c简称OC 从事java开发的程序员们都知道,在java中 ...
-
workerman 的回调函数
接下来,记录一下workerman 的回调函数 <?php /** * Created by PhpStorm. * User: zeopean * Date: 2016-08-26 * Tim ...
-
mORMot 数据库操作
程序中要使用数据库,首先是引用SynCommons, SynDB单元,根据不同的数据库类型,简单举几个例子: 1 使用Access数据库,引用SynCommons, SynDB,SynOleDb三个单 ...
-
3 javascript
3 javascript javascript基础 html: 负责了一个页面的结构. css: 负责了一个页面的样式. javascript: 负责与用户进行交互. 1997年欧洲的计算机 ...
-
Java集合类之Hashtable
package com.test; import java.util.*; public class Demo7_3 { public static void main(String[] args) ...
-
Maven导入项目时报错 Could not calculate build plan
Could not calculate build plan: Plugin org.apache.maven.plugins:maven-war-plugin:2.2 or one of its d ...
-
2017-9-13-Linux移植:u-boot的移植
1.u-boot下载地址:http://ftp.denx.de/pub/u-boot/ 2.Linux环境下使用tar命令解压压缩包:tar -xzvf file.tar.gz tar -xvf fi ...
-
ES6 数组方法拓展
ES6 数组方法拓展 1.Array.from() Array.from方法用于将两类对象转为真正的数组:类似数组的对象(array-like object)和可遍历(iterable)的对象(包括E ...
-
对string 的操作
相信使用过MFC编程的朋友对CString这个类的印象应该非常深刻吧?的确,MFC中的CString类使用起来真的非常的方便好用.但是如果离开了MFC框架,还有没有这样使用起来非常方便的类呢?答案是肯 ...