Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

我看google不到解题报告,所以把自己的代码贴出来了。希望暂时没做出来的,看了,会知道些~

Posted by bugthink at 2010-01-19 22:50:29 on Problem 1480
思路:没什么好讲的,就是DFS,但其实是iterative deepening,别笑我了,有时候,有点代码参考还是好些的,总比没有强。当你不知道错哪了,又没有别人的代码参考时,是很痛苦的~
/*
 * Consider only programs that have at most 10 statements.
 */


//import java.io.*;
import java.util.*;

public class Main {
	private enum Op{
		ADD,
		SUB,
		MUL,
		DIV,
		DUP;
	}
	
	private int[][] stack;
	private int top;
	
	private Op[] opSequence;
	private int len;
	
	private int n;
	
	private int[] x,y;
	
	private Op[] opOptimal;
	private int opLen;
	
	private int maxDepth;
	
	private boolean findOne;
	
	private static final Main test=new Main(10);
	
	private Main(int size){
		stack=new int[size][11];//"at most 10 statements"(only DUP increases stack
							//size by 1)
		opSequence=new Op[10];
		opOptimal=new Op[10];
		x=new int[size];
		y=new int[size];
	}
	
	private void init(int n,Scanner in){
		this.n=n;
		
		for(int i=0;i<n;i++)
			x[i]=in.nextInt();
		for(int i=0;i<n;i++)
			y[i]=in.nextInt();
		
		for(int i=0;i<n;i++)
			stack[i][0]=x[i];//push
		opLen=top=len=0;
		
		findOne=false;
	}
	
	public static Main getInstance(int n,Scanner in){
		test.init(n, in);
		
		return test;
	}
	
	/*public static void main(String[] args) throws IOException{
		Scanner in=new Scanner(new File("inputFiles/poj/OptimalPrograms.txt"));*/
	public static void main(String[] args){
		Scanner in=new Scanner(System.in);
		Main test;
		int n;
		int counter=1;
		while((n=in.nextInt())!=0){
			test=Main.getInstance(n, in);
			System.out.println("Program "+counter);
			counter++;
			test.runAndPrint();
			System.out.println();
		}
	}
	
	public void runAndPrint(){
		int i;
		for(i=0;i<n;i++)
			if(x[i]!=y[i])
				break;
		if(i==n){
			System.out.println("Empty sequence");
			return ;
		}
		
		for(maxDepth=1;maxDepth<11;maxDepth++)
			if(DFS(0)){
				//System.out.println("len:"+len);
				for(i=0;i<opLen;i++)
					System.out.print(opOptimal[i]+" ");
				System.out.println();
				break;
			}
		
	/*	maxDepth=4;
		DFS(0);*/
		
		if(maxDepth==11)
			System.out.println("Impossible");
	}
	
	private boolean isSolution(int[][] stack,int top){
		if(top>0)return false;
		
		for(int i=0;i<n;i++)
			if(stack[i][0]!=y[i])
				return false;
		return true;
	}
	
	/*
	 * s1 lexicographically precedes s2.
	 */
	private boolean better(Op[] s1,Op[] s2){
		for(int i=0;i<len;i++)
			if(!s1[i].toString().equals(s2[i].toString()))
				return s1[i].toString().compareTo(s2[i].toString())<0;
		return false;
	}
	
	private boolean isViable(Op t,int[][] stack,int top){
		switch(t){
		case ADD:
			for(int i=0;i<n;i++)
				if(Math.abs(stack[i][top-1]+stack[i][top])>30000)
					return false;
			return true;
		case SUB:
			for(int i=0;i<n;i++)
				if(Math.abs(stack[i][top-1]-stack[i][top])>30000)
					return false;
			return true;
		case MUL:
			for(int i=0;i<n;i++)
				if(Math.abs(stack[i][top-1]*stack[i][top])>30000)
					return false;
			return true;
			
		case DIV://Notice that DIV never increase the abs value.
			for(int i=0;i<n;i++)
				if(stack[i][top]==0)
					return false;
			return true;
		default: return true;
		}
	}
	
	private boolean DFSAssist(int depth,int[] a,int[] b){
		boolean flag=false;
		
		if(DFS(depth+1))
			flag=true;
		
		//restore:
		for(int i=0;i<n;i++){
			stack[i][top]=b[i];
			stack[i][top+1]=a[i];
		}
		top++;
		len--;
		
		return flag;
	}

	private boolean DFS(int depth){
		boolean flag=false;
		
		if(isSolution(stack,top)){
			if(!findOne||better(opSequence,opOptimal)){
				opLen=len;
				for(int i=0;i<len;i++)
					opOptimal[i]=opSequence[i];
			}
			findOne=true;
			
			return true;
		}
		
		if(depth>=maxDepth)
			return false;
		
		/*check each operation*/
		int[] a=new int[n];
		for(int i=0;i<n;i++)
			a[i]=stack[i][top];
		//dup:
		top++;
		//debug(top,depth);
		for(int i=0;i<n;i++)
			stack[i][top]=a[i];
		opSequence[len++]=Op.DUP;
		if(DFS(depth+1))
			flag=true;
		
		//restore:
		top--;
		len--;
		if(top>0){
			int[] b;
			b=new int[n];
			for(int i=0;i<n;i++)
				b[i]=stack[i][top-1];
			
			//add:
			if(isViable(Op.ADD,stack,top)){
				top--;
				for(int i=0;i<n;i++){
					stack[i][top]=b[i]+a[i];
				}
				opSequence[len++]=Op.ADD;
				if(DFSAssist(depth,a,b))
					flag=true;
			}
			//sub:
			if(isViable(Op.SUB,stack,top)){
				top--;
				for(int i=0;i<n;i++){
					stack[i][top]=b[i]-a[i];
				}
				opSequence[len++]=Op.SUB;
				if(DFSAssist(depth,a,b))
					flag=true;
			}
			//MUL:
			if(isViable(Op.MUL,stack,top)){
				top--;
				for(int i=0;i<n;i++){
					stack[i][top]=b[i]*a[i];
				}
				opSequence[len++]=Op.MUL;
				if(DFSAssist(depth,a,b))
					flag=true;
			}
			//DIV:
			if(isViable(Op.DIV,stack,top)){
				top--;
				for(int i=0;i<n;i++){
					stack[i][top]=b[i]/a[i];
				}
				opSequence[len++]=Op.DIV;
				if(DFSAssist(depth,a,b))
					flag=true;
			}
		}
		
		
		return flag;
	}
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator