题目大意
给定 n n n行 m m m列的棋盘,初始在 ( 1 , 1 ) (1,1) (1,1)有 k k k张牌,从栈顶向下第 i i i个为 a [ i ] a[i] a[i],可以有数次操作,每次操作遵循以下规则
- 仅可把一张牌移动到有共边的栈内
- 除 ( 1 , 1 ) , ( n , m ) (1,1),(n,m) (1,1),(n,m)外每个栈最多容纳一张牌
- 不能从 ( n , m ) (n,m) (n,m)移动牌
-
(
1
,
1
)
(1,1)
(1,1)不能成为目标栈
要求最终在 ( n , m ) (n,m) (n,m)栈自顶向下为 1 1 1~ k k k,判断是否可解
思路
通过数字华容道类推,在给定棋盘上只要有一个空位,我们就可以把任意位置的牌挪到任意位置
开一个
s
e
t
set
set存数字,
o
p
op
op为当前要存的数,用指针
i
d
id
id扫,若到当前要存的
o
p
op
op,则判断
s
e
t
set
set里元素数量,只要小于
(
n
+
m
−
4
)
(n+m-4)
(n+m−4)就可合法移动到目标栈
(
4
为初始栈,目标栈,
o
p
和一个空位
)
(4为初始栈,目标栈,op和一个空位)
(4为初始栈,目标栈,op和一个空位),则跳过这个数字。若
o
p
op
op已经在
s
e
t
set
set中,则直接
e
r
a
s
e
erase
erase即可(因为上一步的
s
e
t
.
s
i
z
e
(
)
<
=
n
+
m
−
4
set.size()<=n+m-4
set.size()<=n+m−4,则这一步必合法)
ACcode
#include<bits/stdc++.h>
using namespace std;
#define int long long
void solve()
{
int n,m,k;cin>>n>>m>>k;
vectora(k+1);
for(int i=1;i<=k;i++)cin>>a[i];
setst;
int id=1;
int op=k;
while(op>0){
if(st.find(op)!=st.end()){
st.erase(op);
op–;
continue;
}
while(a[id]!=op&&id<=k)st.insert(a[id]),id++;
if(st.size()>(n*m-4))op=0;
op–;id++;
}
if(op==-1)cout<<“TIDAK”<<‘\n’;
else cout<<“YA”<<‘\n’;
}
signed main()
{
ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
int t;cin>>t;
while(t–){
solve();
}
return 0;
}