持续创作,加速成长!这是我参与「掘金日新计划 · 10 月更文挑战」的第30天,点击查看活动详情
题目链接
题目
Pak Chanek, a renowned scholar, invented a card puzzle using his knowledge. In the puzzle, you are given a board with nn rows and mm columns. Let (r,c) represent the cell in the r-th row and the c-th column.
Initially, there are k cards stacked in cell (1,1). Each card has an integer from 1 to k written on it. More specifically, the i-th card from the top of the stack in cell (1,1) has the number aiai written on it. It is known that no two cards have the same number written on them. In other words, the numbers written on the cards are a permutation of integers from 1 to k. All other cells are empty.
You need to move the kk cards to cell (n,m) to create another stack of cards. Let be the number written on the i-th card from the top of the stack in cell (n,m). You should create the stack in cell (n,m) in such a way so that =i for all 1≤i≤k.
In one move, you can remove the top card from a cell and place it onto an adjacent cell (a cell that shares a common side). If the target cell already contains one or more cards, you place your card on the top of the stack. You must do each operation while satisfying the following restrictions:
- Each cell other than (1,1) and (n,m) must not have more than one card on it.
- You cannot move a card onto cell (1,1).
- You cannot move a card from cell (n,m).
Given the values of n, m, k and the array a, determine if the puzzle is solvable.
题目大意
有一个 的地图,在 (1,1) 格子自上至下有一摞卡片,上面分别写着,是 1 到 k 的排列。每次操作可以从任何一个格子中选择最上面的卡片往相邻的格子上移动,但是需要满足以下条件:
- 除了 (1,1) 格子和 (n,m) 格子,其他格子上最多放一张卡片。
- 不能把卡片从别的格子移动到 (1,1) 格子。
- 不能把卡片从 (n,m) 格子移动到别的格子。
问是否能将所有卡片按照 的顺序自上至下的摆放在 (n,m) 格子上。
思路
我们称呼不包括 (1,1) 和 (n,m) 的整张地图为操作区域。
容易发现中如果操作区域中卡片数不超过 ,我们就可以把操作区域中的任一张卡片移动到 (n,m) 上。即:
- 如果 ,为了能把该卡片移动到 (n,m) 上,需要满足在该牌上面的牌的数量不超过 ,即 。
- 如果 ,此时值为 的卡片已经在 (n,m) 上了,为了能把该卡片移动到 (n,m) 上,需要满足在该牌上面的牌的数量不超过 ,即 。
- 如果 ,此时值为 的卡片已经在 (n,m) 上了,为了能把该卡片移动到 (n,m) 上,需要满足在该牌上面的牌的数量不超过 ,即 。
- ……
所以我们只需要对于整个数组,判断 和 的关系即可。
代码
赛中式子推成最后排成 的了,于是就令 再进行的判断,与思路区别不大。
#include <bits/stdc++.h>
using namespace std;
using LL=long long;
const int N=500001;
int n,m,k;
int a[N];
int solve()
{
scanf("%d",&n);
scanf("%d",&m);
scanf("%d",&k);
for (int i=1;i<=k;++i)
{
scanf("%lld",&a[i]);
a[i]=k-a[i]+1;
}
for (int i=1;i<=k;++i)
if (i>n*m-4+a[i]) return 0;
return 1;
}
int main()
{
int T=1;
for (scanf("%d",&T);T--;)
if (solve()) printf("YA\n");
else printf("TIDAK\n");
return 0;
}