All about flooble | fun stuff | Get a free chatterbox | Free JavaScript | Avatars
 perplexus dot info

 Firing Line (Posted on 2004-08-25)
There is a group of N soldiers arranged in a straight line, standing side by side. Soldier number 1 is at the extreme left and soldier "N" is at the extreme right. Each soldier has a rifle that can be fired only once, a primitive timer, understands a finite list of commands, and can exist in a finite number of states, like a finite state machine.

Each soldier has the ability to communicate only with the two adjacent soldiers, and has no means of communication with more distant soldiers. The i-th soldier can not see or hear any signals given by the (i+2)th soldier, for example. There are no radios, cell phones, or megaphones.

Your mission as the commander is to devise an algorithm by which all soldiers fire their weapons simultaneously. Soldiers 1 and N are aware of the fact that they are "different" in that they each have only one neighbor. Other than that, however, the soldiers are initially all identical. The algorithm has to work for any value of N>2.

The primitive timers are synchronized and tick off once a second. Once a soldier receives new information, the earliest he can respond in any way is on the next tick of the clock. (I would say he/she, except that they are all identical). A soldier can give a signal to each neighbor simultaneously, based on the information he received one tick earlier. Whenever a soldier's state changes, his neighbors are aware of this one tick later. At time=0, soldier 1 is given the command to start the firing procedure

1. Devise an algorithm that results in all N soldiers firing simultaneously
2. As a function of N, how many clock ticks does this take?

 See The Solution Submitted by Larry Rating: 3.3333 (3 votes)

Comments: ( Back to comment list | You must be logged in to post comments.)
 Jacques Mazoyer's Solution | Comment 11 of 14 |
