一个比较有难度的题目,怎么用程序解决

abc优希 2010-12-12 10:46:30
过河问题:
有一猎人和一只狼,一个妈妈和她的两个孩子(A和B),一个爸爸和他两个孩子(C和D)要通过一小船过河。
小船一次只能载两个人(狼也算一人),
孩子和狼不能划船,
猎人不在狼伤害所有人,
妈妈们不在爸爸伤害妈妈的孩子,
同样爸爸不在妈妈伤害爸爸的孩子。
...全文
569 25 打赏 收藏 转发到动态 举报
写回复
用AI写文章
25 条回复
切换为时间正序
请发表友善的回复…
发表回复
youxuesjszwddr 2011-01-17
  • 打赏
  • 举报
回复
值得学习啊
gonxi 2011-01-11
  • 打赏
  • 举报
回复
遍列一下值空间就可以了
computerlibin 2011-01-10
  • 打赏
  • 举报
回复
人工智能中见过,不过还是不会
abc优希 2011-01-06
  • 打赏
  • 举报
回复

import java.util.Iterator;
import java.util.Stack;

public class River {

private StringBuffer shore = null;
private StringBuffer thither = null;
private String boat = null;
Stack<String> course = new Stack<String>();
private String top = "";
private boolean flag = false;
private River() {
shore = new StringBuffer("MmmFffNW");
thither = new StringBuffer(8);
boat = "";
}
private boolean licit(String bevy) {
if ((bevy.indexOf('W') != -1) && (bevy.length() > 1)) {
if ((bevy.indexOf('N') == -1))
return false;
}
if ((bevy.indexOf('M') != -1) && (bevy.indexOf('f') != -1)) {
if ((bevy.indexOf('F') == -1))
return false;
}
if ((bevy.indexOf('F') != -1) && (bevy.indexOf('m') != -1)) {
if ((bevy.indexOf('M') == -1))
return false;
}
return true;
}
private boolean licitBoat() {
if (1 == boat.length()) {
if ((boat.indexOf('W') != -1) || (boat.indexOf('m') != -1)
|| (boat.indexOf('f') != -1))
return false;
}
if (2 == boat.length()) {
if ((boat.indexOf('N') == -1) && (boat.indexOf('M') == -1)
&& (boat.indexOf('F') == -1))
return false;
}
return licit(boat);
}
private void go() {
int len = boat.length();
char[] c = boat.toCharArray();
int index = -1;
for (int i = 0; i < len; i++) {
index = shore.toString().indexOf(c[i]);
shore.deleteCharAt(index);
}
thither.append(boat);
}
private void come() {
int len = boat.length();
char[] c = boat.toCharArray();
int index = -1;
for (int i = 0; i < len; i++) {
index = thither.toString().indexOf(c[i]);
thither.deleteCharAt(index);
}
shore.append(boat);
}
public boolean doGo() {
go();
if (licit(shore.toString()) && licit(thither.toString()))
return true;
return false;
}
public boolean doCome() {
come();
if (licit(shore.toString()) && licit(thither.toString()))
return true;
return false;
}
public boolean putBoat(String people) {
boat = people;
return licitBoat();
}
public void organiseGo() {
StringBuffer people = new StringBuffer(2);
char c1 = 0, c2 = 0;
boolean canBoat = false;
int start;
for (int i = 0; i < shore.length() - 1; i++)
for (int j = i + 1; j < shore.length(); j++) {
people.delete(0, 2);
c1 = shore.charAt(i);
people.append(c1);
c2 = shore.charAt(j);
people.append(c2);
if(!course.empty())top = course.peek();
if ((top.indexOf(c1) != -1) && (top.indexOf(c2) != -1))
continue;
canBoat = putBoat(people.toString());
if (canBoat) {
course.push(people.toString());
if (doGo()) {
organiseCome();
}
if (flag) {
return;
} else {
shore.insert(i, c1).insert(j, c2);
start = thither.lastIndexOf(course.peek());
thither.delete(start, start + 2);
course.pop();
}
}
}
for (int i = 0; i < shore.length(); i++) {
people.delete(0, 2);
c1 = shore.charAt(i);
people.append(c1);
top = course.peek();
if ((top.indexOf(c1) != -1) && (top.length() == 1))
continue;
canBoat = putBoat(people.toString());
if (canBoat) {
course.push(people.toString());
if (doGo()) {
organiseCome();
}
if (flag) {
return;
} else {
shore.insert(i, c1);
thither.deleteCharAt(thither.length() - 1);
course.pop();
}
}
}
}

public void organiseCome() {
if (shore.length() == 0) {
System.out.println("恭喜您成功过河!输出结果");
Iterator<String> iter = course.iterator();
int i = 1;
while (iter.hasNext()) {
i = i * (-1);
if (-1 == i)
System.out.print(iter.next() + "去-> <-");
else
System.out.println(iter.next() + "回");
}
flag = true;
return;
}
StringBuffer people = new StringBuffer(2);
char c1 = 0, c2 = 0;
boolean canBoat = false;
int start;
for (int i = 0; i < thither.length(); i++) {
people.delete(0, 2);
c1 = thither.charAt(i);
people.append(c1);
top = course.peek();
if ((top.indexOf(c1) != -1) && (top.length() == 1))
continue;
canBoat = putBoat(people.toString());
if (canBoat) {
course.push(people.toString());
if (doCome()) {
organiseGo();
}

if (flag) {
return;
} else {
course.pop();
thither.insert(i, c1);
shore.deleteCharAt(shore.length() - 1);
}
}
}
for (int i = 0; i < thither.length() - 1; i++)
for (int j = i + 1; j < thither.length(); j++) {
people.delete(0, 2);
c1 = thither.charAt(i);
people.append(c1);
c2 = thither.charAt(j);
people.append(c2);
top = course.peek();
if ((top.indexOf(c1) != -1) && (top.indexOf(c2) != -1))
continue;
canBoat = putBoat(people.toString());
if (canBoat) {
course.push(people.toString());
if (doCome()) {
organiseGo();
}
if (flag) {
return;
} else {
thither.insert(i, c1).insert(j, c2);
start = shore.lastIndexOf(course.peek());
shore.delete(start, start + 2);
course.pop();
}
}
}
}

public static void main(String[] args) {
River pr = new River();
pr.organiseGo();
}
}
------------------------------------------------------
恭喜您成功过河!输出结果
NW去-> <-N回
mN去-> <-WN回
Mm去-> <-M回
FM去-> <-F回
WN去-> <-M回
FM去-> <-F回
fF去-> <-WN回
fN去-> <-N回
WN去-> <-
yqyqyoyo 2011-01-06
  • 打赏
  • 举报
