Advertisement
JoshDreamland

Lights Off

Nov 12th, 2012
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.56 KB | None | 0 0
  1. #include <ncurses.h>
  2. #include <vector>
  3. using std::vector;
  4.  
  5. #define WID 5
  6. #define HGT 5
  7.  
  8. bool grid[HGT][WID];
  9.  
  10. void drawgrid(int x, int y) {
  11.   for (int i = 0; i < HGT; ++i)
  12.   for (int j = 0; j < WID; ++j)
  13.     mvprintw(y+i,x+j,grid[i][j]?"#":" ");
  14. }
  15.  
  16. void getgrid() {
  17.   for (int i = 0; i < HGT; ++i)
  18.   for (int j = 0; j < WID; ++j) {
  19.     move(i,j);
  20.     char c = getch();
  21.     grid[i][j] = (c == '1' || c == 'x' || c == 'X');
  22.     clear();
  23.     drawgrid(0,0);
  24.   }
  25. }
  26.  
  27. struct coord { int x,y; coord(int xx, int yy): x(xx), y(yy) {} };
  28. typedef vector<coord> sv;
  29.  
  30. void doXOR(bool g[HGT][WID], const coord &c) {
  31.   g[c.y][c.x] ^= 1;
  32.   if (c.x > 0) g[c.y][c.x - 1] ^= 1;
  33.   if (c.x < WID-1) g[c.y][c.x + 1] ^= 1;
  34.   if (c.y > 0) g[c.y - 1][c.x] ^= 1;
  35.   if (c.y < HGT-1) g[c.y + 1][c.x] ^= 1;
  36. }
  37.  
  38. bool solution(sv &s) {
  39.   bool sgrid[5][5];
  40.   for (int i = 0; i < HGT; ++i)
  41.   for (int j = 0; j < WID; ++j)
  42.     sgrid[i][j] = grid[i][j];
  43.   for (size_t i = 0; i < s.size(); ++i)
  44.     doXOR(sgrid,s[i]);
  45.   for (int i = 0; i < HGT; ++i)
  46.   for (int j = 0; j < WID; ++j)
  47.     if (sgrid[i][j]) return false;
  48.   return true;
  49. }
  50.  
  51. void nextsolution(sv &s, size_t p) {
  52.   if (s[p].x < WID-1) ++s[p].x;
  53.   else {
  54.     s[p].x = 0;
  55.     if (s[p].y < HGT-1) ++s[p].y;
  56.     else {
  57.       s[p].y = 0;
  58.       if (s.size() > p+1)
  59.         nextsolution(s, p+1);
  60.       else {
  61.         s.push_back(coord(0,0));
  62.         printf("Need at least %lu clicks...\n", s.size());
  63.       }
  64.     }
  65.   }
  66. }
  67.  
  68. #define METHOD 2
  69.  
  70. void printsolution() {
  71.   sv s;
  72.  
  73.   if (METHOD == 1) {
  74.     s.push_back(coord(0,0));
  75.     while (!solution(s)) {
  76.       nextsolution(s,0);
  77.       if (s.size() > WID*HGT) {
  78.         printf("No solution");
  79.         return;
  80.       }
  81.     }
  82.   }
  83.  
  84.   bool sol[WID][HGT];
  85.   for (size_t x = 0; x < WID; ++x)
  86.   for (size_t y = 0; y < HGT; ++y)
  87.     sol[x][y] = 0;
  88.  
  89.   if (METHOD == 2) {
  90.     for (long si = 0; si < (1L << WID*HGT); ++si) {
  91.       s.clear();
  92.       for (long i = 1, i2 = 0; i <= si; i <<= 1, ++i2)
  93.         if (si & i)
  94.           s.push_back(coord(i2 % WID, i2 / WID));
  95.       if (solution(s)) break;
  96.       if (!(si & 0xFFFFF))
  97.         printf("Attempting solution %ld\n", si);
  98.     }
  99.   }
  100.  
  101.   for (size_t i = 0; i < s.size(); ++i)
  102.     sol[s[i].x][s[i].y] = 1;
  103.   for (int y = 0; y < HGT; ++y) {
  104.     for (int x = 0; x < WID; ++x)
  105.       putc(sol[x][y]? 'X' : ' ', stdout);
  106.     putc('\n', stdout);
  107.   }
  108. }
  109.  
  110. int main() {
  111.   initscr();
  112.   timeout(-1);
  113.   getgrid();
  114.   endwin();
  115.   printf("Solution:\n");
  116.   printsolution();
  117.   printf("Thanks for playing\n");
  118.   return 0;
  119. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement