Advertisement
EmanueleBonin

Queen attack II

Mar 25th, 2020
696
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.56 KB | None | 0 0
  1. #include <assert.h>
  2. #include <limits.h>
  3. #include <math.h>
  4. #include <stdbool.h>
  5. #include <stddef.h>
  6. #include <stdint.h>
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <string.h>
  10.  
  11. char* readline();
  12. char** split_string(char*);
  13.  
  14. // Complete the queensAttack function below.
  15. int queensAttack(int n, int k, int r_q, int c_q, int obstacles_rows, int obstacles_columns, int** obstacles) {
  16.    
  17.     // Calcolo intercetta asse x Diagonale Secondaria
  18.     int X_DS_intercept = c_q - r_q;
  19.     // Calcolo intercetta asse y Diagonale Secondaria
  20.     int Y_DS_intercept = -X_DS_intercept;
  21.  
  22.     // Calcolo intercetta asse x = asse y Diagonale Principale
  23.     int X_DP_intercept = c_q + r_q;
  24.     int Y_DP_intercept = X_DP_intercept;
  25.    
  26.     // Calcolo celle Diagonale Secondaria (senza ostacoli)
  27.     int DS = (X_DS_intercept>=0?n-X_DS_intercept:n+X_DS_intercept)-1;
  28.     // Calcolo celle Diagonale Principale (senza ostacoli)
  29.     int DP = (X_DP_intercept>n?2*n-X_DP_intercept+1:X_DP_intercept-1)-1;
  30.     int AX=n-1, AY=n-1;
  31.     int or, oc;
  32.     int obsXTo  = 0, obsXFrom   = n+1;
  33.     int obsYTo  = 0, obsYFrom   = n+1;
  34.     int obsDSXTo= -1, obsDSXFrom= -1;
  35.     int obsDPXTo= -1, obsDPXFrom= -1;
  36.  
  37.  
  38.  
  39.  
  40.  
  41.     for (int i=0; i< obstacles_rows; i++){
  42.         or = obstacles[i][0];
  43.         oc = obstacles[i][1];
  44.         //printf("ostacolo %d %d\n", or, oc);
  45.  
  46.         if (or==r_q){
  47.             // l'ostacolo si trova sulla stessa riga della regina
  48.             if (oc > c_q && oc < obsXFrom){
  49.                 //AX-=(obsXFrom-oc);
  50.                 obsXFrom = oc;
  51.             } else if (oc < c_q && oc > obsXTo){
  52.                 //AX-=(oc-obsXTo);
  53.                 obsXTo = oc;
  54.             }  
  55.         } else if(oc==c_q){
  56.             // l'ostacolo si trova sulla stessa colonna della regina
  57.             if (or > r_q && or < obsYFrom){
  58.                 //AY-=(obsYFrom-or);
  59.                 obsYFrom = or;
  60.             } else if (or < r_q && or > obsYTo){
  61.                 //AY-=(or-obsYTo);
  62.                 obsYTo = or;
  63.             }  
  64.         } else if(or==oc+Y_DS_intercept){
  65.             // l'ostacolo si trova sulla stessa diagonale secondaria della regina
  66.             if(oc<c_q && or<r_q){ // lower left
  67.                 obsDSXTo = (obsDSXTo < oc? oc: obsDSXTo);
  68.             } else if(oc >c_q && or>r_q){ // upper right
  69.                 obsDSXFrom = (obsDSXFrom > oc || obsDSXFrom == -1 ? oc :obsDSXFrom) ;
  70.             }
  71.         } else if(or==-oc+Y_DP_intercept){
  72.             // l'ostacolo si trova sulla stessa diagonale principale della regina
  73.             if(oc<c_q && or>r_q){ // Upper left
  74.                 obsDPXTo = (obsDPXTo ==-1 || obsDPXTo < oc? oc: obsDPXTo);
  75.             } else if(oc>c_q && or<r_q){ // lower right
  76.                 obsDPXFrom = (obsDPXFrom ==-1 || obsDPXFrom > oc? oc: obsDPXFrom);
  77.             }
  78.         }
  79. /*
  80. 88587 9
  81. 20001 20003
  82. 20001 20002
  83. 20001 20004
  84. 20000 20003
  85. 20002 20003
  86. 20000 20004
  87. 20000 20002
  88. 20002 20004
  89. 20002 20002
  90. 564 323
  91.  
  92. ------
  93. 0
  94.  
  95. manca diagonale principale
  96. */
  97.  
  98.  
  99.  
  100.     }
  101.     int XX=0;
  102.     XX+=obsXFrom-c_q-1;
  103.  
  104.     XX+=c_q-obsXTo-1;
  105.  
  106.     AX=XX;
  107.  
  108.  
  109.     int YY = 0;
  110.     YY+=obsYFrom-r_q-1;
  111.  
  112.  
  113.     YY+= r_q - obsYTo -1;
  114.  
  115.     AY=YY;
  116.  
  117.     int DDS=0;
  118.     if (obsDSXFrom !=-1 ) // upper right
  119.         DS-=((Y_DS_intercept<0?n:n-Y_DS_intercept)-obsDSXFrom+1);
  120.     //DDS+= obsDSXFrom - c_q - 1;
  121.  
  122.  
  123.     if (obsDSXTo !=-1) // lower left
  124.         DS-=(obsDSXTo+(Y_DS_intercept<0?Y_DS_intercept:0));
  125.         DDS+= c_q - obsDSXTo - 1;
  126.  
  127.     int DDP = 0;
  128.     if (obsDPXFrom !=-1 ) // lower right
  129.         DP-=(Y_DP_intercept<=n?Y_DP_intercept-obsDPXFrom:n-obsDPXFrom+1);
  130.  
  131.     if (obsDPXTo !=-1) // upper left
  132.         DP-=(obsDPXTo + (Y_DP_intercept<=n?0:n-Y_DP_intercept+1));
  133.  
  134.     return DP + DS + AX + AY ;
  135. }
  136.  
  137. int main()
  138. {
  139.     FILE* fptr = fopen(getenv("OUTPUT_PATH"), "w");
  140.  
  141.     char** nk = split_string(readline());
  142.  
  143.     char* n_endptr;
  144.     char* n_str = nk[0];
  145.     int n = strtol(n_str, &n_endptr, 10);
  146.  
  147.     if (n_endptr == n_str || *n_endptr != '\0') { exit(EXIT_FAILURE); }
  148.  
  149.     char* k_endptr;
  150.     char* k_str = nk[1];
  151.     int k = strtol(k_str, &k_endptr, 10);
  152.  
  153.     if (k_endptr == k_str || *k_endptr != '\0') { exit(EXIT_FAILURE); }
  154.  
  155.     char** r_qC_q = split_string(readline());
  156.  
  157.     char* r_q_endptr;
  158.     char* r_q_str = r_qC_q[0];
  159.     int r_q = strtol(r_q_str, &r_q_endptr, 10);
  160.  
  161.     if (r_q_endptr == r_q_str || *r_q_endptr != '\0') { exit(EXIT_FAILURE); }
  162.  
  163.     char* c_q_endptr;
  164.     char* c_q_str = r_qC_q[1];
  165.     int c_q = strtol(c_q_str, &c_q_endptr, 10);
  166.  
  167.     if (c_q_endptr == c_q_str || *c_q_endptr != '\0') { exit(EXIT_FAILURE); }
  168.  
  169.     int** obstacles = malloc(k * sizeof(int*));
  170.  
  171.     for (int i = 0; i < k; i++) {
  172.         *(obstacles + i) = malloc(2 * (sizeof(int)));
  173.  
  174.         char** obstacles_item_temp = split_string(readline());
  175.  
  176.         for (int j = 0; j < 2; j++) {
  177.             char* obstacles_item_endptr;
  178.             char* obstacles_item_str = *(obstacles_item_temp + j);
  179.             int obstacles_item = strtol(obstacles_item_str, &obstacles_item_endptr, 10);
  180.  
  181.             if (obstacles_item_endptr == obstacles_item_str || *obstacles_item_endptr != '\0') { exit(EXIT_FAILURE); }
  182.  
  183.             *(*(obstacles + i) + j) = obstacles_item;
  184.         }
  185.     }
  186.  
  187.     int obstacles_rows = k;
  188.     int obstacles_columns = 2;
  189.  
  190.     int result = queensAttack(n, k, r_q, c_q, obstacles_rows, obstacles_columns, obstacles);
  191.  
  192.     fprintf(fptr, "%d\n", result);
  193.  
  194.     fclose(fptr);
  195.  
  196.     return 0;
  197. }
  198.  
  199. char* readline() {
  200.     size_t alloc_length = 1024;
  201.     size_t data_length = 0;
  202.     char* data = malloc(alloc_length);
  203.  
  204.     while (true) {
  205.         char* cursor = data + data_length;
  206.         char* line = fgets(cursor, alloc_length - data_length, stdin);
  207.  
  208.         if (!line) { break; }
  209.  
  210.         data_length += strlen(cursor);
  211.  
  212.         if (data_length < alloc_length - 1 || data[data_length - 1] == '\n') { break; }
  213.  
  214.         size_t new_length = alloc_length << 1;
  215.         data = realloc(data, new_length);
  216.  
  217.         if (!data) { break; }
  218.  
  219.         alloc_length = new_length;
  220.     }
  221.  
  222.     if (data[data_length - 1] == '\n') {
  223.         data[data_length - 1] = '\0';
  224.     }
  225.  
  226.     data = realloc(data, data_length);
  227.  
  228.     return data;
  229. }
  230.  
  231. char** split_string(char* str) {
  232.     char** splits = NULL;
  233.     char* token = strtok(str, " ");
  234.  
  235.     int spaces = 0;
  236.  
  237.     while (token) {
  238.         splits = realloc(splits, sizeof(char*) * ++spaces);
  239.         if (!splits) {
  240.             return splits;
  241.         }
  242.  
  243.         splits[spaces - 1] = token;
  244.  
  245.         token = strtok(NULL, " ");
  246.     }
  247.  
  248.     return splits;
  249. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement