AOJ 0267 Triangle of Blocks
こんにちは川です。
今回は、問題文にある通りにブロックの操作をしていけば解けます。
この問題でコード長を短くすることを意識してたけど、そこまでだね。
#include<bits/stdc++.h>
using
namespace
std;
int
main()
{
int
n;
while
(cin>>n,n){
int
num[2000]={},co=n;
for
(
int
i=0;i<n;i++)cin>>num[i];
bool
NA=0;
for
(
int
i=0;!NA&&i<10000;i++){
bool
kawa=1;
for
(
int
j=0;j<co&&kawa;j++)
if
(num[j]!=j+1)kawa=0;
if
(kawa){
NA=1;
cout<<i<<endl;
}
num[co]=co+1;
for
(
int
j=0;j<=co;j++){
if
(num[j]==1)
for
(
int
k=j+1;k<=co&&num[j]==1;k++)
if
(num[k]!=1)swap(num[j],num[k]);
num[j]--;
}
for
(
int
j=co+1;j>=0;j--){
if
(num[j]!=0){
co=j+1;
break
;
}
}
}
if
(!NA)cout<<-1<<endl;
}
}