Codeforces Round #385 (Div. 2) B. Hongcow Solves A Puzzle

题意:

给你两个相同的拼图,每个拼图里的X都只能整体水平竖直移动,不能旋转,问你通过整体移动能不能让这两块拼图的X组成一个矩形(中间不能为空)。

思路:

因为每个拼图内部的X不能移动,所以要想拼成矩形,只有一个拼图中的X组成的是一个矩形才可以。

如何判断一个拼图里的X是不是一个矩形?找到一行有连续X的然后找下面几行相同的连续的X。数出数量,判断下是不是为X的数量和即可。


代码:

#include<bits/stdc++.h>
using namespace std;
const int maxn = 505;
char str[maxn][maxn];
bool book[maxn][maxn];
int row, col, num;

void solve()
{
    int i, j, k = 0, cur = 0;
    for(i = 0; i < row; i++)
    {
        for(j = 0; j < col; j++)
        {
            if(str[i][j] == 'X')
            {
                for(k = 0; j+k < col; k++)
                    if(str[i][j+k] != 'X') break;
                    else cur++;
            }
            if(k) break;
        }
        if(k) break;
    }
    bool ok = 1;
//    cout << cur << ' ' << num << endl;
    for(i++; i < row && ok; i++)
    {
        for(int t = j; t < j+k; t++)
            if(str[i][t] != 'X') { ok = 0; break; }
            else cur++;
    }
    puts(cur == num ? "YES" : "NO");
}

int main(void)
{
    while(cin >> row >> col)
    {
        num = 0;
        memset(book, 0, sizeof(book));
        for(int i = 0; i < row; i++)
            for(int j = 0; j < col; j++)
            {
                scanf(" %c", &str[i][j]);
                if(str[i][j] == 'X') num++;
            }
        solve();
    }
    return 0;
}


B. Hongcow Solves A Puzzle
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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.

Examples
input
2 3
XXX
XXX
output
YES
input
2 2
.X
XX
output
NO
input
5 5
.....
..X..
.....
.....
.....
output
YES
Note

For the first sample, one example of a rectangle we can form is as follows

111222
111222

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


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值