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

HDU 5336 XYZ and Drops

2016-05-21 20:54 525 查看
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5336

题意:有一个n*m的平面,平面上有若干个大小为1~4的水滴,当一个水滴碰到一个水珠后大小加1并且水珠消失,一旦水滴大小超过4之后会变成4个水珠向四个方向炸开,现在(x,y)有一个炸开的水滴,水珠1s移动一格且互不干扰,问t秒之后平面上的水滴状态

思路:蛮简单的bfs,判断水珠碰到水滴后是消失还是炸开,注意一下1个以上的水珠同时到达一个水滴的情况就好

#include <iostream>
#include <cstring>
#include <algorithm>
#include <cstdio>
#include <queue>
using namespace std;

struct Node
{
int x,y,t,f;
}st;

int mp[130][130],sta[130][130];
int sx[130],sy[130];
int chx[5]={1,0,-1,0},chy[5]={0,1,0,-1};
int n,m,k,t;
queue <Node> que;

void bfs()
{
for (int i=0;i<4;i++)
{
int xx=st.x+chx[i];
int yy=st.y+chy[i];
if (xx<=0 || xx>n || yy<=0 || yy>m) continue;
Node nxt;
nxt.x=xx;
nxt.y=yy;
nxt.t=1;
nxt.f=i;
que.push(nxt);
}
while (!que.empty())
{
Node now=que.front();
que.pop();

if (mp[now.x][now.y]!=0)
{
if (mp[now.x][now.y]==4)
{
mp[now.x][now.y]=0;
sta[now.x][now.y]=now.t;
for (int i=0;i<4;i++)
{
int xx=now.x+chx[i];
int yy=now.y+chy[i];
if (xx<=0 || xx>n || yy<=0 || yy>m) continue;
Node nxt;
nxt.x=xx;
nxt.y=yy;
nxt.t=now.t+1;
nxt.f=i;
if (nxt.t<=t)
que.push(nxt);
}
}
else
{
mp[now.x][now.y]++;
}
}
else
{
int xx=now.x+chx[now.f];
int yy=now.y+chy[now.f];
if (now.t==sta[now.x][now.y]) continue;
if (xx<=0 || xx>n || yy<=0 || yy>m) continue;
Node nxt;
nxt.x=xx;
nxt.y=yy;
nxt.t=now.t+1;
nxt.f=now.f;
if (nxt.t<=t)
que.push(nxt);
}

}
}

int main()
{

while (scanf("%d%d%d%d",&n,&m,&k,&t)!=EOF)
{
while (!que.empty())
que.pop();
memset(mp,0,sizeof(mp));
memset(sta,0,sizeof(sta));
for (int i=0;i<k;i++)
{
int x,y,num;
scanf("%d%d%d",&x,&y,&num);
sx[i]=x;
sy[i]=y;
mp[x][y]=num;
}
scanf("%d%d",&st.x,&st.y);
bfs();
for (int i=0;i<k;i++)
{
int xx=sx[i];
int yy=sy[i];
if (mp[xx][yy]!=0)
{
printf("1 %d\n",mp[xx][yy]);
}
else
{
printf("0 %d\n",sta[xx][yy]);
}
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: