hdu5336 多校聯合第四場1010 模擬+bfs優先佇列

life4711發表於2015-07-31

http://acm.hdu.edu.cn/showproblem.php?pid=5336

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 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
/**
hdu5336 多校聯合第四場1010   模擬+bfs
題目大意:很像一個遊戲“十滴水”,在一個n*m的棋盤裡有N個點有一些水滴,含水量為1~4,如果一滴水多於4則該水滴會向四個方向放射量為1
          的水,並且原位置水滴消逝,放射的水運動速度為1格/s,最開始在xy位置有一滴水崩裂,問T s時各初始狀態的水滴的狀態是什麼
解題思路:因為水滴的迸射會產生連鎖反應,因此我們用優先佇列維護,bfs。
          值得一提的是如果2或以上的水同時到達一水滴,並該水滴迸射那麼這些水滴同時消失。
*/
#include <string.h>
#include <algorithm>
#include <iostream>
#include <stdio.h>
#include <queue>
using namespace std;
int n,m,N,T,num[1005][2],sta[1005][1005],time[1005][1005];
int dx[][2]= {1,0,0,1,-1,0,0,-1};
struct note
{
    int x,y,d,t;
    note() {}
    note(int _x,int _y,int _d,int _t)
    {
        x=_x,y=_y,d=_d,t=_t;
    }
    bool operator < (const note &other)const
    {
        return t>other.t;
    }
};

void bfs(int x,int y)
{
    memset(time,-1,sizeof(time));
    priority_queue<note> q;
    q.push(note(x,y,0,0));
    q.push(note(x,y,1,0));
    q.push(note(x,y,2,0));
    q.push(note(x,y,3,0));
    while(!q.empty())
    {
        note cnt=q.top();
        if(cnt.t>=T)return;
        q.pop();
        x=cnt.x+dx[cnt.d][0];
        y=cnt.y+dx[cnt.d][1];
        if(x<=0||x>n||y<=0||y>m||time[x][y]==cnt.t+1)continue;
        if(sta[x][y]!=0)
        {
            if(sta[x][y]==4)
            {
                sta[x][y]=0;
                time[x][y]=cnt.t+1;
                q.push(note(x,y,0,cnt.t+1));
                q.push(note(x,y,1,cnt.t+1));
                q.push(note(x,y,2,cnt.t+1));
                q.push(note(x,y,3,cnt.t+1));
            }
            else
            {
                sta[x][y]++;
            }
        }
        else
            q.push(note(x,y,cnt.d,cnt.t+1));
    }
}
int main()
{
    while(~scanf("%d%d%d%d",&n,&m,&N,&T))
    {
        memset(sta,0,sizeof(sta));
        for(int i=0; i<N; i++)
        {
            int x;
            scanf("%d%d%d",&num[i][0],&num[i][1],&x);
            sta[num[i][0]][num[i][1]]=x;
        }
        int x,y;
        scanf("%d%d",&x,&y);
        bfs(x,y);
        for(int i=0; i<N; i++)
        {
            if(time[num[i][0]][num[i][1]]==-1)
                printf("1 %d\n",sta[num[i][0]][num[i][1]]);
            else
                printf("0 %d\n",time[num[i][0]][num[i][1]]);
        }
    }
    return 0;
}


相關文章