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

Home > Logic
Nine cards (Posted on 2005-10-14) Difficulty: 2 of 5
Nine cards are disposed as shown:
           +---+---+---+
           | a | b | c |
           +---+---+---+
           | d | e | f |
           +---+---+---+
           | g | h | i |
           +---+---+---+

1) There are, at least, two Aces, two Kings, two Queens and two Jacks.
2) Every Ace borders a King and a Queen.
3) Every King borders a Queen and a Jack.
4) Every Queen borders a Jack.
Note: "border" means "touch" horizontally or vertically, not diagonally.

Identify the nine cards.

See The Solution Submitted by pcbouhid    
Rating: 2.7500 (8 votes)

Comments: ( Back to comment list | You must be logged in to post comments.)
computer logic | Comment 8 of 30 |
(In reply to re(2): solution by derek)

The computer program below tries all permutations of "aakkqqjj?", where the ? stands for any one card. an array is used to indicate the positions which are considered adjacent to a particular position, with the 0 element of each row representing how many adjacencies are within that row. (The row represents the position whose adjacencies are needed.)

The same four rotated versions were found as found by derek, but each was repeated 3 times, as any of the three queens could be the one represented by the ?.  When a hit is found, the logic is gone through again to assure that the substitution of a specific character for the ? did not create new needs (the Q requiring an adjacent J, whereas a ? did not require anything in particular).

DECLARE SUB permute (a$)
CLS
a$ = "aakkqqjj?": h$ = a$

cnct(1, 0) = 2: cnct(1, 1) = 2: cnct(1, 2) = 4
cnct(3, 0) = 2: cnct(3, 1) = 2: cnct(3, 2) = 6
cnct(7, 0) = 2: cnct(7, 1) = 8: cnct(7, 2) = 4
cnct(9, 0) = 2: cnct(9, 1) = 6: cnct(9, 2) = 8
cnct(2, 0) = 3: cnct(2, 1) = 1: cnct(2, 2) = 5: cnct(2, 3) = 3
cnct(4, 0) = 3: cnct(4, 1) = 1: cnct(4, 2) = 5: cnct(4, 3) = 7
cnct(6, 0) = 3: cnct(6, 1) = 9: cnct(6, 2) = 5: cnct(6, 3) = 3
cnct(8, 0) = 3: cnct(8, 1) = 9: cnct(8, 2) = 5: cnct(8, 3) = 7
cnct(5, 0) = 4: cnct(5, 1) = 2: cnct(5, 2) = 4: cnct(5, 3) = 6: cnct(5, 4) = 8

