| ||||||||||
| Online Judge | Problem Set | Authors | Online Contests | User | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest | |||||||||
java......
import java.io.BufferedInputStream;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;
import java.util.Stack;
class Node{
int x,y;
String ope;
Node pre;
public Node(int x,int y,String ope,Node pre) {
this.x=x;
this.y=y;
this.ope=ope;
this.pre=pre;
}
}
public class Main {
public static void main(String[] args) {
boolean[][] vis=new boolean[105][105];
Queue<Node> que=new LinkedList<Node>();
Scanner sc=new Scanner(new BufferedInputStream(System.in));
LinkedList<Node> save=new LinkedList<Node>();
Stack<Node> print=new Stack<Node>();
int a;
int b;
int c;
int count=0;
boolean flag=false;
a=sc.nextInt();
b=sc.nextInt();
c=sc.nextInt();
que.offer(new Node(0,0,"",null));
while(!que.isEmpty()) {
Node temp=que.poll();
save.offer(temp);
if(temp.x==c||temp.y==c) {
flag=true;
while(temp.pre!=null) {
count++;
print.push(temp);
temp=temp.pre;
}
break;
}
if(temp.x<a&&!vis[a][temp.y]) {
vis[a][temp.y]=true;
que.offer(new Node(a,temp.y,"FILL(1)",temp));
}
if(temp.y<b&&!vis[temp.x][b]) {
vis[temp.x][b]=true;
que.offer(new Node(temp.x,b,"FILL(2)",temp));
}
if(temp.x>0&&temp.y<b) {
int from=temp.x;
int to=temp.y;
if(from>=(b-to)&&!vis[from-b+to][b]) {
vis[from-b+to][b]=true;
que.offer(new Node(from-b+to,b,"POUR(1,2)",temp));
}
else
if(from<(b-to)&&!vis[0][from+to]) {
vis[0][from+to]=true;
que.offer(new Node(0,from+to,"POUR(1,2)",temp));
}
}
if(temp.y>0&&temp.x<a) {
int from=temp.y;
int to=temp.x;
if(from>=(a-to)&&!vis[a][from-a+to]) {
vis[a][from-a+to]=true;
que.offer(new Node(a,from-a+to,"POUR(2,1)",temp));
}
else
if(from<(a-to)&&!vis[from+to][0]) {
vis[from+to][0]=true;
que.offer(new Node(from+to,0,"POUR(2,1)",temp));
}
}
if(temp.x>0&&!vis[0][temp.y]) {
vis[0][temp.y]=true;
que.offer(new Node(0,temp.y,"DROP(1)",temp));
}
if(temp.y>0&&!vis[temp.x][0]) {
vis[temp.x][0]=true;
que.offer(new Node(temp.x,0,"DROP(2)",temp));
}
}
if(flag) {
System.out.println(count);
while(!print.isEmpty()) {
System.out.println(print.pop().ope);
}
}
else
System.out.println("impossible");
}
}
Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator