题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5336
4 4 5 10 2 1 4 2 3 3 2 4 4 3 1 2 4 3 4 4 4
0 5 0 3 0 2 1 3 0 1
题意:
在一个n * m 的矩阵中,一共有 num 个大水滴,求在经过了 T 秒后这 num 个大水滴的状态!
在 0 秒时 (x,y) 位置有个水滴开始爆炸,然后生成了四个小水滴分别向上下左右移动,每个大水滴的 Size > 4 就会爆炸,生成向四周移动的小水滴!
PS:
BFS,把每个小水滴压入优先队列里;
代码如下:
#include <cstdio>
#include <cstring>
#include <queue>
#include <algorithm>
#include <iostream>
using namespace std;
#define maxn 1017
int dx[4] = {-1,1,0,0};
int dy[4] = {0,0,-1,1};
struct node
{
int x, y;
int dir;
int t;
node() {}
node(int _x, int _y, int _dir, int _t)
{
x = _x,y = _y, dir = _dir, t = _t;
}
bool operator < (const node &time)const
{
return t > time.t;
}
};
struct NODE
{
int x, y;
} a[maxn];
int n, m, T;
int Size[maxn][maxn];
int time[maxn][maxn];
int judge(int x, int y, int t)
{
if(x<=0 || x>n || y<=0 || y>m || time[x][y]== t+1)
{
return 1;
}
return 0;
}
void init()
{
memset(Size, 0, sizeof(Size));
memset(time, -1,sizeof(time));
}
void BFS(int x, int y)
{
priority_queue<node> q;
for(int i = 0; i < 4; i++)
{
q.push(node(x, y, i, 0));
}
while(!q.empty())
{
node tt = q.top();
if(tt.t >= T)
return ;
q.pop();
int tx = tt.x + dx[tt.dir];
int ty = tt.y + dy[tt.dir];
if(judge(tx, ty, tt.t))
continue;
if(Size[tx][ty] != 0)
{
if(Size[tx][ty] == 4)
{
Size[tx][ty] = 0;
time[tx][ty] = tt.t+1;
for(int i = 0; i < 4; i++)
{
q.push(node(tx, ty, i, tt.t+1));
}
}
else
{
Size[tx][ty]++;
}
}
else
{
q.push(node(tx, ty, tt.dir, tt.t+1));
}
}
}
int main()
{
int num;
while(~scanf("%d%d%d%d",&n,&m,&num,&T))
{
init();
int _size;
for(int i = 0; i < num; i++)
{
scanf("%d%d%d",&a[i].x,&a[i].y,&_size);
Size[a[i].x][a[i].y] = _size;
}
int x, y;
scanf("%d%d",&x,&y);
BFS(x, y);
for(int i = 0; i < num; i++)
{
if(time[a[i].x][a[i].y] != -1)//爆炸
{
printf("0 %d\n",time[a[i].x][a[i].y]);
}
else
{
printf("1 %d\n",Size[a[i].x][a[i].y]);
}
}
}
return 0;
}
版权声明:本文为博主原创文章,未经博主允许不得转载。
HDU 5336 XYZ and Drops(模拟十滴水游戏 BFS啊)
原文地址:http://blog.csdn.net/u012860063/article/details/47205397