您的位置:首页 > 其它

hdu 4527 小明系列故事——玩转十滴水 bfs 解题报告

2016-07-08 18:11 513 查看


小明系列故事——玩转十滴水

Time Limit: 500/200 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)

Total Submission(s): 705 Accepted Submission(s): 275



Problem Description

  小明最近喜欢上了一个名为十滴水的游戏。



  游戏是在一个6*6的方格内进行的,每个格子上有一滴水或者没有水滴。水滴分为四个等级1~4。初始时你有十滴水,通过把水加入格子内的水滴,会让水滴升1级。你也可以把水放到空格子内,这样会在这个格子里面产生一个1级的水滴。当水滴等级大于4时则会爆裂为四个小水滴,并向四个方向飞溅。每个飞溅的小水滴碰到其他水滴后会融入其中,使其升一级或者爆裂,以此类推。飞溅的小水滴互不干扰,运动速度相等(1秒可以移动一个格子的距离)。水滴爆裂后就消失掉了。

Input

题目包含多组测试用例;

对于每组数据,首先是6行,每行有6个整数数字,每个数字的范围为0~4;当数字为0时,表示空格子,当数字为1~4时,表示1~4级的水滴;

然后第七行是一个整数m,表示有m个操作;接下来是m行,每行有两个整数x, y ,表示在(x,y)放入一滴水。

特别说明:每次都是在全部的水滴静止后才进行下一次操作,也就是说只有在方格内没有任何飞溅的小水滴时才能放入一滴水。

[Technical Specification]

1 <= m <= 10

1 <= x, y <= 6

Output

对于每组测试数据,请输出m个操作之后6*6方格内水滴的样子,每组数据的输出后面跟着一个空行。

Sample Input

0 0 0 4 0 4
0 0 0 0 0 0
1 0 0 4 0 4
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 1 0 0
1
1 6
0 0 0 4 0 4
0 2 0 4 0 4
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
1
1 6
2 2 0 2 0 3
3 3 0 1 3 1
2 2 2 4 0 4
2 4 4 2 2 3
3 3 2 4 0 1
1 3 4 3 0 1
6
5 3
5 3
3 3
3 2
2 1
6 3


Sample Output

0 0 0 0 0 0
0 0 0 0 0 0
2 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 2 0 0

0 0 0 0 0 0
0 3 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0

0 4 0 2 0 3
0 0 0 4 3 2
0 0 0 0 0 0
0 0 0 0 4 4
0 0 0 0 0 4
4 0 0 0 0 3

Hint
第一组数据:
(1,6)爆裂,然后在第二秒(1,4)(2,6)爆裂,在第四秒,两滴水同时到达(3,4), (3,4)变成了6,爆裂,然后在第七秒(3,1)(6,4)变成了2。


思路:不能用递归,用队列模拟每一秒的情况。

代码:

#include <iostream>
#include <cstring>
#include <string>
#include <queue>
#include <cstdio>
#include <algorithm>
using namespace std;

int m, x, y, map[10][10];
int fx[4][2] = {1, 0, 0, 1, -1, 0, 0, -1};
struct node
{
int x, y, time, d;
};
int check(int x, int y)
{
if(x >= 1 && x <= 6 && y >= 1 && y <= 6) return 1;
return 0;
}
void cal(int tx, int ty)
{
map[tx][ty]++;
if(map[tx][ty] > 4)
{
queue<node> Q;
map[tx][ty] = 0;
for(int i = 0; i < 4; i++)
{
node next;
next.x = tx + fx[i][0];
next.y = ty + fx[i][1];
next.time = 0;
next.d = i;
Q.push(next);
}
int time = 0;
while(!Q.empty())
{
while(!Q.empty() && Q.front().time == time)
{
node now = Q.front();
Q.pop();
if(map[now.x][now.y])
map[now.x][now.y]++;
else
{
if(check(now.x + fx[now.d][0], now.y + fx[now.d][1]))
{
node next;
next.x = now.x+fx[now.d][0];
next.y = now.y+fx[now.d][1];
next.time = time+1;
next.d = now.d;
Q.push(next);
}
}
}
for(int i = 1; i <= 6; i++)
{
for(int j = 1; j <= 6; j++)
if(map[i][j] > 4)
{
map[i][j] = 0;
for(int k = 0; k < 4; k++)
{
node next;
next.x = i+fx[k][0];
next.y = j+fx[k][1];
next.time = time+1;
next.d = k;
Q.push(next);
}
}
}
time++;
}
}
}

int main()
{
while(scanf("%d", &map[1][1]) != EOF)
{
for(int i = 2; i <= 6; i++)
scanf("%d", &map[1][i]);
for(int i = 2; i <= 6; i++)
for(int j = 1; j <= 6; j++)
scanf("%d", &map[i][j]);
scanf("%d", &m);
while(m--)
{
scanf("%d %d", &x, &y);
cal(x, y);
}
for(int i = 1; i <= 6; i++)
for(int j = 1; j <=6; j++)
if(j != 6) printf("%d ", map[i][j]);
else printf("%d\n", map[i][j]);
printf("\n");
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  acm bfs