codeforces 1740D

题目链接

题目大意

给定 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+m4)就可合法移动到目标栈 ( 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+m4,则这一步必合法)

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;
}

  • 7
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值