P7724 Ancient Archive(Ancient Archive)

Background

To uncover the secrets behind moonlight energy, you have arrived at the underground Ancient Archive. The secrets of the ancient people and forgotten knowledge are all stored in this dust-sealed maze. Can you solve the puzzle and learn the ancient knowledge?

Description

At the center of the Ancient Archive is a puzzle: - There is a $2 \times 2$ grid. Each cell either contains a positive integer or is empty. - You may perform some operations. In each operation, you choose a **cell with a positive integer** and an **empty cell adjacent to it**, and move the positive integer into that empty cell. - Given the initial state and the final state of the grid, it is guaranteed that the initial and final states contain the same number of positive integers (denote it as $k$), and these integers are exactly the first $k$ distinct positive integers. Determine whether it is possible to reach the final state from the initial state in a finite number of operations. The figure below shows a grid containing three positive integers after two operations: ![](https://cdn.luogu.com.cn/upload/image_hosting/y2dnr4ss.png) Only by completing the puzzle can you obtain the forgotten knowledge, so you want to solve this problem as soon as possible. **Note: The grid may contain no positive integers, and it may also contain no empty cells.**

Input Format

The input consists of four lines, with two integers on each line. The first two lines describe the initial state, and the last two lines describe the final state. Use $0$ to represent an empty cell.

Output Format

If the final state can be reached from the initial state, output `Yes`; otherwise output `No`.

Explanation/Hint

**[Sample 1 Explanation]** As shown in the figure in the problem description. --- **[Sample 2 Explanation]** There are no movable positive integers, so it is impossible to reach a different final state from the initial state. --- **[Constraints]** **This problem uses bundled testdata.** All testdata satisfy the description of the problem. - Subtask 1 (40 points): There are no empty cells. - Subtask 2 (60 points): No special restrictions. --- ![](https://cdn.luogu.com.cn/upload/image_hosting/rz2eg0ca.png) Translated by ChatGPT 5