[贪心][暴力]Ticks Codeforces1579C

Casimir has a rectangular piece of paper with a checkered field of size n×m. Initially, all cells of the field are white.

Let us denote the cell with coordinates i vertically and j horizontally by (i,j). The upper left cell will be referred to as (1,1) and the lower right cell as (n,m).

Casimir draws ticks of different sizes on the field. A tick of size d (d>0) with its center in cell (i,j) is drawn as follows:

  1. First, the center cell (i,j) is painted black.
  2. Then exactly d cells on the top-left diagonally to the center and exactly d cells on the top-right diagonally to the center are also painted black.
  3. That is all the cells with coordinates (i−h,j±h) for all h between 0 and d are painted. In particular, a tick consists of 2d+1 black cells.

An already painted cell will remain black if painted again. Below you can find an example of the 4×9 box, with two ticks of sizes 2 and 3.

You are given a description of a checkered field of size n×m. Casimir claims that this field came about after he drew some (possibly 0) ticks on it. The ticks could be of different sizes, but the size of each tick is at least k (that is, d≥k for all the ticks).

Determine whether this field can indeed be obtained by drawing some (possibly none) ticks of sizes d≥k or not.

Input

The first line contains an integer t (1≤t≤100) — the number test cases.

The following lines contain the descriptions of the test cases.

The first line of the test case description contains the integers n, m, and k(1≤k≤n≤10; 1≤m≤19) — the field size and the minimum size of the ticks that Casimir drew. The following nn lines describe the field: each line consists of mm characters either being '.' if the corresponding cell is not yet painted or '*' otherwise.

Output

Print t lines, each line containing the answer to the corresponding test case. The answer to a test case should be YES if the given field can be obtained by drawing ticks of at least the given size and NO otherwise.

You may print every letter in any case you want (so, for example, the strings yEs, yes, Yes, and YES will all be recognized as positive answers).

Example

input

Copy

8
2 3 1
*.*
...
4 9 2
*.*.*...*
.*.*...*.
..*.*.*..
.....*...
4 4 1
*.*.
****
.**.
....
5 5 1
.....
*...*
.*.*.
..*.*
...*.
5 5 2
.....
*...*
.*.*.
..*.*
...*.
4 7 1
*.....*
.....*.
..*.*..
...*...
3 3 1
***
***
***
3 5 1
*...*
.***.
.**..

output

Copy

NO
YES
YES
YES
NO
NO
NO
NO

题意: 给出一个n*m的字符矩阵,判断该矩阵是否能由大小至少为k的V形组成,在第二个样例中,可以由大小为2的V和大小为3的V组成。

分析: 贪心的思想,要使全部的'*'被V所覆盖,即每个V覆盖尽可能多的'*',在矩阵上枚举每一个'*',判断以它为起点,能向左上及右上延伸的最长长度,与k作比较,如果大于等于k,说明该V是一个合法的V,之后覆盖V上的所有'*',同时维护一个vis数组标记当前点是否被覆盖,统计被覆盖点的个数,与矩阵中'*'个数作比较,二者相等时输出"YES",否则输出"NO"。

具体代码如下:

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
//贪心的思想,要想使所有'*'都出现在对号里需要每个点的对号尽可能长
char mp[20][20];
bool vis[20][20];

signed main()
{
	int T;
	cin >> T;
	while(T--)
	{
		int n, m, k, sum = 0, cnt = 0;
		cin >> n >> m >> k;
		for(int i = 1; i <= n; i++)
			for(int j = 1; j <= m; j++)
			{
				cin >> mp[i][j];
				if(mp[i][j] == '*')
					sum++;
			}
		memset(vis, 0, sizeof vis);
		for(int i = 1; i <= n; i++)
			for(int j = 1; j <= m; j++)
			{
				if(mp[i][j] == '*')
				{
					int d = 0;//搜索最大能延伸长度 
					while(1)
						if(i-d >= 1 && j-d >= 1 && mp[i-d][j-d] == '*' && i-d >= 1 && j+d <= m && mp[i-d][j+d] == '*')
							d++;
						else
							break;
					d--;
					if(d >= k)//如果是合法的对号 
					{
						for(int q = 0; q <= d; q++)//更新一遍答案 
						{
							if(!vis[i-q][j-q])
							{
								vis[i-q][j-q] = 1;
								cnt++;
							}
							if(!vis[i-q][j+q])
							{
								vis[i-q][j+q] = 1;
								cnt++;
							}
						}
					}
				}
			} 
		if(cnt == sum)//覆盖了全部的'*' 
			puts("YES");
		else
			puts("NO");
	} 
    return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值