DO
  good = 1
  a2$ = a$
  FOR p = 1 TO 9
   b$ = ""
   SELECT CASE MID$(a2$, p, 1)
     CASE "a"
       FOR i = 1 TO cnct(p, 0)
         b$ = b$ + MID$(a2$, cnct(p, i), 1)
       NEXT
       IF INSTR(b$, "k") = 0 THEN
         IF INSTR(b$, "?") THEN
           ix = INSTR(a2$, "?")
           MID$(a2$, ix, 1) = "k"
         ELSE
           good = 0: EXIT FOR
         END IF
       END IF
       IF INSTR(b$, "q") = 0 THEN
         IF INSTR(b$, "?") > 0 AND INSTR(a2$, "?") > 0 THEN
           ix = INSTR(a2$, "?")
           MID$(a2$, ix, 1) = "q"
         ELSE
           good = 0: EXIT FOR
         END IF
       END IF
     CASE "k"
       FOR i = 1 TO cnct(p, 0)
         b$ = b$ + MID$(a2$, cnct(p, i), 1)
       NEXT
       IF INSTR(b$, "j") = 0 THEN
         IF INSTR(b$, "?") THEN
           ix = INSTR(a2$, "?")
           MID$(a2$, ix, 1) = "j"
         ELSE
           good = 0: EXIT FOR
         END IF
       END IF
       IF INSTR(b$, "q") = 0 THEN
         IF INSTR(b$, "?") > 0 AND INSTR(a2$, "?") > 0 THEN
           ix = INSTR(a2$, "?")
           MID$(a2$, ix, 1) = "q"
         ELSE
           good = 0: EXIT FOR
         END IF
       END IF
     CASE "q"
       FOR i = 1 TO cnct(p, 0)
         b$ = b$ + MID$(a2$, cnct(p, i), 1)
       NEXT
       IF INSTR(b$, "j") = 0 THEN
         IF INSTR(b$, "?") THEN
           ix = INSTR(a2$, "?")
           MID$(a2$, ix, 1) = "j"
         ELSE
           good = 0: EXIT FOR
         END IF
       END IF
   END SELECT
  NEXT p
  IF good THEN

    FOR p = 1 TO 9
     b$ = ""
     SELECT CASE MID$(a2$, p, 1)
       CASE "a"
         FOR i = 1 TO cnct(p, 0)
           b$ = b$ + MID$(a2$, cnct(p, i), 1)
         NEXT
         IF INSTR(b$, "k") = 0 THEN
           IF INSTR(b$, "?") THEN
             ix = INSTR(a2$, "?")
             MID$(a2$, ix, 1) = "k"
           ELSE
             good = 0: EXIT FOR
           END IF
         END IF
         IF INSTR(b$, "q") = 0 THEN
           IF INSTR(b$, "?") THEN
             ix = INSTR(a2$, "?")
             MID$(a2$, ix, 1) = "q"
           ELSE
             good = 0: EXIT FOR
           END IF
         END IF
       CASE "k"
         FOR i = 1 TO cnct(p, 0)
           b$ = b$ + MID$(a2$, cnct(p, i), 1)
         NEXT
         IF INSTR(b$, "j") = 0 THEN
           IF INSTR(b$, "?") THEN
             ix = INSTR(a2$, "?")
             MID$(a2$, ix, 1) = "j"
           ELSE
             good = 0: EXIT FOR
           END IF
         END IF
         IF INSTR(b$, "q") = 0 THEN
           IF INSTR(b$, "?") THEN
             ix = INSTR(a2$, "?")
             MID$(a2$, ix, 1) = "q"
           ELSE
             good = 0: EXIT FOR
           END IF
         END IF
       CASE "q"
         FOR i = 1 TO cnct(p, 0)
           b$ = b$ + MID$(a2$, cnct(p, i), 1)
         NEXT
         IF INSTR(b$, "j") = 0 THEN
           IF INSTR(b$, "?") THEN
             ix = INSTR(a2$, "?")
             MID$(a2$, ix, 1) = "j"
           ELSE
             good = 0: EXIT FOR
           END IF
         END IF
     END SELECT
    NEXT p

    IF good THEN
       rw1 = (ct \ 4) * 5 + 1
       cl1 = (ct MOD 4) * 10 + 1
     
       ct = ct + 1

       LOCATE rw1, cl1: PRINT LEFT$(a2$, 3);
       LOCATE rw1 + 1, cl1: PRINT MID$(a2$, 4, 3)
       LOCATE rw1 + 2, cl1: PRINT RIGHT$(a2$, 3)
       LOCATE rw1 + 3, cl1: PRINT a$
       PRINT
    END IF

  END IF
  permute a$
LOOP UNTIL a$ = h$


  Posted by Charlie on 2005-10-14 15:25:20
Please log in:
Login:
Password:
Remember me:
Sign up! | Forgot password


Search:
Search body:
Forums (0)
Newest Problems
Random Problem
FAQ | About This Site
Site Statistics
New Comments (2)
Unsolved Problems
Top Rated Problems
This month's top
Most Commented On

Chatterbox:
Copyright © 2002 - 2017 by Animus Pactum Consulting. All rights reserved. Privacy Information