CodeForces 1345 A Puzzle Pieces 几何

探讨如何解决由n*m个特殊拼图组成的难题,每个拼图有三个凸起和一个凹槽,通过旋转和平移,判断是否能排列成满足条件的网格。算法解析了拼接可能性的数学原理,并提供了代码实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1. 题目描述

1.1. Limit

Time Limit: 1000 ms

Memory Limit: 256 MB

1.2. Problem Description

You are given a special jigsaw puzzle consisting of n ⋅ m n \cdot m nm identical pieces. Every piece has three tabs and one blank, as pictured below.

1
The jigsaw puzzle is considered solved if the following conditions hold:

  1. The pieces are arranged into a grid with n n n
    rows and m m m columns.
  2. For any two pieces that share an edge in the grid, a tab of one piece fits perfectly into a blank of the other piece.

Through rotation and translation of the pieces, determine if it is possible to solve the jigsaw puzzle.


1.3. Input

The test consists of multiple test cases. The first line contains a single integer t ( ≤ t ≤ 1000 ) t( \le t \le 1000) t(t1000) — the number of test cases. Next t t t lines contain descriptions of test cases.

Each test case contains two integers n n n and m m m ( 1 ≤ n , m ≤ 1000 ) (1 \le n, m \le 1000) (1n,m1000).


1.4. Output

For each test case output a single line containing “YES” if it is possible to solve the jigsaw puzzle, or “NO” otherwise. You can print each letter in any case (upper or lower).


1.5. Sample Input

3
1 3
100000 100000
2 2

1.6. Sample Output

YES
NO
YES

1.7. Note

For the first test case, this is an example solution:

2

For the second test case, we can show that no solution exists.

For the third test case, this is an example solution:

3

1.8. Source

CodeForces 1345 A Puzzle Pieces


2. 解读

要将 n ⋅ m n \cdot m nm 个拼图组合起来,形成 m m m n n n 列的图形,则会有 m × ( n − 1 ) + n × ( m − 1 ) m \times (n - 1) + n \times (m - 1) m×(n1)+n×(m1) 个连接处,每个连接处需要一个凸起和一个凹陷的接口。由于每个拼图只有一个凹陷,那么要成功将拼图进行拼接,则需要 n × m ≥ m × ( n − 1 ) + n × ( m − 1 ) n \times m \ge m \times (n - 1) + n \times (m - 1) n×mm×(n1)+n×(m1)

3. 代码

#include <iostream>
using namespace std;

int main()
{
    // test case
    int t;
    scanf("%d", &t);
    // 行列数列
    long long line, row;
    // 连接处数量
    long long edges;
    // test case
    for (int i = 0; i < t; i++) {
        // 行列数量
        scanf("%lld %lld", &line, &row);
        // 计算连接处
        edges = (line) * (row - 1) + row * (line - 1);
        // 输出
        printf("%s\n", edges <= (line * row) ? "YES" : "NO");
    }
}

联系邮箱:curren_wong@163.com

Github:https://github.com/CurrenWong

欢迎转载/Star/Fork,有问题欢迎通过邮箱交流。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值