码迷,mamicode.com
首页 > 其他好文 > 详细

HDU 5336——XYZ and Drops——————【广搜BFS】

时间:2015-08-04 00:16:55      阅读:228      评论:0      收藏:0      [点我收藏+]

标签:

XYZ and Drops

Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1250    Accepted Submission(s): 407


Problem Description
XYZ is playing an interesting game called "drops". It is played on a rc 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 (xy), before the first second there is a waterdrop cracking at position (xy). XYZ wants to know each waterdrop‘s status afterT seconds, can you help him?

1r1001c1001n1001T10000
 

 

Input
The first line contains four integers rcn and Tn stands for the numbers of waterdrops at the beginning. 
Each line of the following n lines contains three integers xiyisizei, meaning that the i-th waterdrop is at position (xiyi) and its size is sizei. (1sizei4)
The next line contains two integers xy

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 AiBi
If the i-th waterdrop cracks in T seconds, Ai=0Bi= the time when it cracked. 
If the i-th waterdrop doesn‘t crack in T seconds, Ai=1Bi= 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个水坑,时间T。xi,yi,vi表示水坑的位置和起始水量。x,y表示在这个位置在0时间发生迸射,水坑消失,在上下左右四个方向各自形成1个容量为1的小水滴,且如果不受水坑影响时,每单位时间向迸射方向前进1单位距离。其他的水坑会在水量超过4的时候发生迸射,水坑消失,形成小水滴。小水滴相遇不会出现反应,当小水滴出边界即消失,跟大水坑相遇时,融进大水坑,水坑水量增加1单位,小水滴消失。如果在同一时间有多个小水滴到达该水坑,那么如果超过4容量后,后面同时到达的小水滴就当做是融合在该水坑中,即消失。
 
#include<bits/stdc++.h>
using namespace std;
const int maxn=110;
struct WaterDrops{  //水坑
    int x,y;
    WaterDrops(){}
    WaterDrops(int xx,int yy){
        x=xx,y=yy;
    }
}waterdrops[maxn];
struct Drops{   //水滴
    int x,y;
    int dir,t;
    Drops(){}
    Drops(int xx,int yy,int d,int tt){
        x=xx,y=yy,dir=d,t=tt;
    }
}drops[maxn*5];
int crack[maxn][maxn],Map[maxn][maxn];//记录迸射、水坑容量
int f[4][2]={{0,1},{1,0},{0,-1},{-1,0}};
int r,c,T;
queue<Drops>Q;
bool inside(int x,int y){   //判断是否在边界中
    if(x>=1&&x<=r&&y>=1&&y<=c){
        return true;
    }else{
        return false;
    }
}
void BFS(){
    int xx, yy, tt, d,tx,ty;
    Drops st;
    while(!Q.empty()){
        st=Q.front();
        xx=st.x,yy=st.y,d=st.dir,tt=st.t;
        Q.pop();
        if(Map[xx][yy]){    //如果该位置还有水坑
            Map[xx][yy]++;
            if(Map[xx][yy]>4){
                Map[xx][yy]=0;  //容量置0
                crack[xx][yy]=tt;//记录迸射时间
                for(int i=0;i<4;i++){
                    tx=xx+f[i][0],ty=yy+f[i][1];
                    if(inside(tx,ty)){
                        if(tt+1<=T){
                            Q.push(Drops(tx,ty,i,tt+1));
                        }
                    }
                }
            }
        }else if(crack[xx][yy]!=tt){//如果没有水坑或水坑迸射,且当前小水滴不是跟发生迸射的那个小水滴同时到达
            tx=xx+f[d][0];
            ty=yy+f[d][1];
            if(inside(tx,ty)){
                if(tt+1<=T){
                    Q.push(Drops(tx,ty,d,tt+1));
                }
            }
        }

    }
}
void init(){
    while(!Q.empty())
        Q.pop();
    memset(Map,0,sizeof(Map));
    memset(crack,0,sizeof(crack));
}
int main(){
    int n,xx,yy,val,tx,ty;
    while(scanf("%d%d%d%d",&r,&c,&n,&T)!=EOF){
        init();
        for(int i=1;i<=n;i++){
            scanf("%d%d%d",&xx,&yy,&val);
            waterdrops[i].x=xx,waterdrops[i].y=yy;
            Map[xx][yy]=val;
        }
        scanf("%d%d",&xx,&yy);
        crack[xx][yy]=1;
        for(int i=0;i<4;i++){
            tx=xx+f[i][0];
            ty=yy+f[i][1];
            if(inside(tx,ty)){
                Q.push(Drops(tx,ty,i,1));
            }
        }
        BFS();

        for(int i=1;i<=n;i++){
            tx=waterdrops[i].x,ty=waterdrops[i].y;
            if(Map[tx][ty]==0){
                printf("0 %d\n",crack[tx][ty]);
            }else{
                printf("1 %d\n",Map[tx][ty]);
            }
        }
    }
    return 0;
}

  

HDU 5336——XYZ and Drops——————【广搜BFS】

标签:

原文地址:http://www.cnblogs.com/chengsheng/p/4700784.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!