hdu 5336 XYZ and Drops 【BFS模拟】

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5336

题意:给你一个r*c的网格,有的网格为空,有的有水,再给出一个爆炸点,从这个点向四周爆出四个水滴,若碰到水则融为一体,若碰到其他水滴直接跑过去互不影响,每秒可跑一格,若水中水滴数量超过4则爆开,问T秒后网格的状态是怎样的。

代码:

#include <stdio.h>
#include <ctime>
#include <math.h>
#include <limits.h>
#include <complex>
#include <string>
#include <functional>
#include <iterator>
#include <algorithm>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <list>
#include <bitset>
#include <sstream>
#include <iomanip>
#include <fstream>
#include <iostream>
#include <ctime>
#include <cmath>
#include <cstring>
#include <cstdio>
#include <time.h>
#include <ctype.h>
#include <string.h>
#include <string>
#include <assert.h>
#pragma comment(linker,"/STACK:1024000000,1024000000")

using namespace std;

const int MAXN = 110;

int r, c, n, t;
int dir[][2] = { { 0,0 },{ 1,0 },{ -1,0 },{ 0,1 },{ 0,-1 } };
int s[MAXN][MAXN];// 地图,记录水滴的数目
int ans[MAXN][MAXN];// 记录(i,j)位置的爆裂时间

struct water // 询问的n个水坑
{
    int x, y;// 坐标
    int t;
}p[MAXN];

struct node// 水滴
{
    // 坐标,方向
    int x, y, dir;
    int t;
};

int sx, sy;

bool is_ok(int x, int y)
{
    if (x<1 || x>r || y<1 || y>c) return false;
    return true;
}

void bfs()
{
    queue<node> q;
    while (!q.empty()) q.pop();

    node tmp;
    for (int i = 1; i <= 4; i++)
    {
        tmp.x = sx;
        tmp.y = sy;
        tmp.dir = i;//水滴运动的方向
        q.push(tmp);
    }

    node qq,qqq;

    for (int k = 1; k <= t; k++)
    {
        int len = q.size();
        if (len == 0) break;
        for (int kk = 0; kk < len;kk++)
        {
            qq = q.front();
            q.pop();
            int x = qq.x; int y = qq.y; int d = qq.dir;
            int xx = qq.x + dir[d][0];
            int yy = qq.y + dir[d][1];

            if (is_ok(xx, yy))
            {
                if (ans[xx][yy] == k) continue;

                if (s[xx][yy])//有水被吸收
                {
                    s[xx][yy]++;
                    if (s[xx][yy] > 4)
                    {
                        if (ans[xx][yy] == 0)
                        {
                            ans[xx][yy] = k;
                            s[xx][yy] = 0;
                        }
                        for (int i = 1; i <= 4; i++)
                        {
                            tmp.x = xx;
                            tmp.y = yy;
                            tmp.dir = i;//水滴运动的方向
                            q.push(tmp);
                        }
                    }
                }
                else //没水继续走
                {
                    tmp.x = xx;
                    tmp.y = yy;
                    tmp.dir = d;//水滴运动的方向
                    q.push(tmp);
                }
            }

        }
    }
    return;
}

int main()
{
    while (~scanf("%d%d%d%d", &r, &c, &n, &t))
    {
        memset(s, 0, sizeof(s));
        memset(ans, 0, sizeof(ans));
        int x, y, z;

        for (int i = 1; i <= n; i++)
        {
            scanf("%d%d%d", &x, &y, &z);
            p[i].x = x; p[i].y = y;
            s[x][y] = z;
        }

        scanf("%d%d", &sx, &sy);
        bfs();

        for (int i = 1; i <= n; i++)
        {
            if (ans[p[i].x][p[i].y])
                printf("0 %d\n", ans[p[i].x][p[i].y]);
            else
                printf("1 %d\n", s[p[i].x][p[i].y]);
        }
    }
    return 0;
}

版权声明:转载请注明出处。

时间: 2024-08-25 22:29:46

hdu 5336 XYZ and Drops 【BFS模拟】的相关文章

Hdu 5336 XYZ and Drops (bfs 模拟)

题目链接: Hdu 5336 XYZ and Drops 题目描述: 有一个n*m的格子矩阵,在一些小格子里面可能会有一些水珠,每个小水珠都有一个size.现在呢,游戏开始咯,在一个指定的空的小格子里面有一个将要向四周爆裂的水珠,在下一面分别向上,下,左,右四个方向发射一个小水滴,(小水滴与水珠同,小水滴没有size),当小水滴走向一个格子,这个格子如果是空或者有其他的小水滴同时走到这个格子的情况下,对小水滴的运动轨迹是不影响的.但是遇到水珠的话,小水滴就会被吸收,水珠每次吸收一个小水滴size

HDU 5336 XYZ and Drops (模拟+搜索,详解)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5336 题面: XYZ and Drops Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 725    Accepted Submission(s): 201 Problem Description XYZ is playing an in

HDU 5336 XYZ and Drops(模拟十滴水游戏 BFS啊)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5336 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 &quo

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

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 r∗

HDU 5336(2015多校4)-XYZ and Drops(bfs)

题目地址:HDU 5336 题意:有一个r 行 c 列的格子,给出n个格子里有水滴的大小.再给出时间限制T,使得水滴从(sx,sy)位置开始爆破,当飞渐的水遇到格子里的静态水时就会聚在一起,当聚集的水滴大小>4时就会爆破.问在T时给定的n个位置格子里的水滴情况,如果没有爆破就输出:1 格子里水滴大小.否则输出:0 爆破的时间. #include <stdio.h> #include <math.h> #include <string.h> #include <

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?

hdu5336 XYZ and Drops (模拟+vector删除第i个元素)

题目链接: hdu5336 XYZ and Drops 模拟题一道,比较水,但是因为题意曲折 顺带vector的删除操作也是不太明白 总之逗了很长时间 删除第i个元素 v.erase(v.begin() + i); 删完后后面的元素都会往前移一个,所以下一个元素还是v[i] 也可以下面这样 it = v.erase(it); //erase()返回值是指向下一个元素的指针 //#define __LOCAL //#define __LLD #include <stdio.h> #include

CUGBACM_Summer_Tranning3 2013长沙现场赛(二分+bfs模拟+DP+几何)

A题:二分 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4791 用lower_bound可以轻松解决,不过比赛的时候逗逼了. 刚开始没有预处理,所以队友给出一组数据的时候没通过,然后一时紧张又想不出什么好的解决办法,所以就没再继续敲代码.实在有点可惜了. #include<iostream> #include<cstring> #include<cstdio> #include<cmath> #includ

解题报告 之 HDU5336 XYZ and Drops

解题报告 之 HDU5336 XYZ and Drops Description XYZ is playing an interesting game called "drops". It is played on a  grid. Each grid cell is either empty, or occupied by a waterdrop. Each waterdrop has a property "size". The waterdrop cracks