您的位置:首页 > 其它

HDU-1685-Booksort(IDA*)

2014-08-11 14:18 267 查看
[align=left]Problem Description[/align]
The Leiden University Library has millions of books. When a student wants to borrow a certain book, he usually submits an online loan form. If the book is available, then the next day the student can go and get it at the loan counter.
This is the modern way of borrowing books at the library.

There is one department in the library, full of bookcases, where still the old way of borrowing is in use. Students can simply walk around there, pick out the books they like and, after registration, take them home for at most three weeks.

Quite often, however, it happens that a student takes a book from the shelf, takes a closer look at it, decides that he does not want to read it, and puts it back. Unfortunately, not all students are very careful with this last step. Although each book has
a unique identification code, by which the books are sorted in the bookcase, some students put back the books they have considered at the wrong place. They do put it back onto the right shelf. However, not at the right position on the shelf.

Other students use the unique identification code (which they can find in an online catalogue) to find the books they want to borrow. For them, it is important that the books are really sorted on this code. Also for the librarian, it is important that the books
are sorted. It makes it much easier to check if perhaps some books are stolen: not borrowed, but yet missing.

Therefore, every week, the librarian makes a round through the department and sorts the books on every shelf. Sorting one shelf is doable, but still quite some work. The librarian has considered several algorithms for it, and decided that the easiest way for
him to sort the books on a shelf, is by sorting by transpositions: as long as the books are not sorted,

take out a block of books (a number of books standing next to each other),

shift another block of books from the left or the right of the resulting ‘hole’, into this hole,

and put back the first block of books into the hole left open by the second block.

One such sequence of steps is called a transposition.

The following picture may clarify the steps of the algorithm, where X denotes the first block of books, and Y denotes the second block.



Of course, the librarian wants to minimize the work he has to do. That is, for every bookshelf, he wants to minimize the number of transpositions he must carry out to sort the books. In particular, he wants to know if the books on the shelf can be sorted by
at most 4 transpositions. Can you tell him?

 

[align=left]Input[/align]
The first line of the input file contains a single number: the number of test cases to follow. Each test case has the following format:

One line with one integer n with 1 ≤ n ≤ 15: the number of books on a certain shelf.

One line with the n integers 1, 2, …, n in some order, separated by single spaces: the unique identification codes of the n books in their current order on the shelf.

 

[align=left]Output[/align]
For every test case in the input file, the output should contain a single line, containing:

if the minimal number of transpositions to sort the books on their unique identification codes (in increasing order) is T ≤ 4, then this minimal number T;

if at least 5 transpositions are needed to sort the books, then the message "5 or more".

 

[align=left]Sample Input[/align]

3
6
1 3 4 6 2 5
5
5 4 3 2 1
10
6 8 5 3 4 7 2 9 1 10

 

[align=left]Sample Output[/align]

2
3
5 or more

 

[align=left]Source[/align]
华东区大学生程序设计邀请赛_热身赛
 

思路:因为最多只有四步操作,可以考虑迭代加深DFS,结果试了一下最后一组样例都跑不出来。参考了一下学长的做法,还要加个函数求出可能的最小步数来剪枝。

#include <stdio.h>
#include <math.h>

int n,num[15],lim;
bool flag;

int h()
{
int i,r=0;

if(num[0]!=1) r++;
if(num[n-1]!=n) r++;

for(i=0;i<n-1;i++) if(num[i]+1!=num[i+1]) r++;

return r;
}

void dfs(int cnt)
{
if(flag) return;

int i;

for(i=0;i<n;i++) if(num[i]!=i+1) break;

if(i==n)
{
flag=1;
return;
}

if(cnt>=lim) return;

if(cnt+ceil(h()/3.0)>lim) return;//ceil(h()/3.0)至少还需要的步数,实际可能比他还大

int len,pos,j,p,temp[15];

for(i=0;i<n;i++) temp[i]=num[i];//把序列存到temp数组中

for(i=0;i<n;i++)
{
for(len=1;len<=n-i;len++)
{
for(pos=0;pos<=n-len;pos++)
{
if(pos==i) continue;

for(j=0;j<len;j++) num[pos+j]=temp[i+j];

for(p=0,j=0;j<n;)
{
if(pos==j) j+=len;

if(j>=n) break;

if(p==i) p+=len;

num[j++]=temp[p++];
}

dfs(cnt+1);
}
}
}

for(i=0;i<n;i++) num[i]=temp[i];//别忘了恢复数组
}

int main()
{
int T,i;

scanf("%d",&T);

while(T--)
{
scanf("%d",&n);

for(i=0;i<n;i++) scanf("%d",&num[i]);

flag=0;

for(lim=0;lim<5;lim++)
{
dfs(0);

if(flag) break;
}

if(flag) printf("%d\n",lim);
else printf("5 or more\n");
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: