题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=18748 点击打开链接
题目思路:主要是用到优先队列,每次从队列里拿出来的都是步数最大的,其次是判断周围是否有Enemy时要考虑坐标范围是否越界。
Time Limit: 1000MS | Memory Limit: 32768KB | 64bit IO Format: %I64d & %I64u |
Description
War chess is hh's favorite game:
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.
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.
Input
The 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.
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.
Output
Output 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 .*.
//
// main.cpp
// hdu 3345
//
// Created by xjw on 15/5/18.
// Copyright (c) 2015年 xjw. All rights reserved.
//
#include <stdio.h>
#include <queue>
#include <string.h>
#include <algorithm>
using namespace std;
int dx[4]={1,-1,0,0};
int dy[4]={0,0,-1,1};
int p, n, m, MV;
char map[105][105];
int vis[105][105];
char ans[105][105];
typedef struct node{
int x, y, mv;
friend bool operator <(node a,node b)
{
return a.mv<b.mv ;
}//优先队列实现每次从队列里拿出来的都是步数最大的。!
}node;
node lct;
priority_queue <node> q; //优先队列
int enemy(int x, int y)
{
for (int i = 0; i < 4; i++){
int xx = x + dx[i];
int yy = y + dy[i];
if (xx < 0 || yy < 0 || xx >= n || yy >= m) continue;
if (map[xx][yy] == 'E')
return 1;
}
return 0;
}//判断是否周围有敌军还要考虑是否越界
int main()
{
int T;
while (scanf("%d", &T) != EOF){
while (T--){
memset(vis, 0, sizeof(vis));
while (!q.empty()) q.pop();
node start;
scanf("%d%d%d", &n, &m, &MV);
for (int i = 0; i < n; i++){
getchar();
for (int j = 0; j < m; j++){
scanf("%c", &map[i][j]);
ans[i][j] = map[i][j];
if (map[i][j] == 'Y'){start.x = i;start.y = j;}
}
}
vis[start.x][start.y] = 1;
start.mv = MV;
q.push(start);
while (!q.empty()){
node tmp, news;
tmp = q.top(); //优先队列用top();
for (int i = 0; i < 4; i++){
news = tmp;
news.x += dx[i];
news.y += dy[i];
p = 0;
if (news.x < 0 || news.x >= n) continue;
if (news.y < 0 || news.y >= m) continue;
if (enemy(news.x, news.y)) p = 1;
if (news.mv <= 0) continue;
if (vis[news.x][news.y] == 1) continue;
if (map[news.x][news.y] == '#' ) continue;
if (map[news.x][news.y] == 'E' ) continue;
if (map[news.x][news.y] == '.'){
if (news.mv - 1 < 0) continue;
news.mv -= 1;
if (p == 1) news.mv = 0;
q.push(news);
vis[news.x][news.y] = 1;
ans[news.x][news.y] = '*';
}
if (map[news.x][news.y] == 'P'){
if (news.mv - 1 < 0) continue;
news.mv -= 1;
if (p == 1) news.mv = 0;
q.push(news);
vis[news.x][news.y] = 1;
}
if (map[news.x][news.y] == 'T'){
if (news.mv - 2 < 0) continue;
news.mv -= 2;
if (p == 1) news.mv = 0;
q.push(news);
vis[news.x][news.y] = 1;
ans[news.x][news.y] = '*';
}
if (map[news.x][news.y] == 'R'){
if (news.mv - 3 < 0) continue;
news.mv -= 3;
if (p == 1) news.mv = 0;
q.push(news);
vis[news.x][news.y] = 1;
ans[news.x][news.y] = '*';
}
}
q.pop();
}
for (int i = 0; i < n; i++){
for (int j = 0; j < m; j++)
printf("%c", ans[i][j]);
printf("\n");
}
printf("\n");
}
}
}