回复
问题解决了,把代码贴出来看看····顺便结贴给分。。
abc优希 2011-01-06
  • 打赏
  • 举报
回复
今天终于把问题给解答了
开心
Debugcool 2010-12-15
  • 打赏
  • 举报
回复
猎人、狼、爸爸、妈妈、A、B、C、D一共有8个对象,可以用一个整数的低8bit来表示(1表示有,0表示无),这样河的两岸的状态就可以用两个整数来保存,预处理出来不合法的状态,然后就可以直接广搜就行了,如果要求效率高的话,可以加上启发式什么的,但是这样的状态显然不是很多,至少少于 256 * 256 = 65535,直接广度优先搜索就能瞬秒出结果了。
wizard_tiger 2010-12-15
  • 打赏
  • 举报
回复
如用回溯法可以吗?
lzc52151 2010-12-13
  • 打赏
  • 举报
回复
化成畫,這是一個最短路徑問題
keeya0416 2010-12-13
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 wizard_tiger 的回复:]
一猎人P
一只狼W
一个妈妈M 和她的两个孩子(A和B)
一个爸爸F 和他两个孩子(C和D)

P和W过河P回来;
P和A过河P和W回来;
M和B过河M回来;
M和F过河F回来;
P和W过河M回来;
F和M过河F回来;
F和C过河P和W回来;
P和D过河P回来;
P和W过河;
[/Quote]
原来如此 佩服
NowDoIT 2010-12-13
  • 打赏
  • 举报
回复
这个用程序应该怎么设计?
pmars 2010-12-13
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 wizard_tiger 的回复:]
一猎人P
一只狼W
一个妈妈M 和她的两个孩子(A和B)
一个爸爸F 和他两个孩子(C和D)

P和W过河P回来;
P和A过河P和W回来;
M和B过河M回来;
M和F过河F回来;
P和W过河M回来;
F和M过河F回来;
F和C过河P和W回来;
P和D过河P回来;
P和W过河;
[/Quote]
如此这般,最后两步没想到!
恩,深搜吧,只是限制比较多!限制多,可减的支就多了!
llongfeng2008 2010-12-13
  • 打赏
  • 举报
回复
做过这种游戏,是一种智力题,头一次听说用程序做呢?学习中……
wizard_tiger 2010-12-13
  • 打赏
  • 举报
回复
一猎人P
一只狼W
一个妈妈M 和她的两个孩子(A和B)
一个爸爸F 和他两个孩子(C和D)

P和W过河P回来;
P和A过河P和W回来;
M和B过河M回来;
M和F过河F回来;
P和W过河M回来;
F和M过河F回来;
F和C过河P和W回来;
P和D过河P回来;
P和W过河;
pmars 2010-12-13
  • 打赏
  • 举报
回复
请lz给一下答案吧,他们是如何过河的?
keeya0416 2010-12-13
  • 打赏
  • 举报
回复
这个题目是不是有问题啊
貌似就没解的
逆向思考下
最后一步分 2 种情况
1. 猎人和狼
2. 爸爸和妈妈
如果是2的话,倒数第2步是谁划船回来的? 所以 2 不成立
如果是1的话,那倒数第2步是猎人划船回来的;这样倒数第3步就是猎人带一个小孩过去的;这样倒数第4步是谁划船回来的? 又不成立了

不知我遗漏了什么东西,望楼主解释下
  • 打赏
  • 举报
回复
人工智能的过河问题,A*算法。
flysnowhite 2010-12-13
  • 打赏
  • 举报
回复
状态图,看过程序算法教材上编过狼、人、白菜的算法。
li88yz 2010-12-13
  • 打赏
  • 举报
回复
高手啊,学习了
sjkof 2010-12-13
  • 打赏
  • 举报
回复
状态图的转换,一旦某个状态是非法的,就可以把这一分支减去
加载更多回复(2)

33,008

社区成员

发帖
与我相关
我的任务
社区描述
数据结构与算法相关内容讨论专区
社区管理员
  • 数据结构与算法社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