BFS。当水滴破裂飞溅后,直到碰到水滴才会停止(观察case1)。同时,考虑当水滴飞溅到点(x,y)并且该点同一时间破裂的情况,该水滴算作吸收。

 /* 3451 */
#include <iostream>
#include <queue>
#include <cstdio>
#include <cstring>
#include <cstdlib>
using namespace std; #define MAXN 105 typedef struct node_t{
int x,y,d,t;
node_t() {}
node_t(int xx, int yy, int dd, int tt) {
x = xx; y = yy; d = dd; t = tt;
}
} node_t; node_t beg;
int bx, by;
int n, m, l;
int map[MAXN][MAXN];
int t[MAXN][MAXN];
int dir[][] = {
-,,,,,-,,
}; inline bool check(int x, int y) {
return x< || x>=n || y< || y>=m;
} bool isValid() {
int i, j; for (i=; i<n; ++i)
for (j=; j<m; ++j)
if (map[i][j])
return false;
return true;
} void bfs() {
int x, y, d;
int i, j, k;
node_t nd;
queue<node_t> Q; memset(t, , sizeof(t));
if (map[bx][by] == l) {
map[bx][by] = ;
t[bx][by] = ;
nd.t = ;
for (i=; i<; ++i) {
nd.x = bx + dir[i][];
nd.y = by + dir[i][];
if (check(nd.x, nd.y))
continue;
nd.d = i;
Q.push(nd);
}
} else {
++map[bx][by];
return ;
} while (!Q.empty()) {
nd = Q.front();
Q.pop();
if (map[nd.x][nd.y] == l) {
// break
map[nd.x][nd.y] = ;
t[nd.x][nd.y] = nd.t;
for (i=; i<; ++i) {
x = nd.x + dir[i][];
y = nd.y + dir[i][];
if (check(x, y))
continue;
Q.push(node_t(x, y, i, nd.t+));
}
} else if (map[nd.x][nd.y]) {
// absorb
++map[nd.x][nd.y];
} else if (t[nd.x][nd.y] != nd.t) {
// no drop, continue fly
x = nd.x + dir[nd.d][];
y = nd.y + dir[nd.d][];
if (check(x, y))
continue;
Q.push(node_t(x, y, nd.d, nd.t+));
}
}
} int main() {
int t;
int i, j, k;
bool flag; #ifndef ONLINE_JUDGE
freopen("data.in", "r", stdin);
freopen("data.out", "w", stdout);
#endif scanf("%d", &t);
while (t--) {
scanf("%d %d %d", &n, &m, &l);
flag = false;
for (i=; i<n; ++i) {
for (j=; j<m; ++j) {
scanf("%d", &map[i][j]);
}
}
flag = isValid();
scanf("%d", &k);
while (k--) {
scanf("%d %d", &bx, &by);
--bx; --by;
if (flag == false) {
bfs();
flag = isValid();
}
}
if (flag) {
puts("YES");
} else {
puts("NO");
for (i=; i<n; ++i) {
for (j=; j<m; ++j)
printf("%d ", map[i][j]);
printf("\n");
}
}
} return ;
}
04-16 23:19