?? mcstate.java
字號:
package AI;
import java.util.*;
/*
* 狀態類
*/
public class MCState
{
public int weight;//權值
public int m,c,b;//m=傳教士,c=野人,b=0無船,b=1有船
public ArrayList<MCOperationMeta> OperaionMetas;//所有操作算子
public MCState PrevState ;//上級節點,用于回溯
public ArrayList<MCState> NextState ;//下級節點
public MCOperationMeta opMeta;//到達這一步的操作算子
public HashSet<MCOperationMeta> opMetasUnused;//當前節點還沒有嘗試的操作算子集合
////生成操作算子 這個方法和上面的OperaionMetas字段,適合單獨搞一個類,并且用屬性的概念來做
public MCState()
{
weight=0;
NextState=new ArrayList<MCState>();
opMetasUnused=new HashSet<MCOperationMeta>();
}
public void AddWeight(int w)
{
//找到成功路徑則給可行的節點加權,權值加w;
if(w==0)//=0可以不執行操作
return;
MCState s;
s=this.PrevState;
while(s!=null)
{
s.weight +=w;
s=s.PrevState ;
}
}
//狀態相等判斷
public boolean equals(MCState s)
{
if(this.m!=s.m)return false;
if(this.c!=s.c)return false;
if(this.b!=s.b)return false;
return true;
}
//生成可用的操作算子
public int CreateOperationMetas()
{
if(this.OperaionMetas==null)
return 0;
for(int i=0;i<this.OperaionMetas.size();i++)
{
opMetasUnused.add(this.OperaionMetas.get(i));
}
//到達這一點的操作算子不能再使用
opMetasUnused.remove(opMeta);
//無法完成的操作算子不能使用,3-m 3-c 為右岸狀態
MCOperationMeta op;
Iterator<MCOperationMeta> iter=opMetasUnused.iterator() ;
while(iter.hasNext())
{
op=iter.next();
if(b==1)//在左岸,操作算子不能大于m&c
{
if((op.m>this.m)||(op.c>this.c))
{
opMetasUnused.remove(op);
iter=opMetasUnused.iterator();
}
//并且移動后左岸的m>=c,或者m=0
if(((this.m-op.m)!=0)&&((this.m-op.m)<(this.c-op.c)))
{
opMetasUnused.remove(op);
iter=opMetasUnused.iterator();
}
//并且移動后右岸的m>=c,或者m=0
if((3-this.m+op.m!=0)&&((3-this.m+op.m)<(3-this.c+op.c)))
{
opMetasUnused.remove(op);
iter=opMetasUnused.iterator();
}
}
else
{
//在右岸,操作算子不能大于3-m&3-c
if((3-this.m<op.m)||(3-this.c<op.c))
{
opMetasUnused.remove(op);
iter=opMetasUnused.iterator();
}
//并且移動后左岸的m>=c,或者m=0
if(((this.m+op.m)!=0)&&((this.m+op.m)<(this.c+op.c)))
{
opMetasUnused.remove(op);
iter=opMetasUnused.iterator();
}
//并且移動后右岸的m>=c,或者m=0
if((3-this.m-op.m!=0)&&((3-this.m-op.m)<(3-this.c-op.c)))
{
opMetasUnused.remove(op);
iter=opMetasUnused.iterator();
}
}
}
return opMetasUnused.size();
}
//規則定義
public boolean isLegalState()
{
if((m<c)&&(m!=0)) return false;
return true;
}
public boolean isStartState()
{
if(m==3&&c==3&&b==1)
return true;
else
return false;
}
public boolean isEndState()
{
if(m==0&&c==0&&b==0)
return true;
else
return false;
}
//進行操作,產生下一個狀態點
public MCState takeOperation()
{
//操作算子都用過了,則返回空;
if(opMetasUnused.isEmpty())
return null;
MCState s;
s=new MCState();
//獲得一個操作算子
s.opMeta=opMetasUnused.iterator().next();
//從當前狀態的可用算子中移去這個算子
opMetasUnused.remove(s.opMeta);
//如果有船則開過去
if(this.b==1)
{
s.m=this.m-s.opMeta.m ;
s.c=this.c-s.opMeta.c;
s.b=0;
}
else//沒船則開過來
{
s.m=this.m+s.opMeta.m ;
s.c=this.c+s.opMeta.c;
s.b=1;
}
s.OperaionMetas=this.OperaionMetas ;
s.CreateOperationMetas();
s.PrevState=this;
return s;
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -