資源簡介
用深度遍歷方法解決傳教士過河問題;
源代碼為java源代碼;
按照要求輸入;
代碼片段和文件信息
package?com.example;
import?java.util.Scanner;
import?static?java.lang.System.*;
/*
*?狀態類,包括每次開船前兩岸人數,方向,將要裝載的人數
*?l、r分別代表左岸、右岸,m、c分別代表野人、傳教士
*?*/
class?Status{
????public?int?lmlc;?????????????????????????????//左岸野人,傳教士數量
????public?int?m_in_shipc_in_ship;?????????????//準備要上船的野人,傳教士數量
????public?int?direction;????????????????????????//船的朝向0表示向右,1表示向左
????public?int?open;??????????????????????????????//記錄狀態可擴展的次數
????public?Status?next;???????????????????????????????????//指向下一個狀態
????public?Status?parent;?????????????????????????????????//指向前一個狀態
????Status()?{
????????this.lm?=?0;
????????this.lc?=?0;
????????this.m_in_ship?=?0;
????????this.c_in_ship?=?0;
????????this.direction?=?0;
????????this.open?=?5;??????????????????????//每一個狀態初始有五個可擴展狀態,代表了五種運輸方案。
- 上一篇:android項目中讀取ini配置文件
- 下一篇:蜘蛛紙牌游戲java源碼
評論
共有 條評論