您的位置:首页 > 其它

hdoj 3345 War Chess (搜索,优先队列)

2016-09-23 20:03 253 查看


War Chess

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 2332    Accepted Submission(s): 561


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

 

Author

shǎ崽

 

Source

HDU2010省赛集训队选拔赛(校内赛)

因为走每个点消耗的步数不同,所以就需要使用优先队列。

代码:

#include<bits/stdc++.h>
using namespace std;
const int maxn = 105;
char pic[maxn][maxn];
bool book[maxn][maxn];
int sx, sy, row, col, mv;

struct node
{
int x, y, setp;
node() {}
node(int xx, int yy, int ss): x(xx), y(yy), setp(ss) {}
bool operator < (const node &a) const
{
return setp < a.setp;
}
};

bool judge(int x, int y)
{
return (x >= 0 && x < row && y >= 0 && y < col);
}

void bfs()
{
memset(book, 0, sizeof(book));
int next[4][2] = {0, 1, 0, -1, 1, 0, -1, 0};
priority_queue<node> pq;
pq.push(node(sx, sy, mv));
book[sx][sy] = 1;
while(!pq.empty())
{
node u = pq.top(); pq.pop();
int x = u.x;
int y = u.y;
int setp = u.setp;
if(pic[x][y] != 'P' && pic[x][y] != 'Y') pic[x][y] = '*';
if(setp <= 0) continue;
for(int i = 0; i < 4; i++)
{
int tx = x+next[i][0];
int ty = y+next[i][1];
int tsetp = setp;
if(!judge(tx, ty) || book[tx][ty] || pic[tx][ty] == '#' || pic[tx][ty] == 'E') continue;
if(pic[tx][ty] == '.' || pic[tx][ty] == 'P') tsetp--;
if(pic[tx][ty] == 'T') tsetp -= 2;
if(pic[tx][ty] == 'R') tsetp -= 3;
if(tsetp < 0) continue;
//判断周围是否有敌军
for(int i = 0; i < 4; i++)
{
int ex = tx+next[i][0];
int ey = ty+next[i][1];
if(judge(ex, ey) && pic[ex][ey] == 'E')
{
tsetp = 0;
break;
}
}
pq.push(node(tx, ty, tsetp));
book[tx][ty] = 1;
}
}
}

int main(void)
{
int t;
cin >> t;
while(t--)
{
memset(pic, 0, sizeof(pic));
scanf("%d%d%d", &row, &col, &mv);
for(int i = 0; i < row; i++)
for(int j = 0; j < col; j++)
{
scanf(" %c", &pic[i][j]);
if(pic[i][j] == 'Y') sx = i, sy = j;
}
bfs();
for(int i = 0; i < row; i++)
printf("%s\n", pic[i]);
printf("\n");
}
return 0;
}


 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息