Codeforces Round #501 (Div. 3) E2. Stars Drawing (Hard Edition)

题目链接

A star is a figure of the following type: an asterisk character '*' in the center of the figure and four rays (to the left, right, top, bottom) of the same positive length. The size of a star is the length of its rays. The size of a star must be a positive number (i.e. rays of length 00 are not allowed).

Let's consider empty cells are denoted by '.', then the following figures are stars:

The leftmost figure is a star of size 11, the middle figure is a star of size 22 and the rightmost figure is a star of size 33.

You are given a rectangular grid of size n×mn×m consisting only of asterisks '*' and periods (dots) '.'. Rows are numbered from 11 to nn, columns are numbered from 11 to mm. Your task is to draw this grid using any number of stars or find out that it is impossible. Stars can intersect, overlap or even coincide with each other. The number of stars in the output can't exceed n⋅mn⋅m. Each star should be completely inside the grid. You can use stars of same and arbitrary sizes.

In this problem, you do not need to minimize the number of stars. Just find any way to draw the given grid with at most n⋅mn⋅m stars.

Input

The first line of the input contains two integers nn and mm (3≤n,m≤10003≤n,m≤1000) — the sizes of the given grid.

The next nn lines contains mm characters each, the ii-th line describes the ii-th row of the grid. It is guaranteed that grid consists of characters '*' and '.' only.

Output

If it is impossible to draw the given grid using stars only, print "-1".

Otherwise in the first line print one integer kk (0≤k≤n⋅m0≤k≤n⋅m) — the number of stars needed to draw the given grid. The next kk lines should contain three integers each — xjxj, yjyj and sjsj, where xjxj is the row index of the central star character, yjyj is the column index of the central star character and sjsj is the size of the star. Each star should be completely inside the grid.

 

Description:

定义一个星星由 '*' 组成,形状为一个对称的“十”字型,大小为星星 1/2 的横长(或纵长)减一(如题目中的图)。给出一个 n*m 的图,判断是不是每一个 '*' 都能属于某个星星,如果能则输出所有星星的位置及大小(各个星星间可以出现重叠甚至重合),否则输出-1.

 

Solution:

f[i][j][k] 代表在当前位置 (i, j) 在 k 方向有多少个连续的星号 '*'(1代表上,2代表下,3代表左,4代表右)。

枚举每一个星号(i, j),每一个星号上取 f[i][j][k](1 <= k <= 4) 中的最小值min,代表已当前位置为中心可形成的最大星星,当min > 1时代表当前星星合法。用vector记录下答案,扫一遍就可以找到所有星星。

同时用两个数组 presum_x 与 presum_y 表示当前星星的覆盖值,如果当前星星的位置为(i, j),大小为 d,那么将当前星星横向的左端点加一(presum_x[i-d+1][j]++),右端点加一的位置上减一(presum[i+d][j]--);同理,星星纵向的上端点加一(presum_y[i][j-d+1]++),下端点加一的位置上减一(presum_y[i][j+d]--)。

然后分别对这两个数组求横向和纵向的和,可以得出,所有找到的答案中的星星的覆盖值都大于0(妙啊)。所以,再进行一次扫描,如果当前位置为星号,且纵向和横向的覆盖值都为0,说明当前图中的所有星号不能被完全覆盖,输出-1,否则输出vector中记录下的答案。

 

Code:

#include <cstdio>
#include <cstring>
#include <string>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <map>
#define Fio ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define fopen freopen("in.txt", "r", stdin); freopen("out.txt", "w", stdout);
#define mst(a, b) memset(a, b, sizeof(a))
#define _rush() int T; cin >> T; while(T--)
#define rush() int T; scanf("%d", &T); while(T--)
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
const int INF = 0x3f3f3f3f;
const double eps = 1e-9;
const int Mod = 1e9 + 7;
const int MaxN = 1e5 + 5;

struct node {
	int x, y, d;
};

char s[1005][1005];
int f[1005][1005][5];
int presum_x[1005][1005], presum_y[1005][1005];
int n, m;

void init() {
	scanf("%d %d", &n, &m);
	for(int i = 1; i <= n; i++) {
		scanf("%s", s[i] + 1);
		for(int j = 1; j <= m; j++) {
			if(s[i][j] == '*') {
				f[i][j][1] = f[i-1][j][1] + 1;
				f[i][j][3] = f[i][j-1][3] + 1;
			}
			//else f[i][j][1] = f[i][j][3] = 0;
		}
	}
	for(int i = n; i >= 1; i--) {
		for(int j = m; j >= 1; j--) {
			if(s[i][j] == '*') {
				f[i][j][2] = f[i+1][j][2] + 1;
				f[i][j][4] = f[i][j+1][4] + 1;
			}
			// else f[i][j][4] = f[i][j][2] = 0;
		}
	}
}

vector <node> res;

int main()
{
    //Fio;

    init();
    for(int i = 1; i <= n; i++) {
    	for(int j = 1; j <= m; j++) {
    		if(s[i][j] == '*') {
    			int d = min(min(f[i][j][1], f[i][j][2]), min(f[i][j][3], f[i][j][4]));
    			if(d > 1) {
    				res.push_back((node){i, j, d-1});
    				presum_x[i-d+1][j]++; presum_x[i+d][j]--;
    				presum_y[i][j-d+1]++; presum_y[i][j+d]--;
				}
			}
		}
	}
	for(int i = 1; i <= n; i++) {
		for(int j = 1; j <= m; j++) {
			presum_x[i][j] += presum_x[i-1][j];
			presum_y[i][j] += presum_y[i][j-1];
		}
	}
	for(int i = 1; i <= n; i++) {
		for(int j = 1; j <= m; j++) {
			if(s[i][j] == '*' && presum_x[i][j] == 0 && presum_y[i][j] == 0) {
				printf("-1\n");
				return 0;
			}
		}
	}
	printf("%d\n", res.size());
	for(int i = 0; i < res.size(); i++) printf("%d %d %d\n", res[i].x, res[i].y, res[i].d);
	return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值