hdu5336XYZ and Drops bfs模拟

//n个水团,当水团的大小大于4时
//就会炸开,变为4个小水滴向四个方向移动
//每个小水滴一秒移动一格
//当小水滴遇到大水团,大水团大小加1,小水滴消失
//0秒时有一个水团在sx,sy炸开
//问t秒时各个水团的状态
//用队列保存每个水滴的位置,方向,模拟就行
#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std ;
const int maxn = 110 ;
int map[maxn][maxn] ;
int id[maxn][maxn] ;
int dx[4] = {0 , 0 , 1 , -1} ;
int dy[4] = {1 , -1 , 0 , 0} ;
struct node
{
    int x , y ;
    int d ;
} ;
int r , c , t , n ;
int a[maxn] , b[maxn] ;
void bfs(int sx , int sy)
{
    queue<node> que[2] ;
    queue<int> q ;
    for(int i = 0 ;i < 4;i++)
    que[1].push(node{sx,sy,i}) ;
    for(int i = 1;i <= t;i++)
    {
        while(que[i%2].size())
        {
            node now = que[i%2].front() ;
            que[i%2].pop() ;
            int nx = now.x + dx[now.d] ;
            int ny = now.y + dy[now.d] ;
            if(nx < 1 || nx > r || ny < 1 || ny > c)
            continue ;
            if(map[nx][ny]){
                map[nx][ny]++ ;
                q.push(nx) ;
                q.push(ny) ;
            }
            else
                que[(i+1)%2].push(node{nx,ny,now.d}) ;
        }
        while(q.size())
        {
            int nx = q.front();q.pop() ;
            int ny = q.front();q.pop() ;
            if(map[nx][ny] > 4)
            {
                a[id[nx][ny]] = 0 ;
                b[id[nx][ny]] = i ;
                map[nx][ny] = 0 ;
                for(int j = 0;j < 4;j++)
                que[(i+1)%2].push(node{nx,ny,j}) ;
            }
        }
    }
}
int main()
{
    //freopen("d:\\in.txt" , "r" , stdin) ;
    while(~scanf("%d%d%d%d" , &r , &c , &n , &t))
    {
        memset(map , 0 , sizeof(map)) ;
        for(int i = 1;i <= n;i++)
        {
            int x , y , v ;
            scanf("%d%d%d" , &x , &y, &v) ;
            map[x][y] = v ;
            id[x][y] = i ;
        }
        int sx , sy ;
        scanf("%d %d" , &sx , &sy) ;
        if(sx >= 1 && sx <= r && sy >= 1 && sy <= n)
        bfs(sx , sy) ;
        for(int i = 1;i <= r;i++)
          for(int j = 1;j <= c;j++)
          if(map[i][j]){
             a[id[i][j]] = 1 ;
             b[id[i][j]] = map[i][j] ;
          }
        for(int i = 1;i <= n;i++)
        printf("%d %d\n"  , a[i] , b[i]) ;
    }
    return  0 ;
}
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值