B. Tiling Challenge

time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
One day Alice was cleaning up her basement when she noticed something very curious: an infinite set of wooden pieces! Each piece was made of five square tiles, with four tiles adjacent to the fifth center tile:

By the pieces lay a large square wooden board. The board is divided into n2 cells arranged into n rows and n columns. Some of the cells are already occupied by single tiles stuck to it. The remaining cells are free.
Alice started wondering whether she could fill the board completely using the pieces she had found. Of course, each piece has to cover exactly five distinct cells of the board, no two pieces can overlap and every piece should fit in the board entirely, without some parts laying outside the board borders. The board however was too large for Alice to do the tiling by hand. Can you help determine if it’s possible to fully tile the board?

Input
The first line of the input contains a single integer n (3≤n≤50) — the size of the board.

The following n lines describe the board. The i-th line (1≤i≤n) contains a single string of length n. Its j-th character (1≤j≤n) is equal to “.” if the cell in the i-th row and the j-th column is free; it is equal to “#” if it’s occupied.

You can assume that the board contains at least one free cell.

Output
Output YES if the board can be tiled by Alice’s pieces, or NO otherwise. You can print each letter in any case (upper or lower).

Examples
inputCopy
3
#.#

#.#
outputCopy
YES
inputCopy
4
##.#
#…

##.#
outputCopy
NO
inputCopy
5
#.###
…#
#…
###.#

outputCopy
YES
inputCopy
5
#.###
…#
#…
…#
#…##
outputCopy
NO
Note
The following sketches show the example boards and their tilings if such tilings exist:

因为给出的阵列不存在重叠的状况
所以从头开始一层一层的找,只要找到.(x,y)就看一下(x+1,y),(x+2,y),(x+1,y-1),(x+1,y+1)
这四个点是否都是.,如果不全都是点,输出NO,如果全是点的话,把这五处坐标均变为#
继续寻找

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<ctime>
#include<cmath>
#include<iostream>
#include<algorithm>
#include<map>
#include<stack>
#include<queue>
#include<set>
#include<vector>
#define ll long long
#define dd double
using namespace std;

int main() {
	ll n;
	cin >> n;
	char s[55][55];
	for (ll i = 0; i < n; i++) {
		for (ll j = 0; j < n; j++) {
			cin >> s[i][j];
		}
	}
	ll flag = 0;
	for (ll i = 0; i < n; i++) {
		for (ll j = 0; j < n; j++) {
			if (s[i][j] == '.'&&i>=0&&i<=n-3&&j<=n-2&&j>=1) {
			//判断一下越界问题
				if (s[i + 1][j] == '.' && s[i + 2][j] == '.' && s[i + 1][j - 1] == '.' && s[i + 1][j + 1] == '.') {
					s[i + 1][j] = '#';
					s[i + 2][j] = '#';
					s[i + 1][j - 1] = '#';
					s[i + 1][j + 1] = '#';
				}
				else {
					flag = 1;
					cout << "NO" << endl;
					break;
				}
			}
			else if (s[i][j] == '#') {
				continue;
			}
			else {
				flag = 1;
				cout << "NO" << endl;
				break;
			}
		}
		if (flag == 1) {
			break;
		}
	}
	if (flag == 0) {
		cout << "YES" << endl;
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值