Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "stdafx.h"
- #include <stdio.h>
- #include <windows.h>
- int field1[64];
- int field2[64];
- int c = 0;
- void attack(int i)
- {
- int x = i / 8;
- int y = i % 8;
- if (++field2[i] == 1)
- c++;
- if ((x - 2) >= 0)
- {
- if ((y - 1) >= 0)
- {
- if (++(*(field2 + 8*(x-2) + (y-1))) == 1)
- c++;
- }
- if ((y + 1) <= 7)
- {
- if (++(*(field2 + 8*(x-2) + (y+1))) == 1)
- c++;
- }
- }
- if ((x + 2) <= 7)
- {
- if ((y - 1) >= 0)
- {
- if (++(*(field2 + 8*(x+2) + (y-1))) == 1)
- c++;
- }
- if ((y + 1) <= 7)
- {
- if (++(*(field2 + 8*(x+2) + (y+1))) == 1)
- c++;
- }
- }
- if ((y - 2) >= 0)
- {
- if ((x - 1) >= 0)
- {
- if (++(*(field2 + (y-2) + 8*(x-1))) == 1)
- c++;
- }
- if ((x + 1) <= 7)
- {
- if (++(*(field2 + (y-2) + 8*(x+1))) == 1)
- c++;
- }
- }
- if ((y + 2) <= 7)
- {
- if ((x - 1) >= 0)
- {
- if (++(*(field2 + (y+2) + 8*(x-1))) == 1)
- c++;
- }
- if ((x + 1) <= 7)
- {
- if (++(*(field2 + (y+2) + 8*(x+1))) == 1)
- c++;
- }
- }
- }
- void unattack(int i)
- {
- int x = i / 8;
- int y = i % 8;
- if (--field2[i] == 0)
- c--;
- if ((x - 2) >= 0)
- {
- if ((y - 1) >= 0)
- {
- if (--(*(field2 + 8*(x-2) + (y-1))) == 0)
- c--;
- }
- if ((y + 1) <= 7)
- {
- if (--(*(field2 + 8*(x-2) + (y+1))) == 0)
- c--;
- }
- }
- if ((x + 2) <= 7)
- {
- if ((y - 1) >= 0)
- {
- if (--(*(field2 + 8*(x+2) + (y-1))) == 0)
- c--;
- }
- if ((y + 1) <= 7)
- {
- if (--(*(field2 + 8*(x+2) + (y+1))) == 0)
- c--;
- }
- }
- if ((y - 2) >= 0)
- {
- if ((x - 1) >= 0)
- {
- if (--(*(field2 + (y-2) + 8*(x-1))) == 0)
- c--;
- }
- if ((x + 1) <= 7)
- {
- if (--(*(field2 + (y-2) + 8*(x+1))) == 0)
- c--;
- }
- }
- if ((y + 2) <= 7)
- {
- if ((x - 1) >= 0)
- {
- if (--(*(field2 + (y+2) + 8*(x-1))) == 0)
- c--;
- }
- if ((x + 1) <= 7)
- {
- if (--(*(field2 + (y+2) + 8*(x+1))) == 0)
- c--;
- }
- }
- }
- void prinfields()
- {
- for (int i = 0; i < 64; i++)
- {
- printf("%d ", field1[i]);
- if ((i+1) % 8 == 0)
- printf("\n");
- }
- printf("\n");
- for (int i = 0; i < 64; i++)
- {
- printf("%d ", field2[i]);
- if ((i+1) % 8 == 0)
- printf("\n");
- }
- printf("%d", c);
- printf("\n\n\n");
- }
- bool proverka(int i)
- {
- bool p = 1;
- for (int j = i / 8 - 3; (j < i / 8 + 8) && p; j++)
- if (field2[j] == 0)
- p = 0;
- return p;
- }
- void search(int k, int index)
- {
- if (k <= 12)
- {
- for (int i = index; i < 55 && c < 64; i++)
- {
- if (i % 8 == 7)
- i++;
- else
- if (proverka(i) || k < 12)
- {
- field1[i] = 1;
- attack(i);
- search(k+1, i+1);
- if (k == 12 && c == 64)
- prinfields();
- field1[i] = 0;
- unattack(i);
- }
- else
- break;
- }
- }
- }
- int _tmain(int argc, _TCHAR* argv[])
- {
- for (int i = 0; i < 64; i++)
- {
- field1[i] = 0;
- field2[i] = 0;
- }
- search(1, 9);
- system("pause");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement