CodeForces 389B Fox and Cross

链接:http://codeforces.com/problemset/problem/389/B

Fox and Cross

time limit per test:1 second
memory limit per test:256 megabytes
input:standard input
output:standard output

Fox Ciel has a board with n rows and n columns. So, the board consists of n × n cells. Each cell contains either a symbol '.', or a symbol '#'.

A cross on the board is a connected set of exactly five cells of the board that looks like a cross. The picture below shows how it looks.

CodeForces 389B Fox and Cross_第1张图片

Ciel wants to draw several (may be zero) crosses on the board. Each cross must cover exactly five cells with symbols '#', and any cell with symbol '#' must belong to some cross. No two crosses can share a cell.

Please, tell Ciel if she can draw the crosses in the described way.

Input

The first line contains an integer n (3 ≤ n ≤ 100) — the size of the board.

Each of the next n lines describes one row of the board. The i-th line describes the i-th row of the board and consists of n characters. Each character is either a symbol '.', or a symbol '#'.

Output

Output a single line with "YES" if Ciel can draw the crosses in the described way. Otherwise output a single line with "NO".

Sample test(s)

Input
5
.#...
####.
.####
...#.
.....
Output
YES
Input
4
####
####
####
####
Output
NO
Input
6
.#....
####..
.####.
.#.##.
######
.#..#.
Output
YES
Input
6
.#..#.
######
.####.
.####.
######
.#..#.
Output
NO
Input
3
...
...
...
Output
YES

Note

In example 1, you can draw two crosses. The picture below shows what they look like.

In example 2, the board contains 16 cells with '#', but each cross contains 5. Since 16 is not a multiple of 5, so it's impossible to cover all.


大意——给你一个只含.和#的地图,一个#和它的上下左右的#能组成一个十字架,并且两个十字架的#不能够共用,问:图中的所有#能不能全部被十字架给覆盖掉,如果能够,输出YES,否则输出NO。注意:只有组成十字架的#才能被十字架覆盖。


思路——贪心题目。将地图全部遍历一遍即可。每一次遇到#,就判断一下它是否能被覆盖掉,如果能够,将组成十字架的地方全部改为.以免重复使用,然后继续遍历,如果不能够,则不再遍历,直接输出NO结束。这样,遍历完之后,如果全部能被覆盖就输出YES结束。检查能不能被覆盖可以这样做:假设当前#位于(x,y),则检查(x+1,y)、(x+2,y)、(x+1,y-1)和(x+1,y+1)是不是为#,是的话就能覆盖,不是的话就不能。


复杂度分析——时间复杂度:O(n^2),空间复杂度:O(n^2)


附上AC代码:


#include <iostream>
#include <cstdio>
#include <string>
#include <cmath>
#include <iomanip>
#include <ctime>
#include <climits>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <queue>
#include <vector>
#include <set>
#include <map>
//#pragma comment(linker, "/STACK:102400000, 102400000")
using namespace std;
typedef long long ll;
const double pi = acos(-1.0);
const double e = exp(1.0);
const double eps = 1e-8;
const int maxn = 105;
char mat[maxn][maxn];
int n;

bool check(int x);

int main()
{
	ios::sync_with_stdio(false);
	while (~scanf("%d", &n))
	{
		for (int i=0; i<n; i++)
			scanf("%s", mat[i]);
		bool flag = 0;
		for (int i=0; i<n&&!flag; i++)
			for (int j=0; j<n&&!flag; j++)
				if (mat[i][j]=='#')
				{
					if (check(i+1) && check(i+2) && check(j-1) &&
						check(j+1) && mat[i+1][j-1]=='#' &&
						mat[i+1][j]=='#' && mat[i+1][j+1]=='#' &&
						mat[i+2][j]=='#')
					{
						mat[i][j] = '.';
						mat[i+1][j-1] = '.';
						mat[i+1][j] = '.';
						mat[i+1][j+1] = '.';
						mat[i+2][j] = '.';
					}
					else
						flag = 1;
				}
		if (flag)
			printf("NO\n");
		else
			printf("YES\n");
	}
	return 0;
}

bool check(int x)
{
	if (x>=0 && x<n)
		return 1;
	return 0;
}


你可能感兴趣的:(codeforces,贪心)