Poj 3460 Booksort (IDA *)

Source: Internet
Author: User

There are N (1 ≤ N ≤ 15) Books, each of which is different from the height of each. Now you can sort out the books in the following ways: extract a pile of books, and then keep the original order in place. In this case, we call it "one operation ". Now you need to find at least several operations to turn the book into a highly ascending state. If you need five or more times, you only need to output "5 or more ".

Solution: IDA * is a special dfs. Each dfs is set with a threshold value. Once the depth of the solution exceeds the threshold value, it will be traced back. IDA * adds an estimate of the current State. Once the number of steps that have been taken plus the estimated number of steps exceeds the threshold value, it traces back. If no solution is available under this threshold value, the threshold value is added iteratively. Only the depth of the solution is the minimum. Of course, the closer the "valuation function" is to the actual depth, the better, and should be less than or equal to the actual depth.

The evaluation function of this question is classic-because a single operation can change the successor values of up to three values, and the final State requires that the successor values at each location be 1 larger than those at the same time, therefore, the evaluation function divides the number of subsequent errors of each value in the current State by 3.

import java.util.Scanner;public class Main{int h() {int res = 0;for(int i=1;i<n;i++)if(arr[i]+1!=arr[i+1]) res++;if(arr[n]!=n) res++;return (res+2)/3;}long hash(){long res=0;return res;}int lim, n;int arr[]=new int[20];boolean flag;int dfs(int d) {int h = h();if (h == 0){flag=true;return d;}if (h + d > lim)return h + d;int temp[]=new int[n+1];for(int i=1;i<=n;i++)temp[i]=arr[i];int res = 1 << 28;for (int i = 1;i<n;i++)for (int j= i;j<n;j++)for (int k = j + 1; k <= n; k++) {int cnt=i;for(int p=j+1;p<=k;p++)arr[cnt++]=temp[p];for(int p=i;p<=j;p++)arr[cnt++]=temp[p];int tp=dfs(d+1);if(flag)  return tp;elseres=Math.min(tp, res);cnt=i;for(int p=i;p<=j;p++)arr[cnt++]=temp[p];for(int p=j+1;p<=k;p++)arr[cnt++]=temp[p];}return res;}void astar(){lim=h();flag=false;while(lim<=4&&!flag)lim=dfs(0);if(flag)System.out.println(lim);elseSystem.out.println("5 or more");}Scanner scan=new Scanner(System.in);void run(){int cas=scan.nextInt();while(cas-->0){n=scan.nextInt();for(int i=1;i<=n;i++)arr[i]=scan.nextInt();astar();}}public static void main(String[] args) {new Main().run();}}

Related Keywords:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.