TheBlockProblem——java

Uva 101 the block problem 木块问题

题目大意:

输入n,得到编号为0~n-1的木块,分别摆放在顺序排列编号为0~n-1的位置。现对这些木块进行操作,操作分为四种。

1、move a onto b:把木块a、b上的木块放回各自的原位,再把a放到b上;

2、move a over b:把a上的木块放回各自的原位,再把a发到含b的堆上;

3、pile a onto b:把b上的木块放回各自的原位,再把a连同a上的木块移到b上;

4、pile a over b:把a连同a上木块移到含b的堆上。

当输入quit时,结束操作并输出0~n-1的位置上的木块情况

Sample Input 
10
move 9 onto 1
move 8 over 1
move 7 over 1
move 6 over 1
pile 8 over 6
pile 8 over 5
move 2 over 1
move 4 over 9
quit
Sample Output 
 0: 0
 1: 1 9 2 4
 2:
 3: 3
 4:
 5: 5 8 7 6
 6:
 7:
 8:
 9:

代码:

import java.util.LinkedList;
import java.util.Scanner;

/**
 * 题意:
 * 
 * 从左到右有n个木块,编号为0~n-1,要求模拟以下四种操作(下面的a和b都是木块编号):
 * <1> move a onto b : 把a和b上方的木块全部归位,然后把a放在b上面。
 * <2> move a over b : 把a上方的木块全部归位,然后把a放在b所在木块堆的顶部。
 * <3> pile a onto b : 把b上方的木块全部归位,然后把a及上面的木块整体放在b上面。 
 * <4> pile a over b : 把a及上面的木块整体放在b所在木块堆的顶部。
 * 遇到quit时终止数据。a和b在同一堆的指令是非法指令,忽略。
 * 所有操作结束后,输出每个位置的木块列表,按照从底部到顶部的顺序排列。
 * 
 */
class Block{
	private int p;//place
	private int h;//height
	public void setP(int p) {
		this.p = p;
	}
	public void setH(int h) {
		this.h = h;
	}
	public int getP() {
		return p;
	}
	public int getH() {
		return h;
	}
}
class Link{
	private LinkedList<Integer> list;	
	public Link() {
		this.list = new LinkedList<Integer>(); 
	}
	public LinkedList<Integer> getList() {
		return list;
	}
	
}
public class Main{
	static Link pile[];
	static Block place[];
	static int n;
	public static void main(String[] args) {
		pile = new Link[30];
		place = new Block [30];
	    Scanner input = new Scanner(System.in);
		n = input.nextInt();
		for(int i = 0; i<n; i++) {
			pile[i] = new Link();
			place[i] = new Block();
			pile[i].getList().add(i);
		}
		String s1;
		while(!"quit".equals(s1 = input.next())) {
			int a = input.nextInt();
			String s2 = input.next();
			int b = input.nextInt();
			find_block(a);
			find_block(b);
			if(place[a].getP() == place[b].getP()) {
				continue;
			}
			if("onto".equals(s2)) {
				clear_above(place[b].getP(),place[b].getH());
			}
			if("move".equals(s1)) {
				clear_above(place[a].getP(),place[a].getH());
			}
			find_block(a);
			find_block(b);
			pile_onto(place[a].getP(),place[a].getH(),place[b].getP());
		}
		print();
		input.close();
		return;
	}
	//找到a木块的位置
	static void find_block(int a) {
		for(int p = 0; p < n; p++) {
			for(int h = 0; h < pile[p].getList().size(); h++) {
				if(pile[p].getList().get(h)==a) {
					//设置木块a的位置信息,在第p堆,高度为h的位置
					place[a].setP(p);
					place[a].setH(h);
				}
			}
		}
	}
	//归位:把p堆高度为h的木块上方所有的木块移回原位置
	static void clear_above(int p,int h) {
		//在t堆添加木块t
		int i = h + 1;
		while(i < pile[p].getList().size()){
	        int t = pile[p].getList().get(i);
	        pile[t].getList().add(t); 
	        pile[p].getList().remove(h+1);
	        find_block(t);
		} 
	}
	//把p1堆高度为h及其上方的木块整体移到p2堆的尾部
	static void pile_onto(int p1,int h,int p2){
		//在p2堆依次添加木块
		int i = h;
		 while(i < pile[p1].getList().size()) {
			 int t = pile[p1].getList().get(i);
			 pile[p2].getList().add(t);
			 pile[p1].getList().remove(h);
			 }
	}
	//输出木块最后的状态
	  static void print(){
	     for(int i=0;i<n;i++){
	         System.out.print(i+":");
	         for(int j=0;j<pile[i].getList().size();j++){
	        	 System.out.print(" "+pile[i].getList().get(j));
	         }
	         System.out.println();
	     }
	 }
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值