Codeforce#385B.Hongcow Solves A Puzzle
来源:互联网 发布:肛泰和马应龙 知乎 编辑:程序博客网 时间:2024/06/05 19:50
Hongcow likes solving puzzles.
One day, Hongcow finds two identical puzzle pieces, with the instructions "make a rectangle" next to them. The pieces can be described by an n by m grid of characters, where the character 'X' denotes a part of the puzzle and '.' denotes an empty part of the grid. It is guaranteed that the puzzle pieces are one 4-connected piece. See the input format and samples for the exact details on how a jigsaw piece will be specified.
The puzzle pieces are very heavy, so Hongcow cannot rotate or flip the puzzle pieces. However, he is allowed to move them in any directions. The puzzle pieces also cannot overlap.
You are given as input the description of one of the pieces. Determine if it is possible to make a rectangle from two identical copies of the given input. The rectangle should be solid, i.e. there should be no empty holes inside it or on its border. Keep in mind that Hongcow is not allowed to flip or rotate pieces and they cannot overlap, i.e. no two 'X' from different pieces can share the same position.
The first line of input will contain two integers n and m (1 ≤ n, m ≤ 500), the dimensions of the puzzle piece.
The next n lines will describe the jigsaw piece. Each line will have length m and will consist of characters '.' and 'X' only. 'X' corresponds to a part of the puzzle piece, '.' is an empty space.
It is guaranteed there is at least one 'X' character in the input and that the 'X' characters form a 4-connected region.
Output "YES" if it is possible for Hongcow to make a rectangle. Output "NO" otherwise.
2 3XXXXXX
YES
2 2.XXX
NO
5 5.......X.................
YES
For the first sample, one example of a rectangle we can form is as follows
111222111222
For the second sample, it is impossible to put two of those pieces without rotating or flipping to form a rectangle.
In the third sample, we can shift the first tile by one to the right, and then compose the following rectangle:
.......XX................
大体题意:
给你一个n*m 的矩阵, 你要用两个同样的图形构造出一个矩形来,问是否可以? .表示 这个地方是空地,X表示是一个小元素!你不可以旋转覆盖, 只能移动整体!其实可以把.给忽略。
思路:
逆向思考,一个矩形需要被两个相同的图形拼接起来,那么这个图形就必须是矩形。然后判断X覆盖的部分是不是矩形就可以了。计算出能套住所有X的最小矩形,然后把这个矩形扫一遍就可以判断里面是不是都是X了。
本弱ji本来就渣,题目还这么迷!无力吐槽!
AC代码:
#include<bits/stdc++.h>using namespace std;const int INF = 0x3f3f3f3f;char a[505][505];int main(){ int n, m; while(cin >> n >> m) { for(int i = 0; i < n; i++) cin >> a[i]; int left = INF, right = -INF, top = INF, bottom = - INF; for(int i = 0; i < n; i++) for(int j = 0; j < m; j++) if(a[i][j] == 'X') { left = min(left, j); right = max(right, j); top = min(top, i); bottom = max(bottom, i); } int flag = 1; for(int i = top; i <= bottom && flag; i++) for(int j = left; j <= right && flag; j++) if(a[i][j] != 'X') { puts("NO"); flag = 0; } if(flag) puts("YES"); } return 0;}
- Codeforce#385B.Hongcow Solves A Puzzle
- Codeforces Round #385 (Div. 2) B. Hongcow Solves A Puzzle
- CodeForces 745B Hongcow Solves A Puzzle
- 745 B. Hongcow Solves A Puzzle codeforces
- 【Codeforces 745 B Hongcow Solves A Puzzle】
- Hongcow Solves A Puzzle (思维)
- Hongcow Solves A Puzzle CodeForces
- 【37.50%】【codeforces 745B】Hongcow Solves A Puzzle
- CodeForces745B B - Hongcow Solves A Puzzle 暴力+判断
- Codeforces Round #385 (Div. 2) -- B. Hongcow Solves A Puzzle (判断是否是矩形,水题)
- Codeforces Round #385 (Div. 2)B. Hongcow Solves A Puzzle【思维+暴力】
- Codeforces Round #385 (Div. 2) B. Hongcow Solves A Puzzle 几何、思维题
- Codeforces Round #385 (Div. 2) 745B Hongcow Solves A Puzzle
- codeforce 11 11 A B
- codeforce 237A 237B
- CodeForce 339:A+B+C
- A. Hongcow Learns the Cyclic Shift #385
- codeforce 385 div1 a
- Android Material Design之CoordinatorLayout+AppBarLayout实现上滑隐藏
- View的深入浅出(二)View的滑动
- instancetype和id的区别
- 微信开发映射工具ngrok
- 重新格式化HDFS的方法
- Codeforce#385B.Hongcow Solves A Puzzle
- 树莓派无线网络配置
- Android杂记(二)
- 使用GDI实现视频字幕及特效
- python内建函数
- 第19天 静态导入、增强for循环、枚举
- 使用jquery怎么选择有两个class的元素?
- 驱动复习(mor8)
- js拖动div或图片