Hongcow Solves A Puzzle (思维)

来源:互联网 发布:交换机端口速率配置 编辑:程序博客网 时间:2024/05/29 10:18

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.

Input

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

Output "YES" if it is possible for Hongcow to make a rectangle. Output "NO" otherwise.

Example
Input
2 3XXXXXX
Output
YES
Input
2 2.XXX
Output
NO
Input
5 5.......X.................
Output
YES
Note

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...........

.....

思路 : 因为不允许翻转或旋转,所以,只有当一个拼图构成矩形时两块才能构成,因此,只要判断一块是不是矩形就行了。

具体就是记录所有‘X’的上下,左右最大值最小值,看里面是不是空。

#include <iostream>using namespace std;const int maxn = 510;char mp[maxn][maxn];int main(){    int m,n;    bool yes = 1;    cin >> m >> n;    int a[4];    a[0] = 0, a[1] = maxn;//a[0] a[1] 对应行最大最小    a[2] = 0, a[3] = maxn;// // a[2] a[3] 对应列最大最小    for(int i = 1;i <= m; i++)    for(int j = 1;j <= n; j++)    {        cin >> mp[i][j];        if(mp[i][j] == 'X')        {            if(i > a[0]) a[0] = i;            if(i < a[1]) a[1] = i;            if(j > a[2]) a[2] = j;            if(j < a[3]) a[3] = j;        }    }    for(int i = a[1]; i <= a[0]; i++)    for(int j = a[3]; j <= a[2]; j++)    {        if(mp[i][j] == '.') yes = 0;    }    if(yes) cout << "YES" <<endl;    else cout << "NO" <<endl;    return 0;}


阅读全文
0 0
原创粉丝点击