` `
`Here is my implementation of Jacques Mazoyer's solution in Perl.`
`For all values of n I have run so far, the number of ticks is 2n.`
`# fssp.pluse warnings;use strict;{my \$n = 31;my \$k;my \$i;my \$j;my \$line;my \$lmr;my %inputs;my \$states;`
`#   Read in the state transition table`
`    while(\$line=<STDIN>) {      \$lmr = substr(\$line,0,1) . substr(\$line,2,1) . substr(\$line,4,1);      \$inputs{\$lmr} = substr(\$line,6,1);    }`
`#   Initialize states`
`    \$k = 2*\$n+1;    \$states = "-" x (\$n) . "G";`
`#   Time tick loop`
`    for ( \$j=0 ; \$j<\$k ; \$j++) {      \$lmr = "GG" . substr(\$states,0,1);      \$line = "%";      print substr(\$states,0,\$n)," ",\$j,"  ";`
`#   Soldier loop`
`      for ( \$i=0 ; \$i<\$n ; \$i++) {        \$lmr = substr(\$lmr,1,2) . substr(\$states,\$i+1,1);        if ( exists \$inputs{\$lmr} ) {          \$line = \$inputs{\$lmr};          substr(\$states,\$i,1) = \$line;        }      }      last if \$line eq "%";    }}`
`This is the state transition table - L M R N`
`   L : The previous state of the soldier to my left`
`   M : My previous state`
`   R : The previous state of the soldier to my right`
`   N : My next state`
`The states are {-,A,B,C,F,G} with "-" the quiescent state,"G" the excite state, and "F" the fire state. In myimplementation the state of the soldier to the left ofsoldier #1 and the state of the soldier to right ofsoldier #n is always "G".`
`- - - -- - B -- - C -- - G -- A A A- A B -- A C G- B A G- B B B- B C -- B G B- C - C- C A A- C B G- C C C- C G G- G A G- G B G- G C GA - - GA - A -A - B -A - C -A - G CA A - AA A A AA A B BA A C CA A G BA B - GA B A BA B B BA B C -A C - BA C B BA C G BA G - BA G B GA G C GB - - -B - A -B - B -B - C -B - G -B A - GB A B GB A C CB A G CB B - GB B A AB B B BB B C CB B G BB C - CB C C CB C G GB G - BB G B GB G C GB G G GC - - AC - A -C - B -C - C -C - G GC A - AC A A AC B - -C B A AC B G -C C - CC C A AC C B BC C C CC C G BC G - AC G B GC G C GC G G AG - - CG - A -G - B -G - C -G - G AG A C CG A G CG B - CG B A CG B C BG B G GG C - BG C B BG C G BG G - BG G B GG G C GG G G F`
`Here is the output for n = 31:`
`------------------------------- 0C------------------------------ 1BA----------------------------- 2CGG---------------------------- 3BABC--------------------------- 4CG-CA-------------------------- 5BA-AAG------------------------- 6CG-ABBC------------------------ 7BA--BCCA----------------------- 8CGG--CAAG---------------------- 9BABC-AABBC--------------------- 10CG-C-ABBCCA-------------------- 11BA-C--BCCAAG------------------- 12CG-CA--CAABBC------------------ 13BA-AAG-AABBCCA----------------- 14CG-ABB-ABBCCAAG---------------- 15BA--BG--BCCAABBC--------------- 16CGG-BBC--CAABBCCA-------------- 17BAB-BCCA-AABBCCAAG------------- 18CGG--CAA-ABBCCAABBC------------ 19BABC-AAA--BCCAABBCCA----------- 20CG-C-AAAG--CAABBCCAAG---------- 21BA-C-AABBC-AABBCCAABBC--------- 22CG-C-ABBCC-ABBCCAABBCCA-------- 23BA-C--BCCC--BCCAABBCCAAG------- 24CG-CA--CCCA--CAABBCCAABBC------ 25BA-AAG-CCAAG-AABBCCAABBCCA----- 26CG-ABB-CAABB-ABBCCAABBCCAAG---- 27BA--BG-AABBG--BCCAABBCCAABBC--- 28CGG-BB-ABBBBC--CAABBCCAABBCCA-- 29BAB-BG--BBBCCA-AABBCCAABBCCAAG- 30CGG-BBC-BBCCAA-ABBCCAABBCCAABBA 31BAB-BCC-BCCAAA--BCCAABBCCAABBAC 32CGG--CC--CAAAAG--CAABBCCAABBACB 33BABC-CCA-AAAABBC-AABBCCAABBACB- 34CG-C-CAA-AAABBCC-ABBCCAABBACB-- 35BA-C-AAA-AABBCCC--BCCAABBACB--- 36CG-C-AAA-ABBCCCCA--CAABBACB---- 37BA-C-AAA--BCCCCAAG-AABBACB----- 38CG-C-AAAG--CCCAABB-ABBACB------ 39BA-C-AABBC-CCAABBG--BACB------- 40CG-C-ABBCC-CAABBBBC-GCB-------- 41BA-C--BCCC-AABBBBCCGGB--------- 42CG-CA--CCC-ABBBBCCBAGC--------- 43BA-AAG-CCC--BBBCCBACGBA-------- 44CG-ABB-CCCA-BBCCBACBGCGG------- 45BA--BG-CCAA-BCCBACB-GBABC------ 46CGG-BB-CAAA--CBACB--GCG-CA----- 47BAB-BG-AAAAG-GACB---GBA-AAG---- 48CGG-BB-AAABBAGCB----GCG-ABBC--- 49BAB-BG-AABBACGB-----GBA--BCCA-- 50CGG-BB-ABBACBGC-----GCGG--CAAG- 51BAB-BG--BACB-GBA----GBABC-AABBA 52CGG-BBC-GCB--GCGG---GCG-C-ABBAC 53BAB-BCCGGB---GBABC--GBA-C--BACB 54CGG--CBAGC---GCG-CA-GCG-CA-GCB- 55BABC-GACGBA--GBA-AACGBA-AACGB-- 56CG-CGGCBGCGG-GCG-ACBGCG-ACBGC-- 57BA-GAGB-GBABAGBA-GB-GBA-GB-GBA- 58CGCGCGC-GCGBCGCGCGC-GCGCGC-GCGC 59BGBGBGBGGBGBGGBGBGBGGBGBGBGGBGB 60GGGGGGGGGGGGGGGGGGGGGGGGGGGGGGG 61FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF 62`
` `

Edited on August 28, 2004, 1:17 am

Edited on August 28, 2004, 1:19 am

Edited on August 30, 2004, 12:29 am
 Posted by Bractals on 2004-08-27 23:35:38

 Search: Search body:
Forums (0)