Advertisement
cmiN

broscute

Sep 9th, 2011
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.25 KB | None | 0 0
  1. /**
  2.  * Se cauta punctele de pe diagonale comune, marcand locurile,
  3.  * apoi se face intersectia celorlalte diagonale.
  4.  * O(KNsqrt2)
  5.  */
  6. #include <stdio.h>
  7. #define N 1001
  8. #define K 200001
  9. #define iname "broscute.in"
  10. #define oname "broscute.out"
  11.  
  12.  
  13. int n, mat[N][N], total;
  14. char used[K];
  15.  
  16.  
  17. inline char check(int x, int y)
  18. {
  19.     if (x < 1 || x > n) return 0;
  20.     if (y < 1 || y > n) return 0;
  21.     return 1;
  22. }
  23.  
  24.  
  25. inline void mark(int x, int y, int id)
  26. {
  27.     /* Se numara toate intersectiile cu exceptia celor de pe diagonala comuna. */
  28.     int i = 1, c1, c2, c3, c4, *tmp; // in functie de raza incrementam intersectiile
  29.     char d1, d2, d3, d4; // daca exista sau nu directia respectiva
  30.     char s1, s2, s3, s4; // daca nu am intalnit pe directie vreo sursa
  31.     d1 = d2 = d3 = d4 = 1;
  32.     s1 = s2 = s3 = s4 = 1;
  33.     c1 = c2 = c3 = c4 = 0;
  34.     mat[x][y] = id; // marcam sursa
  35.     while (d1 || d2 || d3 || d4) {
  36.         d1 = check(x + i, y + i);
  37.         d2 = check(x + i, y - i);
  38.         d3 = check(x - i, y - i);
  39.         d4 = check(x - i, y + i);
  40.         if (d1) {
  41.             tmp = &mat[x + i][y + i];
  42.             if (*tmp) {
  43.                 if (*tmp == -1) {
  44.                     ++c1;
  45.                 } else {
  46.                     if (!used[id]) ++total;
  47.                     if (!used[*tmp]) ++total;
  48.                     used[id] = 1;
  49.                     used[*tmp] = 1;
  50.                     s1 = 0;
  51.                 }
  52.             } else {
  53.                 *tmp = -1;
  54.             }
  55.         }
  56.         if (d2) {
  57.             tmp = &mat[x + i][y - i];
  58.             if (*tmp) {
  59.                 if (*tmp == -1) {
  60.                     ++c2;
  61.                 } else {
  62.                     if (!used[id]) ++total;
  63.                     if (!used[*tmp]) ++total;
  64.                     used[id] = 1;
  65.                     used[*tmp] = 1;
  66.                     s2 = 0;
  67.                 }
  68.             } else {
  69.                 *tmp = -1;
  70.             }
  71.         }
  72.         if (d3) {
  73.             tmp = &mat[x - i][y - i];
  74.             if (*tmp) {
  75.                 if (*tmp == -1) {
  76.                     ++c3;
  77.                 } else {
  78.                     if (!used[id]) ++total;
  79.                     if (!used[*tmp]) ++total;
  80.                     used[id] = 1;
  81.                     used[*tmp] = 1;
  82.                     s3 = 0;
  83.                 }
  84.             } else {
  85.                 *tmp = -1;
  86.             }
  87.         }
  88.         if (d4) {
  89.             tmp = &mat[x - i][y + i];
  90.             if (*tmp) {
  91.                 if (*tmp == -1) {
  92.                     ++c4;
  93.                 } else {
  94.                     if (!used[id]) ++total;
  95.                     if (!used[*tmp]) ++total;
  96.                     used[id] = 1;
  97.                     used[*tmp] = 1;
  98.                     s4 = 0;
  99.                 }
  100.             } else {
  101.                 *tmp = -1;
  102.             }
  103.         }
  104.         ++i;
  105.     }
  106.     if (s1 && s3) total += c1 + c3;
  107.     if (s2 && s4) total += c2 + c4;
  108. }
  109.  
  110.  
  111. int main()
  112. {
  113.     int i, k, x, y;
  114.     freopen(iname, "r", stdin);
  115.     freopen(oname, "w", stdout);
  116.     scanf("%d %d", &n, &k);
  117.     for (i = 1; i <= k; ++i) {
  118.         scanf("%d %d", &x, &y);
  119.         mark(x, y, i);
  120.     }
  121.     printf("%d", total);
  122.     fclose(stdin);
  123.     fclose(stdout);
  124.     return 0;
  125. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement