HDU 3345 War Chess

War Chess

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2975    Accepted Submission(s): 750


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

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.
 

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 .*.
 这道题真的是搞得我欲仙欲死,头皮发麻......
题目大意:Y为起始点,MV为移动能量,经过同伴和空地消耗一点能量,经过森林消耗两点,小河三点,如果旁边四格有敌人,能量变为0,而且不能停留在同伴所在的格子,最后输出时将所有能停留的格子变为*

很久之前写过这道题,当时毫无头绪,只知道肯定是要用广搜,后来懂了优先队列,就知道这道题要用优先队列和广搜了

#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
int n,m,mv,startx,starty;
char map[110][110];
int book[110][110];
int dirx[4]={0,1,0,-1};
int diry[4]={1,0,-1,0};
struct Node{
  int x,y,mv;
  friend bool operator < (Node a,Node b){
    return a.mv<b.mv;
  }
};
bool judge(int x,int y){
  if(x<1||y<1||x>n||y>m) return true;
  return false;
}
bool jjudge(int x,int y){
  if(!judge(x-1,y)&&map[x-1][y]=='E') return true;
  if(!judge(x+1,y)&&map[x+1][y]=='E') return true;
  if(!judge(x,y+1)&&map[x][y+1]=='E') return true;
  if(!judge(x,y-1)&&map[x][y-1]=='E') return true;
  return false;
}
void fxxkbfs(){
  Node a,next;
  memset(book,0,sizeof(book));
  priority_queue<Node> que;
  a.x=startx;
  a.y=starty;
  a.mv=mv;
  que.push(a);
  book[a.x][a.y]=1;
  while(!que.empty()){
    a=que.top();
    que.pop();
    if(a.mv<0) continue;
    for(int i=0;i<4;i++){
      next.x=a.x+dirx[i];
      next.y=a.y+diry[i];
      next.mv=a.mv;
      //que.push(next);
      if(judge(next.x,next.y)||book[next.x][next.y]||map[next.x][next.y]=='#'||map[next.x][next.y]=='E') continue;
      if(map[next.x][next.y]=='.') next.mv--;
      else if(map[next.x][next.y]=='T') next.mv-=2;
      else if(map[next.x][next.y]=='R') next.mv-=3;
      else if(map[next.x][next.y]=='P') next.mv--;
      if(next.mv<0) continue;
      if(jjudge(next.x,next.y)) next.mv=0;
      que.push(next);
      book[next.x][next.y]=1;
      if(map[next.x][next.y]!='P') map[next.x][next.y]='*';
    }
  }
}
int main(){
  int t;
  scanf("%d",&t);
  while(t--){
    scanf("%d%d%d",&n,&m,&mv);
    getchar();
    for(int i=1;i<=n;i++){
      for(int j=1;j<=m;j++){
        scanf("%c",&map[i][j]);
        if(map[i][j]=='Y'){
          startx=i;
          starty=j;
        }
      }
      getchar();
    }
    fxxkbfs();
    for(int i=1;i<=n;i++){
      for(int j=1;j<=m;j++){
        printf("%c",map[i][j]);
      }
      putchar('\n');
    }
    putchar('\n');
  }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值