[Codeforces1015E(1,2)] Stars Drawing (贪心,暴力)

链接:

http://codeforces.com/contest/1015/problem/E1

http://codeforces.com/contest/1015/problem/E2

给你一个$n×m$的矩阵,里面的*组成十字(至少要5个构成十字,比如5个的十字大小为1,一次类推),问这里面的*是否能都组成十字。并输出一个方案。

这题暴力,贪心地让每一个*向四个方向延拓尽可能长的范围,并打好标记,存下所有可能的十字,看看这些标记里有没有是*但是为标记的就可以了。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#include <bits/stdc++.h>
using namespace std;

constexpr int maxn = 1010;
constexpr int dx[5] = {0, 0, 1, -1};
constexpr int dy[5] = {1, -1, 0, 0};
using Node = struct {
int x, y, v;
};
char G[maxn][maxn];
bool vis[maxn][maxn];
int n, m;
vector<Node> p, ret;

inline bool ok(int x, int y) {
return x >= 0 && x < n && y >= 0 && y < m;
}

void gao(int x, int y) {
int tot = 1;
bool flag = true;
bool yes = false;
while(flag) {
flag = false;
int cnt = 0;
for(int i = 0; i < 4; i++) {
int tx = x + dx[i] * tot;
int ty = y + dy[i] * tot;
if(ok(tx, ty) && G[tx][ty] == '*') cnt++;
}
if(cnt != 4) break;
for(int i = 0; i < 4; i++) {
int tx = x + dx[i] * tot;
int ty = y + dy[i] * tot;
vis[tx][ty] = 1;
}
flag = true;
yes = true;
tot++;
}
if(yes) {
vis[x][y] = 1;
ret.push_back({x+1, y+1, tot-1});
}
}

signed main() {
// freopen("in", "r", stdin);
while(~scanf("%d%d",&n,&m)) {
p.clear(); ret.clear();
memset(vis, 0, sizeof(vis));
for(int i = 0; i < n; i++) scanf("%s", G[i]);
for(int i = 0; i < n; i++) {
for(int j = 0; j < m; j++) {
if(G[i][j] == '*') p.push_back({i, j, 0});
}
}
if(p.size() == 0) {
printf("0\n");
continue;
}
for(auto t : p) gao(t.x, t.y);
if(ret.size() == 0) {
printf("-1\n");
continue;
}
bool flag = 0;
for(int i = 0; i < n; i++) {
for(int j = 0; j < m; j++) {
if(!vis[i][j] && G[i][j] == '*') {
flag = 1;
}
}
}
if(flag) {
printf("-1\n");
continue;
}
printf("%d\n", ret.size());
for(auto t : ret) {
printf("%d %d %d\n", t.x, t.y, t.v);
}
}
return 0;
}