您的位置:首页 > 运维架构

Hdu 5336 XYZ and Drops (bfs 模拟)

2015-07-30 21:04 435 查看
题目链接:

  Hdu 5336 XYZ and Drops

题目描述:

  有一个n*m的格子矩阵,在一些小格子里面可能会有一些水珠,每个小水珠都有一个size。现在呢,游戏开始咯,在一个指定的空的小格子里面有一个将要向四周爆裂的水珠,在下一面分别向上,下,左,右四个方向发射一个小水滴,(小水滴与水珠同,小水滴没有size),当小水滴走向一个格子,这个格子如果是空或者有其他的小水滴同时走到这个格子的情况下,对小水滴的运动轨迹是不影响的。但是遇到水珠的话,小水滴就会被吸收,水珠每次吸收一个小水滴size会增加1。为了万物的平衡,水珠size大于4的话就会向四周爆裂成为小水滴。问T时间后每个水珠的状态。

解题思路:

  就是bfs模拟一下小水滴运动的状态就ok了,比赛的时候一直卡题意。

#include <queue>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int maxn = 110;
int dir[4][2] = {1,0, -1,0, 0,1, 0,-1};
struct node
{//坐标,运动方向,运动时间
int x, y, dir, t;
};
int maps[2][maxn][maxn], point[maxn][2];
int r, c, x, y, t;

void bfs ()
{
queue <node> Q;
node p, q;
p.x = x;
p.y = y;
p.dir = 4;
p.t = 0;
Q.push (p);
while (!Q.empty())
{
p = Q.front ();
Q.pop ();
if (p.t >= t)
return ;
if (p.dir == 4)
{
for (int i=0; i<4; i++)
{
q.x = p.x + dir[i][0];
q.y = p.y + dir[i][1];
q.dir = i;
q.t = p.t + 1;
if (0>=q.x||q.x>r || 0>=q.y||q.y>c)
continue;
if (maps[1][q.x][q.y] == q.t)
continue;
if ( !maps[0][q.x][q.y] )
Q.push (q);
else
{
maps[0][q.x][q.y] ++;
if (maps[0][q.x][q.y] > 4)
{
maps[1][q.x][q.y] = q.t;
maps[0][q.x][q.y] = 0;
q.dir = 4;
Q.push (q);
}
}
}
}
else
{
q.x = p.x + dir[p.dir][0];
q.y = p.y + dir[p.dir][1];
q.dir = p.dir;
q.t = p.t + 1;
if (0>=q.x||q.x>r || 0>=q.y||q.y>c)
continue;
if (maps[1][q.x][q.y] == q.t)
continue;
if ( !maps[0][q.x][q.y] )
Q.push (q);
else
{
maps[0][q.x][q.y] ++;
if (maps[0][q.x][q.y] > 4)
{
maps[1][q.x][q.y] = q.t;
maps[0][q.x][q.y] = 0;
q.dir = 4;
Q.push (q);
}
}
}

}
}
int main ()
{
int n, s;
while (scanf ("%d %d %d %d", &r, &c, &n, &t) != EOF)
{
memset (maps, 0, sizeof(maps));
for (int i=0; i<n; i++)
{
scanf ("%d %d %d", &x, &y, &s);
point[i][0] = x;
point[i][1] = y;
maps[0][x][y] = s;
}
scanf ("%d %d", &x, &y);
bfs ();
for (int i=0; i<n; i++)
{
x = point[i][0];
y = point[i][1];
if (maps[0][x][y] == 0)
printf ("0 %d\n", maps[1][x][y]);
else
printf ("1 %d\n", maps[0][x][y]);
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: