题目链接: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
#include <iostream>
#include <cstring>
#include <queue>
#include <cstdio>
using namespace std;
int r,c,n,t,tx,ty;
//四个方向
int d[4][2]={0,1,-1,0,0,-1,1,0};
//运动的小水珠的方向,坐标和时间
struct node
{
int dir,x,y,t;
};
//判断是否在边界内
bool Inside(int x,int y)
{
if(x>=1&&x<=r&&y>=1&&y<=c)
return true;
return false;
}
//存储水团的位置
struct drop
{
int x,y;
}store[105];
queue <node> qe;
//map记容量
int map[105][105];
//crack记分解时间
int crack[105][105];
//模拟水珠运动
void bfs()
{
int dir,ti,xx,yy;
node tmp;
while(!qe.empty())
{
//取当前节点
tmp=qe.front();
qe.pop();
tx=tmp.x;
ty=tmp.y;
dir=tmp.dir;
ti=tmp.t;
//如果该点有水团
if(map[tx][ty])
{
//体积加1
map[tx][ty]++;
//大于4,分解
if(map[tx][ty]>4)
{
//记录分解时间
crack[tx][ty]=ti;
map[tx][ty]=0;
for(int i=0;i<4;i++)
{
//四个方向
xx=tx+d[i][0];
yy=ty+d[i][1];
//在边界内
if(Inside(xx,yy))
{
tmp.x=xx;
tmp.y=yy;
tmp.t=ti+1;
tmp.dir=i;
//在时间T内
if(tmp.t<=t)
qe.push(tmp);
}
}
}
}
//该点原本就没有水团或者水团已经分解,但不是刚刚分解的,去除那个已经弹出的水珠的情况
else if(crack[tx][ty]!=ti)
{
xx=tx+d[dir][0];
yy=ty+d[dir][1];
if(Inside(xx,yy))
{
tmp.x=xx;
tmp.y=yy;
tmp.t=ti+1;
tmp.dir=dir;
if(tmp.t<=t)
qe.push(tmp);
}
}
}
}
int main()
{
int x,y,v;
node tmp;
while(~scanf("%d%d%d%d",&r,&c,&n,&t))
{
memset(map,0,sizeof(map));
memset(crack,0,sizeof(crack));
//读入
for(int i=0;i<n;i++)
{
scanf("%d%d%d",&store[i].x,&store[i].y,&v);
map[store[i].x][store[i].y]=v;
}
scanf("%d%d",&x,&y);
//初始点分解
for(int i=0;i<4;i++)
{
tx=x+d[i][0];
ty=y+d[i][1];
if(Inside(tx,ty))
{
tmp.x=tx;
tmp.y=ty;
tmp.t=1;
tmp.dir=i;
qe.push(tmp);
}
}
//模拟
bfs();
//输出
for(int i=0;i<n;i++)
{
if(crack[store[i].x][store[i].y])
printf("0 %d\n",crack[store[i].x][store[i].y]);
else
printf("1 %d\n",map[store[i].x][store[i].y]);
}
}
return 0;
}版权声明:本文为博主原创文章,未经博主允许不得转载。
HDU 5336 XYZ and Drops (模拟+搜索,详解)
原文地址:http://blog.csdn.net/david_jett/article/details/47168525