codeforces 1015E1&&E2
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:
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.
The first line of the input contains two integers nn and mm (3≤n,m≤1003≤n,m≤100) — 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.
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 centralstar character and sjsj is the size of the star. Each star should be completely inside the grid.
6 8
....*...
...**...
..*****.
...**...
....*...
........
3
3 4 1
3 5 2
3 5 1
5 5
.*...
****.
.****
..**.
.....
3
2 2 1
3 3 1
3 4 1
5 5
.*...
***..
.*...
.*...
.....
-1
3 3
*.*
.*.
*.*
-1
In the first example the output
2
3 4 1
3 5 2
is also correct.
题意:给你一个大小为n*m的图,求其中‘*’的十字架大小
题解:暴力,记得给走过的路打标记
代码如下:
#include <map> #include <set> #include <cmath> #include <ctime> #include <stack> #include <queue> #include <cstdio> #include <cctype> #include <bitset> #include <string> #include <vector> #include <cstring> #include <iostream> #include <algorithm> #include <functional> #define fuck(x) cout<<"["<<x<<"]"; #define FIN freopen("input.txt","r",stdin); #define FOUT freopen("output.txt","w+",stdout); //#pragma comment(linker, "/STACK:102400000,102400000") using namespace std; typedef long long LL; typedef pair<int, int> PII; const int maxn = 1005; char str[maxn][maxn]; bool vis[maxn][maxn]; int n,m; struct node { int x,y,cnt; }; std::vector<node> v; bool check(int x,int y,int r){ if(x+r>=n||x-r<0||y+r>=m||y-r<0) return 0; return 1; } int main(){ #ifndef ONLINE_JUDGE FIN #endif scanf("%d%d",&n,&m); for(int i=0;i<n;i++){ scanf("%s",str[i]); } for(int i=0;i<n;i++){ for(int j=0;j<m;j++){ int cnt=0; if(str[i][j]=='*'){ for(int a=1; ;a++){ if(check(i,j,a)){ if(str[i+a][j]=='*'&&str[i-a][j]=='*'&&str[i][j+a]=='*'&&str[i][j-a]=='*'){ cnt++; vis[i+a][j] = vis[i-a][j] = vis[i][j+a] = vis[i][j-a] = 1; //求十字架的长度 vis[i][j] = 1; }else break; }else break; } } if (cnt != 0) v.push_back(node{i+1, j+1, cnt}); } } bool ok = 1; for (int i = 0; i < n; i++){ for (int j = 0; j < m; j++){ if (str[i][j] == '*' && !vis[i][j]){ ok = 0; break; } } } if (!ok){ printf("-1\n"); }else{ printf("%d\n", v.size()); for (int i = 0; i < v.size(); i++){ printf("%d %d %d\n", v[i].x, v[i].y, v[i].cnt); } } }