CodeForces 389B Fox and Cross
来源:互联网 发布:网络金融创新 编辑:程序博客网 时间:2024/06/05 11:13
链接:http://codeforces.com/problemset/problem/389/B
Fox and Cross
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.
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. Thei-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)
5.#...####..####...#......
YES
4################
NO
6.#....####...####..#.##.######.#..#.
YES
6.#..#.######.####..####.######.#..#.
NO
3.........
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] = '.';}elseflag = 1;}if (flag)printf("NO\n");elseprintf("YES\n");}return 0;}bool check(int x){if (x>=0 && x<n)return 1;return 0;}
- Codeforces 389B Fox and Cross(dfs)
- 模拟-codeforces-389B-Fox and Cross
- CodeForces 389B Fox and Cross
- codeforces 389b Fox and Cross
- codeforces Fox and Cross
- codeforces Fox and Cross
- B. Fox and Cross
- CF 389B:Fox and Cross
- Codeforces Round #228 (Div. 2) B. Fox and Cross
- Codeforces Round #228 (Div. 2)B. Fox and Cross
- Codeforces Round #228 (Div. 2)B. Fox and Cross
- Codeforces Round #228 (Div. 2) B. Fox and Cross
- CF - 389 - B. Fox and Cross(贪心)
- Codeforces Round #228 (Div. 2) B Fox and Cross(DFS)
- Fox and Cross
- vj Fox and Cross
- codeforces 388B Fox and Minimal path
- Codeforces B. Fox and Minimal path
- 字符设备驱动设计
- Android网络请求,请求参数是中文导致的乱码问题
- servlet禁用cookie
- 1043 -- n个人的工作分配
- 机器学习算法与Python实践之(四)支持向量机(SVM)实现
- CodeForces 389B Fox and Cross
- Mysql连接报错:Unknown system variable 'language'
- 并查集算法介绍
- 1044 -- 用分枝定界法求解TSP问题
- 记一次Linux服务器上查杀木马经历
- property 和 synthesize
- MVC设计模式浅析
- Java加载Class文件的原理机制
- mongodb安装及简单操作