这道题目最主要是题意好迷,我老是get不到那个点。
题意为:一个r*c的方格有n个水滴,然后我们指定一个起点,他会向四周发射一粒小水滴,这个水滴只会在到达原本有水滴的方格才会停止(或者离开方格范围),并且使该处的水滴数+1。如果某个地方这一秒的水滴数大于4,则又会分裂为四个方向的小水滴。各个水滴之间不会碰撞。
我首先肯定是想到bfs,然后自行操作一下。。。
等我明白题意后,改了改就好了。。。当然有坑点,discuss上有。
代码如下:
#include<bits/stdc++.h>
using namespace std;
int dir[][2] = {0,1,0,-1,1,0,-1,0};
int r, c, n, T;
map <int, int>Map;
int G[105][105];
struct node
{
int x, y, cnt, director;
node(){}
node(int a, int b, int c)
{
x = a;
y = b;
cnt = c;
}
}ans[105];
bool Check(node t)
{
if(t.x > 0 && t.x <= r && t.y > 0 && t.y <= c && t.cnt <= T)
return 1;
return 0;
}
queue <node> q;
void add_point(node now, int i)
{
node next;
next.x = now.x + dir[i][0];
next.y = now.y + dir[i][1];
next.cnt = now.cnt + 1;
next.director = i;
if(Check(next))
q.push(next);
}
void bfs(int sx, int sy)
{
vector<pair<int, int> >v;
int tmp = 0;
node now;
now = node(sx, sy, 0);
for(int i = 0; i < 4; i++)
add_point(now, i);
while(!q.empty())
{
now = q.front();
q.pop();
if(now.cnt != tmp)
{
for(int i = 0; i < v.size(); i++)
G[v[i].first][v[i].second] = 0;
tmp = now.cnt;
v.clear();
}
if(G[now.x][now.y] > 0)
{
G[now.x][now.y]++;
if(G[now.x][now.y] == 5)
{
v.push_back(make_pair(now.x, now.y));
for(int j = 0; j < 4; j++)
add_point(now, j);
int key = Map[now.x * 1000 + now.y];
ans[key].cnt = now.cnt;
}
}
else
add_point(now, now.director);
}
}
int main()
{
int x, y, val;
while(cin >> r >> c >> n >> T)
{
memset(G, 0, sizeof(G));
Map.clear();
for(int i = 0; i < n; i++)
{
scanf("%d%d%d", &x, &y, &val);
ans[i].x = x;
ans[i].y = y;
ans[i].cnt = -1;
Map[1000 * x + y] = i;
G[x][y] = val;
}
scanf("%d%d", &x, &y);
bfs(x, y);
for(int i = 0; i < n; i++)
{
if(ans[i].cnt >= 0)
printf("0 %d\n", ans[i].cnt);
else
printf("1 %d\n", G[ans[i].x][ans[i].y]);
}
}
return 0;
}