MrSlippery

Solving 8-Queens with KLEE

Feb 18th, 2016
336
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <klee/klee.h>
  2.  
  3. #define N 8
  4. unsigned char queens[N];
  5.  
  6. int abs(int x)
  7. {
  8.   return x >= 0 ? x: -x;
  9. }
  10.  
  11. int attack()
  12. {
  13.   int i, j;
  14.   for (i = 0; i < N; i++) {
  15.     for (j = i + 1; j < N; j++) {
  16.       if ((queens[i] == queens[j]) ||
  17.           (abs(queens[i] - queens[j]) == j - i)) {
  18.         return 1;
  19.       }
  20.     }
  21.   }
  22.   return 0;
  23. }
  24.  
  25. int main(int argc, char *argv[])
  26. {
  27.   int i;
  28.   klee_make_symbolic(&queens[0], sizeof(unsigned char) * N, "queens");
  29.   for (i = 0; i < N; i++) {
  30.     queens[i] %= N;
  31.   }
  32.   klee_assert(attack());
  33.   return 0;
  34. }
Advertisement