XYZ and Drops
Time Limit: 3000/1500 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 2151 Accepted Submission(s): 741
Problem Description
XYZ is playing an interesting game called "drops". It is played on a r∗c grid. Each grid cell is either empty, or occupied by a waterdrop. Each waterdrop has a property "size". The waterdrop cracks when its size is larger than 4, and produces 4 small drops moving towards 4 different directions (up, down, left and right).
In every second, every small drop moves to the next cell of its direction. It is possible that multiple small drops can be at same cell, and they won't collide. Then for each cell occupied by a waterdrop, the waterdrop's size increases by the number of the small drops in this cell, and these small drops disappears.
You are given a game and a position (x, y), before the first second there is a waterdrop cracking at position (x, y). XYZ wants to know each waterdrop's status after Tseconds, can you help him?
1≤r≤100, 1≤c≤100, 1≤n≤100, 1≤T≤10000
Input
The first line contains four integers r, c, n and T. n stands for the numbers of waterdrops at the beginning.
Each line of the following n lines contains three integers xi, yi, sizei, meaning that the i-th waterdrop is at position (xi, yi) and its size is sizei. (1≤sizei≤4)
The next line contains two integers x, y.
It is guaranteed that all the positions in the input are distinct.
Multiple test cases (about 100 cases), please read until EOF (End Of File).
Output
n lines. Each line contains two integers Ai, Bi:
If the i-th waterdrop cracks in T seconds, Ai=0, Bi= the time when it cracked.
If the i-th waterdrop doesn't crack in T seconds, Ai=1, Bi= its size after T seconds.
Sample Input
4 4 5 10 2 1 4 2 3 3 2 4 4 3 1 2 4 3 4 4 4
Sample Output
0 5 0 3 0 2 1 3 0 1
题意:给你一个r*c行的矩阵,有n个水坑,每个水坑都有1~4滴水,给你时间T,给你初始爆炸点(x,y),这个爆炸点在第一秒会向四个方向炸出4个水滴,水滴每秒移动一个格子(不可以改变方向)。如果水滴遇到水坑,就会融入到水坑里。如果水坑的水滴数超过4,则这个水坑就会爆炸(和爆炸点一样会炸出水滴),让你求T秒之后每个水坑所含的水滴数,如果已经爆炸就输出它爆炸的时间。
思路:按照题意BFS即可。注意同一时间到达水坑的水滴,如果这时候水坑刚好爆炸了那么这些水滴都相当于消失了。
代码:
#include <bits/stdc++.h>
#define mo 1000000007
#define ll long long
using namespace std;
const int maxn=110;
int n,m,T,num;
int bomb[maxn][maxn],c[maxn][maxn];
int dxy[]={0,1,0,-1,1,0,-1,0};
struct keng
{
int x,y;
keng(){}
keng(int a,int b){x=a;y=b;}
}a[maxn];
struct node
{
int x,y;
int fx,t;
node(){}
node(int a,int b,int c,int d){x=a;y=b;fx=c;t=d;}
}k;
queue<node>p,q;
bool jud(int x,int y)
{
if(x<1||x>n||y<1||y>m)return 0;
return 1;
}
void bfs()
{
while(!q.empty())
{
node k=q.front();q.pop();
if(c[k.x][k.y])
{
c[k.x][k.y]++;
if(c[k.x][k.y]>4)
{
c[k.x][k.y]=0;bomb[k.x][k.y]=k.t;
for(int i=0;i<4;i++)
{
int x=k.x+dxy[i],y=k.y+dxy[i+4];
if(jud(x,y))
{
if(k.t+1<=T)q.push(node(x,y,i,k.t+1));
}
}
}
}
else if(k.t!=bomb[k.x][k.y])//这里是关键
{
int x=k.x+dxy[k.fx],y=k.y+dxy[k.fx+4];
if(jud(x,y))
{
if(k.t+1<=T)q.push(node(x,y,k.fx,k.t+1));
}
}
}
}
int main()
{
int cas=1;
while(scanf("%d%d%d%d",&n,&m,&num,&T)!=EOF)
{
memset(c,0,sizeof(c));
memset(bomb,0,sizeof(bomb));
for(int i=1;i<=num;i++)
{
int cc;
scanf("%d%d%d",&a[i].x,&a[i].y,&cc);
c[a[i].x][a[i].y]=cc;
}
int xx,yy;
scanf("%d%d",&xx,&yy);
bomb[xx][yy]=1;
q=p;
for(int i=0;i<4;i++)
{
int x=xx+dxy[i],y=yy+dxy[i+4];
if(jud(x,y))
{
q.push(node(x,y,i,1));
}
}
bfs();
for(int i=1;i<=num;i++)
if(!c[a[i].x][a[i].y]) printf("0 %d\n",bomb[a[i].x][a[i].y]);
else printf("1 %d\n",c[a[i].x][a[i].y]);
}
return 0;
}