此题题目意思很简单:就是问最少的可以剪切和粘贴的次数。
但是实现起来确实是很麻烦,主要是思路不清晰
此题加深了我对迭代加深搜索的理解 同时其中剪枝是非常难以想到的。
借用刘汝佳先生书上所说:此题采用迭代加深搜索,下面是刘汝佳先生的代码
<span style="font-size: 13.3333339691162px; font-family: Arial, Helvetica, sans-serif;">// UVa11212 Editing a Book</span>
// Rujia Liu
// This implementation is not very fast, but easy to understand
#include<cstdio>
#include<cstring>
using namespace std;
const int maxn = 9;
int n, a[maxn];
bool is_sorted() {
for(int i = 0; i < n-1; i++)
if(a[i] >= a[i+1]) return false;
return true;
}
// the number of integers with incorrect successor
int h() {
int cnt = 0;
for(int i = 0; i < n-1; i++)
if(a[i]+1 != a[i+1]) cnt++;
if(a[n-1] != n) cnt++;
return cnt;
}
bool dfs(int d, int maxd) {
if(d*3 + h() > maxd*3) return false;
if(is_sorted()) return true;
int b[maxn], olda[maxn];
memcpy(olda, a, sizeof(a));
for(int i = 0; i < n; i++)
for(int j = i; j < n; j++) {
// cut and paste are <span style="font-family: Arial, Helvetica, sans-serif; font-size: 12px;">the difficult part.</span>
int cnt = 0;
for(int k = 0; k < n; k++)
if(k < i || k > j) b[cnt++] = a[k];
// insert before position k
for(int k = 0; k <= cnt; k++) {
int cnt2 = 0;
for(int p = 0; p < k; p++) a[cnt2++] = b[p];
for(int p = i; p <= j; p++) a[cnt2++] = olda[p];
for(int p = k; p < cnt; p++) a[cnt2++] = b[p];
if(dfs(d+1, maxd)) return true;
memcpy(a, olda, sizeof(a));
}
}
return false;
}
int solve() {
if(is_sorted()) return 0;
int max_ans = 5; // after experiments, we found ans <= 5 for n <= 9
for(int maxd = 1; maxd < max_ans; maxd++)
if(dfs(0, maxd)) return maxd;
return max_ans;
}
int main() {
int kase = 0;
while(scanf("%d", &n) == 1 && n) {
for(int i = 0; i < n; i++) scanf("%d", &a[i]);
printf("Case %d: %d\n", ++kase, solve());
}
return 0;
}