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' 表示这个点可以走, ...
随机推荐
- UVA 11888 - Abnormal 89's(Manachar)
UVA 11888 - Abnormal 89's option=com_onlinejudge&Itemid=8&page=show_problem&category=524 ...
- 07 http内容压缩
一:http内容压缩 思考: Content-Length在之前的学习中,我们知道,代表返回的主体的长度 但此处,为什么返回的主体长度和content-length不一致呢? 原因在于: Conten ...
- Leetcode 002-Search Insert Position
#Given a sorted array and a target value, return the index if the target is found. If not, return th ...
- Subversion基础:概念、安装、配置和基本操作(转)
本文转载至http://www.cnblogs.com/cokecoffe/archive/2012/06/01/2537130.html 转自:http://www.uml.org.cn/pzgl/ ...
- Geoffrey E. Hinton
https://www.cs.toronto.edu/~hinton/ Geoffrey E. Hinton I am an Engineering Fellow at Google where I ...
- flume topology design . tier num 分层数目
32:+:1 x:1 x<=8 https://flume.apache.org/FlumeUserGuide.html#flume-topology-design Flume topology ...
- selenium2 浏览器版本问题
一.chrome浏览器 chrome浏览器与驱动版本对应关系 ----------ChromeDriver v2.26 (2016-12-09)---------- Supports Chrome v ...
- C++引用详解【转】
本文转载自:http://www.cnblogs.com/gw811/archive/2012/10/20/2732687.html 引用:就是某一变量(目标)的一个别名,对引用的操作与对变量直接操作 ...
- BZOJ 3732 Network —— 最小生成树 + 倍增LCA
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=3732 Description 给你N个点的无向图 (1 <= N <= 15, ...
- CSU - 1550 Simple String —— 字符串
题目链接:http://acm.csu.edu.cn/csuoj/problemset/problem?pid=1550 题解: 1.A+B 与C的交集必须>=n 2.A与C的交集必须>= ...