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

XYZ and Drops (hdu 5336 bfs)

时间:2015-07-31 10:35:19      阅读:119      评论:0      收藏:0      [点我收藏+]

标签:xyz and drops   hdu 5336   bfs   

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 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 (xy), before the first second there is a waterdrop cracking at position (xy). XYZ wants to know each waterdrop‘s status after T 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
 

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
*/




版权声明:本文为博主原创文章,未经博主允许不得转载。

XYZ and Drops (hdu 5336 bfs)

标签:xyz and drops   hdu 5336   bfs   

原文地址:http://blog.csdn.net/u014422052/article/details/47166091

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