码迷,mamicode.com
首页 > 其他好文 > 详细

同一个世界(三生梦醒)

时间:2016-05-31 12:18:11      阅读:250      评论:0      收藏:0      [点我收藏+]

标签:

前段时间写了同一个世界的,链接如下

同一个世界(erlang解题答案)

这次带来了三生梦醒的解题答案和思路,

 

首先看题目

技术分享

 

还是用1代表黑色,0代表白色,用2代表黄点,x代表空白

 

抽象就是这样

----------------
| 1 | 1 | 1 | 1 |
----------------
| 1 | 0 | 0 | 1 |
----------------
| 0 | 1 | 1 | 1 |
----------------
| 1 | 0 | 0 | 0 |
----------------
| 1 | 1 | 1 | 1 |
----------------

 

这次因为每个点走的步数定了,所以暴力的使用轮询来解

把起点能走的所有路径找出来,(注意:这次的路径可以重复~~)

-module(oneword_three).

%..........省略

start(Data, {S, Scnt}, {E, Ecnt}) ->
    Pid = self(),
    Load_data = [{Num, List} || {Num, _V, List} <- Data],
    Val_data = [{Num1, V1} || {Num1, V1, _List1} <- Data, V1 =/= []],
    Tab1 = ets:new(?TAB, [public, duplicate_bag, {write_concurrency, true}]),
    Tab2 = ets:new(?TAB, [public, duplicate_bag, {write_concurrency, true}]),
    spawn(fun() -> start_try(Pid, Load_data, Tab1, S, Scnt) end),        %并发去尝试所有的路径
    spawn(fun() -> start_try(Pid, Load_data, Tab2, E, Ecnt) end),
    do_loop(Val_data, Tab1, Tab2, 2).

%..........省略

start_try2(Pid, _Load_data, Tab, Loads, Nownum, 0) ->
    ets:insert(Tab, {?SIGNAL, [Nownum|Loads]}),      %走完了就把路径存表里面
    Pid ! stop;

start_try2(Pid, Load_data, Tab, Loads, Nownum, Scnt) ->
%    ets:insert(Tab, {?SIGNAL, [Nownum|Loads]}),
    {Nownum, List} = lists:keyfind(Nownum, 1, Load_data),
    Pids = [spawn(fun() -> start_try2(Pid, Load_data, Tab, [Nownum|Loads], X, Scnt-1) end) || X <- List],
    Pid ! {start, erlang:length(Pids)},
    Pid ! stop.

 

然后就是取表里面的路径轮询,

%..........省略


start(Val_data, Tab1, Tab2) -> Pid = self(), Yellow_num = [Num || {Num, V} <- Val_data, V == 2], Slist = [ Z || {_N, Z} <- ets:tab2list(Tab1)], Elist = [ Z1 || {_N1, Z1} <- ets:tab2list(Tab2)], % io:format("here is Slist:~p, Elist:~p~n", [Slist, Elist]), _P = [spawn_link(fun() -> check(Pid, Yellow_num, Val_data, X, Y) end)|| X <- Slist, Y <- Elist],    %轮询去所有的数据 wait_loop(). wait_loop() -> receive Any -> io:format("this is a erorr msg:~p~n", [Any]) end, wait_loop(). check(Pid, Yellow_num, Val_data, X, Y) -> X1 = lists:reverse(X), Y1 = lists:reverse(Y), L = X1 ++ Y1, case check_all(Yellow_num, L) of            %首先要求所有的黄点都走到 ok -> case check_ok(Val_data, X1, Y1) of      %check结果是否是全1或者全0 ok -> ?SERVERNAME ! {ok, Pid, X1, Y1}; no -> no end; no -> no end.
%..........省略

 

然后就出结果了

13> oneword:start(A,{12,5},{13,5}).               
<0.6761.106>
 ----------------
| 1 | 1 | 1 | 1 |
 ----------------
| 1 | 0 | 0 | 1 |
 ----------------
| 0 | 1 | 1 | 1 |
 ----------------
| 1 | 0 | 0 | 0 |
 ----------------
| 1 | 1 | 1 | 1 |
 ----------------
the answere is:
 line1:[12,11,10,6,7,11]
 line2:[13,9,10,14,15,16]

 

tips:

如果有3个起点,可以先随便走一个,猜其他的2个,如下

技术分享

 

走一下下面步骤3的,变成这样

技术分享

然后再解答

16> oneword:start(A,{5,5},{8,5}).  
<0.11334.106>
 ----------------
| X | 1 | 1 | X |
 ----------------
| 1 | 0 | 0 | 1 |
 ----------------
| 1 | 0 | 0 | 1 |
 ----------------
| 1 | 1 | 1 | 1 |
 ----------------
| X | 0 | 0 | X |
 ----------------
the answere is:
 line1:[5,6,10,14,18,14]
 line2:[8,7,11,15,19,15]

答案就出来了

 

同一个世界(三生梦醒)

标签:

原文地址:http://www.cnblogs.com/tudou008/p/5545229.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!