zoukankan      html  css  js  c++  java
  • Hdu 5336 XYZ and Drops (bfs 模拟)

    题目链接:

      Hdu 5336 XYZ and Drops

    题目描述:

      有一个n*m的格子矩阵,在一些小格子里面可能会有一些水珠,每个小水珠都有一个size。现在呢,游戏开始咯,在一个指定的空的小格子里面有一个将要向四周爆裂的水珠,在下一面分别向上,下,左,右四个方向发射一个小水滴,(小水滴与水珠同,小水滴没有size),当小水滴走向一个格子,这个格子如果是空或者有其他的小水滴同时走到这个格子的情况下,对小水滴的运动轨迹是不影响的。但是遇到水珠的话,小水滴就会被吸收,水珠每次吸收一个小水滴size会增加1。为了万物的平衡,水珠size大于4的话就会向四周爆裂成为小水滴。问T时间后每个水珠的状态。

    解题思路:

      就是bfs模拟一下小水滴运动的状态就ok了,比赛的时候一直卡题意。

      1 #include <queue>
      2 #include <cstdio>
      3 #include <cstring>
      4 #include <iostream>
      5 #include <algorithm>
      6 using namespace std;
      7 const int maxn = 110;
      8 int dir[4][2] = {1,0, -1,0, 0,1, 0,-1};
      9 struct node
     10 {//坐标,运动方向,运动时间
     11     int x, y, dir, t;
     12 };
     13 int maps[2][maxn][maxn], point[maxn][2];
     14 int r, c, x, y, t;
     15 
     16 void bfs ()
     17 {
     18     queue <node> Q;
     19     node p, q;
     20     p.x = x;
     21     p.y = y;
     22     p.dir = 4;
     23     p.t = 0;
     24     Q.push (p);
     25     while (!Q.empty())
     26     {
     27         p = Q.front ();
     28         Q.pop ();
     29         if (p.t >= t)
     30             return ;
     31         if (p.dir == 4)
     32         {
     33             for (int i=0; i<4; i++)
     34             {
     35                 q.x = p.x + dir[i][0];
     36                 q.y = p.y + dir[i][1];
     37                 q.dir = i;
     38                 q.t = p.t + 1;
     39                 if (0>=q.x||q.x>r || 0>=q.y||q.y>c)
     40                     continue;
     41                 if (maps[1][q.x][q.y] == q.t)
     42                     continue;
     43                 if ( !maps[0][q.x][q.y] )
     44                     Q.push (q);
     45                 else
     46                 {
     47                     maps[0][q.x][q.y] ++;
     48                     if (maps[0][q.x][q.y] > 4)
     49                     {
     50                         maps[1][q.x][q.y] = q.t;
     51                         maps[0][q.x][q.y] = 0;
     52                         q.dir = 4;
     53                         Q.push (q);
     54                     }
     55                 }
     56             }
     57         }
     58         else
     59         {
     60             q.x = p.x + dir[p.dir][0];
     61             q.y = p.y + dir[p.dir][1];
     62             q.dir = p.dir;
     63             q.t = p.t + 1;
     64              if (0>=q.x||q.x>r || 0>=q.y||q.y>c)
     65                     continue;
     66              if (maps[1][q.x][q.y] == q.t)
     67                     continue;
     68              if ( !maps[0][q.x][q.y] )
     69                     Q.push (q);
     70                 else
     71                 {
     72                     maps[0][q.x][q.y] ++;
     73                     if (maps[0][q.x][q.y] > 4)
     74                     {
     75                         maps[1][q.x][q.y] = q.t;
     76                         maps[0][q.x][q.y] = 0;
     77                         q.dir = 4;
     78                         Q.push (q);
     79                     }
     80                 }
     81         }
     82 
     83     }
     84 }
     85 int main ()
     86 {
     87     int n, s;
     88     while (scanf ("%d %d %d %d", &r, &c, &n, &t) != EOF)
     89     {
     90         memset (maps, 0, sizeof(maps));
     91         for (int i=0; i<n; i++)
     92         {
     93             scanf ("%d %d %d", &x, &y, &s);
     94             point[i][0] = x;
     95             point[i][1] = y;
     96             maps[0][x][y] = s;
     97         }
     98         scanf ("%d %d", &x, &y);
     99         bfs ();
    100         for (int i=0; i<n; i++)
    101         {
    102             x = point[i][0];
    103             y = point[i][1];
    104             if (maps[0][x][y] == 0)
    105                 printf ("0 %d
    ", maps[1][x][y]);
    106             else
    107                 printf ("1 %d
    ", maps[0][x][y]);
    108         }
    109     }
    110     return 0;
    111 }
    本文为博主原创文章,未经博主允许不得转载。
  • 相关阅读:
    python实现从生成器中取固定角标的元素
    python-高阶函数
    git学习手册
    python批量进行文件修改操作
    python第一站
    LoadRunner,一个简单的例子
    python3中urllib2的问题
    gitlab 随笔
    sed 删除文本
    sed 修改文本
  • 原文地址:https://www.cnblogs.com/alihenaixiao/p/4690592.html
Copyright © 2011-2022 走看看