aboutsummaryrefslogtreecommitdiff
path: root/24_game.c
blob: 03f6cd3257dca963f8100cfab2e5662334038586 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
// 679. 24 Game
#include "leetcode.h"

/*
 * given an integer array 'cards' of length 4. you have four cards, each
 * containing a number in the range '[1,9]'. you should arrange the numbers on
 * these cards in a mathematical expression using the operators '[+,-,*,/]' and
 * the parenthesis '(' and ')' to get value 24. you are restricted to the
 * following rules:
 * - the division operation represents real division, not integer division
 * - every operation done is between two numbers. in particular, we cannot use
 * '-' as a unary operator
 * - you cannot concatenate numbers together
 * return true if you can get such expression that evaluates to 24 and false
 * otherwise.
 */

#define EPSILON 0.00000001

bool backtrack(double *cards, int cardsSize) {
  if (cardsSize == 1)
    return fabs(cards[0] - 24) < EPSILON;
  double *new = NULL;
  for (int i = 0; i + 1 < cardsSize; i++)
    for (int j = 0; j < cardsSize; j++) {
      int n = cardsSize - 1, newSize = 0;
      new = (double *)calloc(sizeof(double), n);
      for (int k = 0; k < cardsSize; k++)
        if (k != i && k != j)
          new[newSize++] = cards[k];
      new[newSize++] = cards[i] + cards[j];
      if (backtrack(new, newSize))
        return true;
      newSize--;
      new[newSize++] = cards[i] - cards[j];
      if (backtrack(new, newSize))
        return true;
      newSize--;
      new[newSize++] = cards[j] - cards[i];
      if (backtrack(new, newSize))
        return true;
      newSize--;
      new[newSize++] = cards[i] * cards[j];
      if (backtrack(new, newSize))
        return true;
      newSize--;
      new[newSize++] = cards[i] / cards[j];
      if (backtrack(new, newSize))
        return true;
      newSize--;
      new[newSize++] = cards[j] / cards[i];
      if (backtrack(new, newSize))
        return true;
    }
  if (new)
    free(new);
  return false;
}

bool judgePoint24(int *cards, int cardsSize) {
  double *newCards = (double *)calloc(sizeof(double), cardsSize);
  bool ans = backtrack(newCards, cardsSize);
  free(newCards);
  return ans;
}

int main() {
  int c1[] = {4, 1, 8, 7}, c2[] = {1, 2, 1, 2};
  printf("%d\n", judgePoint24(c1, ARRAY_SIZE(c1))); // expect: 1
  printf("%d\n", judgePoint24(c2, ARRAY_SIZE(c2))); // expect: 0
}