HDU 3345 War Chess (优先队列)
2015-04-19 16:16
423 查看
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3345
题意:某人最开始在Y点,拥有mv的能量,如果下一格是同伴或者空地会消耗一点能量,如果是森林则消耗两点能量,小河会消耗三点,如果该点四周存在敌人,则能量清零,而且不能停留在同伴的格子,将所能走到的所有格子全部变为‘*’,输出最后的图
思路:使用优先队列进行BFS
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <string>
#include <set>
#include <utility>
#include <functional>
#pragma comment (linker, "/STACK:1024000000,1024000000")
using namespace std;
const int maxn = 110;
const int inf = 0x3f3f3f3f;
char s[maxn][maxn];
int vis[maxn][maxn];
int n, m, mv;
int sx, sy;
int dx[4] = { -1, 1, 0, 0};
int dy[4] = {0, 0, -1, 1};
struct node
{
int x, y, v;
node(int x = 0, int y = 0, int v = 0) : x(x), y(y), v(v) {}
bool operator < (const node &rhs) const
{
return v < rhs.v;
}
};
bool ok(int x, int y)
{
if (x >= 0 && x < n && y >= 0 && y < m) return true;
return false;
}
void bfs()
{
memset(vis, 0, sizeof(vis));
priority_queue <node> que;
que.push(node(sx, sy, mv));
while (!que.empty())
{
node cur = que.top();
que.pop();
int x = cur.x, y = cur.y, v = cur.v;
if (s[x][y] != 'P' && (x != sx || y != sy))
s[x][y] = '*';
if (v <= 0) continue;
for (int i = 0; i < 4; i++)
{
int nx = x + dx[i];
int ny = y + dy[i];
int nv = v;
if (!ok(nx, ny) || vis[nx][ny] || s[nx][ny] == '#' || s[nx][ny] == 'E')
continue;
if (s[nx][ny] == '.') nv--;
if (s[nx][ny] == 'P') nv--;
if (s[nx][ny] == 'T') nv -= 2;
if (s[nx][ny] == 'R') nv -= 3;
if (nv < 0) continue;
for (int j = 0; j < 4; j++)
{
int ex = nx + dx[j];
int ey = ny + dy[j];
if (!ok(ex, ey)) continue;
if (s[ex][ey] == 'E')
{
nv = 0;
break;
}
}
que.push(node(nx, ny, nv));
vis[nx][ny] = 1;
}
}
}
int main()
{
int t;
scanf("%d", &t);
while (t--)
{
scanf("%d%d%d", &n, &m, &mv);
for (int i = 0; i < n; i++)
{
scanf("%s", s[i]);
for (int j = 0; j < m; j++)
if (s[i][j] == 'Y')
sx = i, sy = j;
}
bfs();
for (int i = 0; i < n; i++)
printf("%s\n", s[i]);
printf("\n");
}
return 0;
}
题意:某人最开始在Y点,拥有mv的能量,如果下一格是同伴或者空地会消耗一点能量,如果是森林则消耗两点能量,小河会消耗三点,如果该点四周存在敌人,则能量清零,而且不能停留在同伴的格子,将所能走到的所有格子全部变为‘*’,输出最后的图
思路:使用优先队列进行BFS
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <string>
#include <set>
#include <utility>
#include <functional>
#pragma comment (linker, "/STACK:1024000000,1024000000")
using namespace std;
const int maxn = 110;
const int inf = 0x3f3f3f3f;
char s[maxn][maxn];
int vis[maxn][maxn];
int n, m, mv;
int sx, sy;
int dx[4] = { -1, 1, 0, 0};
int dy[4] = {0, 0, -1, 1};
struct node
{
int x, y, v;
node(int x = 0, int y = 0, int v = 0) : x(x), y(y), v(v) {}
bool operator < (const node &rhs) const
{
return v < rhs.v;
}
};
bool ok(int x, int y)
{
if (x >= 0 && x < n && y >= 0 && y < m) return true;
return false;
}
void bfs()
{
memset(vis, 0, sizeof(vis));
priority_queue <node> que;
que.push(node(sx, sy, mv));
while (!que.empty())
{
node cur = que.top();
que.pop();
int x = cur.x, y = cur.y, v = cur.v;
if (s[x][y] != 'P' && (x != sx || y != sy))
s[x][y] = '*';
if (v <= 0) continue;
for (int i = 0; i < 4; i++)
{
int nx = x + dx[i];
int ny = y + dy[i];
int nv = v;
if (!ok(nx, ny) || vis[nx][ny] || s[nx][ny] == '#' || s[nx][ny] == 'E')
continue;
if (s[nx][ny] == '.') nv--;
if (s[nx][ny] == 'P') nv--;
if (s[nx][ny] == 'T') nv -= 2;
if (s[nx][ny] == 'R') nv -= 3;
if (nv < 0) continue;
for (int j = 0; j < 4; j++)
{
int ex = nx + dx[j];
int ey = ny + dy[j];
if (!ok(ex, ey)) continue;
if (s[ex][ey] == 'E')
{
nv = 0;
break;
}
}
que.push(node(nx, ny, nv));
vis[nx][ny] = 1;
}
}
}
int main()
{
int t;
scanf("%d", &t);
while (t--)
{
scanf("%d%d%d", &n, &m, &mv);
for (int i = 0; i < n; i++)
{
scanf("%s", s[i]);
for (int j = 0; j < m; j++)
if (s[i][j] == 'Y')
sx = i, sy = j;
}
bfs();
for (int i = 0; i < n; i++)
printf("%s\n", s[i]);
printf("\n");
}
return 0;
}
相关文章推荐
- HDU 3345 war chess(bfs + 优先队列)
- HDU 3345 War Chess
- hdu 3345 War Chess(BFS+模拟)
- hdu 3345 war chess
- hdu 3345 War Chess(BFS 判断条件稍多,有陷阱)
- hdu 3345 War Chess(bfs)
- HDU - 3345 - War Chess
- hdu 3345 War Chess (bfs+优先队列)
- hdu-3345-War Chess(bfd,条件众多)
- hdu 3345 War Chess
- hdu 3345——War Chess
- hdoj 3345 War Chess (搜索,优先队列)
- HDU 3345 - War Chess
- hdu 3345 War Chess(广搜,用不用优先队列都可以)
- hdu 3345 War chess(bfs)
- HDU 3345 War Chess (BFS)
- HDU - 3345 War Chess (记忆化搜索bfs+优先队列)
- HDU 5884 Sort(二分+优先队列)
- hdu 4544 湫湫系列故事——消灭兔子(贪心+优先队列)
- HDU1180 诡异的楼梯 广搜 优先队列