Loading...

油売り - その1

とりあえず油売りに挑戦

Karetta|キミならどう書く 2.0 - 2007 - その 1

あとで、その2を書こう。
package abura;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class C {

public static void main(String[] args) {

new C().aburauri(10, 7, 3);

}

/**
* <p>
* 油を等分にして、売ります。
* </p>
*
* @param a
* 升の上限値を指定します。この升に最初に油が入ります。
* @param b
* 升の上限値を指定します。
* @param c
* 升の上限値を指定します。
*/
public void aburauri(int a, int b, int c) {
this.a = new Masu("a", a, a);
this.b = new Masu("b", b, 0);
this.c = new Masu("c", c, 0);

if (!pre_cond_check(this.a, this.b, this.c)) {
throw new RuntimeException("oh no");
}

move(this.a, this.b, this.c);
}

static void move(final Masu a, final Masu b, final Masu c) {
class Row {
int col1;
int col2;
int col3;

Row(int c1, int c2, int c3) {
this.col1 = c1;
this.col2 = c2;
this.col3 = c3;
}

boolean isOk(int c1, int c2, int c3) {
if (col1 == c1 && col2 == c2 && col3 == c3)
return false;
return true;
}

public String toString(){
return "[" + col1 + ", "+ col2 + ", " + col3 + "] ";
}
}
class CheckTable {
List<Row> list = new ArrayList<Row>();

boolean checkMemo() {
Iterator<Row> iterator = list.iterator();
while (iterator.hasNext()) {
if (!iterator.next().isOk(a.size, b.size, c.size)) {
System.out.println("this condition already appered!!");
return false;
}
}
return true;
}

void memo() {
list.add(new Row(a.size, b.size, c.size));
}

boolean trial(Masu f, Masu t) {
System.out.println(f.name + " -> " + t.name);
// fromのサイズがないので移動できない
if (f.size == 0)
return false;

// 移動許容サイズ
int m_s = t.limit_size - t.size;
System.out.println(f.size + "<= " + m_s);

// 移動先の許容サイズがない
if (m_s == 0)
return false;

//
if (f.size - m_s < 0){
int tmp = f.size;
t.size += f.size;
f.size = 0;
if (!checkMemo()) {
// もとにもどす。s
f.size = tmp;
t.size -= tmp;
return false;
}
return true;
}

if (f.size < m_s) {
f.size -= f.limit_size;
t.size += f.limit_size;

if (!checkMemo()) {
// もとにもどす。s
f.size += f.limit_size;
t.size -= f.limit_size;

return false;
}

return true;
}

f.size -= m_s;
t.size += m_s;

if (!checkMemo()) {
// もとにもどす。s
f.size += m_s;
t.size -= m_s;
return false;
}

return true;
}
void remove(){
list.remove(list.size() - 1);
}
}
CheckTable guard = new CheckTable();
int now_exec = 0;
int skip = 0;
while (true) {
guard.memo();
System.out.println(a + " " + b + " " + c);
if (check_goal(a, b, c))
break;

if (!(skip == 1) && guard.trial(a, b)) {
now_exec = 1;
skip = 1;
continue;
}

if (!(skip == 2) && guard.trial(a, c)) {
now_exec = 2;
skip = 2;
continue;
}

if (!(skip == 3) && guard.trial(b, a)) {
now_exec = 3;
skip = 3;
continue;
}

if (!(skip == 4) && guard.trial(b, c)) {
now_exec = 4;
skip = 4;
continue;
}

if (!(skip == 5) && guard.trial(c, a)) {
now_exec = 5;
skip = 5;
continue;
}

if (!(skip == 6) && guard.trial(c, b)) {
now_exec = 6;
skip = 6;
continue;
}

System.out.println("oh !! end of line...pre exec:["+now_exec+"]");
guard.remove();
skip = now_exec;
}

System.out.println(guard.list);
}

Masu a, b, c;

class Masu {
String name;
int limit_size;
int size;

public Masu(String name, int limit_size, int size) {
this.name = name;
this.limit_size = limit_size;
this.size = size;
}

public String toString() {
return String.format("(%s:%s/%s)", this.name, this.size,
this.limit_size);
}
}

static boolean pre_cond_check(Masu a, Masu b, Masu c) {
int divide = a.size / 2;
if (divide <= b.limit_size || divide <= c.size)
return true;
return false;
}

static boolean check_goal(Masu a, Masu b, Masu c) {
int divide = a.limit_size / 2;
if ((a.size == divide && b.size == divide)
|| (b.size == divide && c.size == divide)
|| (a.size == divide && c.size == divide)) {
return true;
}
return false;
}

protected static boolean eq(Object object, Object object2) {
return object == null ? object2 == null : object.equals(object2);
}

}
リアクション: 
java 852837875993282069

コメントを投稿

ホーム item

このブログを検索

Random Posts

Popular Posts

Labels

ADS