XYZ and Drops (hdu 5336 bfs)

46 篇文章 0 订阅

XYZ and Drops

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


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 ( x y ), before the first second there is a waterdrop cracking at position ( x y ). XYZ wants to know each waterdrop's status after  T  seconds, can you help him?

1r100 1c100 1n100 1T10000
 

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 . ( 1sizei4 )
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
 

Author
XJZX
 

Source
 

Recommend
wange2014   |   We have carefully selected several similar problems for you:   5338  5337  5335  5334  5333 
 

题意:n个大水滴在r*c的平面上,(x,y)处有一水滴分裂,分成四个小水滴向四个方向前进,其他n个大水滴的初始大小为1~4,若大水滴被小水滴撞到大水滴大小增加一,当大水滴大小超过四时会分裂,同样向四个方向,这样连锁反应,问最后T时刻n个水滴的状态。

思路:bfs,比赛写的时候一个小bug没看出来,思路上的一点漏洞,遗憾。

代码:

#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<vector>
#include<cmath>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<algorithm>
#define DBG printf("Hi\n")
using namespace std;

#define mod 10000007

#define N 100005

typedef __int64 ll;

struct Node
{
    int x,y,t,d;
    bool operator<(const Node &a)const
    {
        return t>a.t;
    }
};

int r,c,n,T;
int num[111][111];//该点大水滴的size
int id[111][111];
int q[111][2];
bool have[111][111];
int out[111][111];
int dir[4][2]={1,0,0,1,-1,0,0,-1};

bool isok(int x,int y)
{
    if (x>=0&&x<r&&y>=0&&y<c) return true;
    return false;
}

int Search(int d,int x,int y,int &X,int &Y)
{
    int ans=0;
    while (isok(x,y)&&!have[x][y])
    {
        x=x+dir[d][0];
        y=y+dir[d][1];
        ans++;
    }
    if (!isok(x,y)) return -1;
    X=x;Y=y;
    return ans+1;
}

void bfs(int x,int y)
{
    Node st,now;
    priority_queue<Node>Q;
    while (!Q.empty()) Q.pop();
    st.x=x;st.y=y;
    st.t=0;
    Q.push(st);
    int cnt=0;
    while (!Q.empty())
    {
        if (cnt>=n+1) break;
        st=Q.top();
        Q.pop();
        if (!have[st.x][st.y]) 
        {   //比赛时这个大括号内的没写,WA
            if (out[st.x][st.y]==st.t) continue;
            int ss=Search(st.d,st.x,st.y,now.x,now.y);
            if (ss==-1) continue;
            now.d=st.d;
            now.t=st.t+ss-1;
            Q.push(now);
            continue;
        }
        if (num[st.x][st.y]+1>4)
        {
            cnt++;
            have[st.x][st.y]=false;
            out[st.x][st.y]=st.t;
            num[st.x][st.y]=0;
//            printf("***%d\n",st.t);
            for (int i=0;i<4;i++)
            {
                int dx=st.x+dir[i][0];
                int dy=st.y+dir[i][1];
                if (!isok(dx,dy)) continue;
                int ss=Search(i,dx,dy,now.x,now.y);
                if (ss==-1)  continue;
                now.t=st.t+ss;
                now.d=i;
//                printf("%d %d %d++\n",now.x,now.y,now.t);
                if (now.t>T) continue;
                Q.push(now);
            }
        }
        else
            num[st.x][st.y]++;
    }
}

int main()
{
    int i,j,x,y,z;
    while (~scanf("%d%d%d%d",&r,&c,&n,&T))
    {
        memset(num,0,sizeof(num));
        memset(id,-1,sizeof(id));
        memset(out,0,sizeof(out));
        memset(have,false,sizeof(have));
        for (i=0;i<n;i++)
        {
            scanf("%d%d%d",&x,&y,&z);
            x--;
            y--;
            num[x][y]=z;
            have[x][y]=true;
            id[x][y]=i;
        }
        scanf("%d%d",&x,&y);
        x--;y--;
        have[x][y]=true;
        num[x][y]=10;
        bfs(x,y);
        for (i=0;i<r;i++)
        {
            for (j=0;j<c;j++)
            {
                if (id[i][j]==-1) continue;
                if (have[i][j])
                    q[id[i][j]][0]=1,q[id[i][j]][1]=num[i][j];
                else
                    q[id[i][j]][0]=0,q[id[i][j]][1]=out[i][j];

            }
        }
        for (i=0;i<n;i++)
        {
            printf("%d %d\n",q[i][0],q[i][1]);
        }
    }
    return 0;
}
/*
4 5 4 6
2 2 4
2 4 2
4 4 4
2 5 4
4 2
*/




  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值