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

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

时间:2016-05-23 17:13:54      阅读:172      评论:0      收藏:0      [点我收藏+]

标签:

最近玩同一个世界,才几关就把3次提示用完了,十分气愤,

于是写了程序来解~~~

o(^▽^)o

 

以2-1为例子,题目如下

技术分享

 

以0代表白色,1代表黑色,抽象的就是这样的

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

并发N个进程去走, 走一步,就判断剩下的白色或者黑色的是否能连成一条线,

 下面上程序

%...............................

start_now(Nowdata, Begnum, Endnum) -> P1 = spawn(fun() ->start_one(Nowdata, Begnum, Endnum, 0) end),    %假设最后全黑或者全白 P3 = spawn(fun() ->start_one(Nowdata, Begnum, Endnum, 1) end), loop([P1, P3]). start_one(Nowdata, Begnum, Endnum, Flag) -> Pid = self(), start_two(Pid, Nowdata, Begnum, Endnum, Flag), wait_loop(Pid). start_two(Pid, Nowdata, Begnum, Endnum, Flag) -> Lin = splite_list(Nowdata, Endnum, Flag), spawn(fun() -> oneword_two:start(Pid, Nowdata, Begnum, Endnum, Lin) end). %并发进程去走

%...............................

 

然后就是走和判断了

%...............................

start_try(Fid, Data, Loadlist, Nownum, Endnum, Lin) -> if [Nownum|Loadlist] == [3,9,8,7,13,19,20,14,15,16,17,23,29,28,22,21,27,26,25,31,32, 33,34,35,36,42] -> io:format("here is tttttt Loadlist:~p, Nownum:~p~n,Data:~p~n", [Loadlist, Nownum, Data]); true -> ok end, % io:format("here is 2222 Loadlist:~p, Nownum:~p,Data:~p~n", [Loadlist, Nownum, Data]), try link(Fid) of true -> case maybe_end(Endnum, [Nownum|Loadlist], Lin) of false -> false; true -> {ok, Enlist, Data2} = move(Data, Loadlist, Nownum),       %%走一个位置 case oneword_check:check_ok(Fid, Data2, Endnum) of        %%判断剩下的是否能连成一条线 {right, Line} -> ?SERVERNAME ! {ok, {[Nownum|Loadlist], Line}}; wrong -> % oneword_lib:sleep(1), [spawn(fun() -> start_try(Fid, Data2, [Nownum|Loadlist], X, Endnum, Lin) end)|| X <- Enlist]  %%如果不能,再并发N个进程去走下一步~~ end end catch _:_ -> ok end.

%...............................

  

然后结果就是很简单了下

7> rp(A).
[0,1,0,1,0,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,0,1,1,1,0,0,0,0,0,
 0]
ok
8> oneword:start(A,2,15).
<0.62.0>
 ----------------------------
| 0 | 1 | 0 | 1 | 0 |
 ----------------------------
| 0 | 1 | 0 | 1 | 0 |
 ----------------------------
| 1 | 0 | 1 | 0 | 1 |
 ----------------------------
| 0 | 1 | 0 | 1 | 0 |
 ----------------------------
| 0 | 1 | 1 | 1 | 0 |
 ----------------------------
| 0 | 0 | 0 | 0 | 0 |
 ----------------------------
here is 111 Begnum:2,Endnum:15, Lin:false
here is 111 Begnum:2,Endnum:15, Lin:true
the answere is:                %答案1,
 line1:[2,3,8,9,14,13,12,11]
 line2:[15,14,19,24,23,22,17,12,7,8,3,4]
9> oneword:start(A,2,15).
<0.8545.1>
 ----------------------------
| 0 | 1 | 0 | 1 | 0 |
 ----------------------------
| 0 | 1 | 0 | 1 | 0 |
 ----------------------------
| 1 | 0 | 1 | 0 | 1 |
 ----------------------------
| 0 | 1 | 0 | 1 | 0 |
 ----------------------------
| 0 | 1 | 1 | 1 | 0 |
 ----------------------------
| 0 | 0 | 0 | 0 | 0 |
 ----------------------------
here is 111 Begnum:2,Endnum:15, Lin:false
here is 111 Begnum:2,Endnum:15, Lin:true
the answere is:                %%答案2
 line1:[2,7,12,17,22,23,24,19,14,9,4]
 line2:[15,14,13,12,11]

 1,2,3,4,5,

   6,7,8,9,10,

   11, 12,13,14, 15

 ........

答案的序号是格子的编号代表,格子的顺序,从1开始,如下

发现这种答案不是唯一的~~~~

 

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

标签:

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

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