[ create a new paste ] login | about

Link: http://codepad.org/CjJ4pEh4    [ raw code | output | fork | 1 comment ]

mohit_at_codepad - C, pasted on Jul 16:
/**
 * An absent minded professor designed a house of 27 floors
 * but forgot to build any staircaes.
 * Now he has 4 very light weight ladders of length 1, 4, 13 and 14 floors.
 * To go to any floor he use any ladder to climb up and bring other
 * ladders with him. The he uses rest of the ladders to go up or down.
 * (He can not use the already used ladder)
 * Which floors are not accessible to him.
 */
int reachable[28] = {1};

void go(int r, int *arr, int n) {
  if(0 == n) {
    if(1 <= r && r <= 27) reachable[r] = 1;
  } else {
    int *next = arr+1;
    int rem = n-1;
    go(r, next, rem);
    go(r+arr[0], next, rem);
    go(r-arr[0], next, rem);
  }
}

int main() {
  int i;
  int arr[] = {1, 4, 13, 14};
  go(0, arr, 4);
  for(i = 1; i <= 27; ++i) {
    if( !reachable[i] ) printf("%d\n", i);
  }
  return 0;
}

/* EOF */


Output:
1
2
3
4
7
20
21
25


Create a new paste based on this one


Comments:
posted by mohit_at_codepad on Jul 16
Bonus question: What is the minimum length of extra fifth ladder after which at least one floor is still inaccessible. (Ans. is 4)
reply