Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <assert.h>
- #include <limits.h>
- #include <math.h>
- #include <stdbool.h>
- #include <stddef.h>
- #include <stdint.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- char* readline();
- char** split_string(char*);
- // Complete the queensAttack function below.
- int queensAttack(int n, int k, int r_q, int c_q, int obstacles_rows, int obstacles_columns, int** obstacles) {
- // Calcolo intercetta asse x Diagonale Secondaria
- int X_DS_intercept = c_q - r_q;
- // Calcolo intercetta asse y Diagonale Secondaria
- int Y_DS_intercept = -X_DS_intercept;
- // Calcolo intercetta asse x = asse y Diagonale Principale
- int X_DP_intercept = c_q + r_q;
- int Y_DP_intercept = X_DP_intercept;
- // Calcolo celle Diagonale Secondaria (senza ostacoli)
- int DS = (X_DS_intercept>=0?n-X_DS_intercept:n+X_DS_intercept)-1;
- // Calcolo celle Diagonale Principale (senza ostacoli)
- int DP = (X_DP_intercept>n?2*n-X_DP_intercept+1:X_DP_intercept-1)-1;
- int AX=n-1, AY=n-1;
- int or, oc;
- int obsXTo = 0, obsXFrom = n+1;
- int obsYTo = 0, obsYFrom = n+1;
- int obsDSXTo= -1, obsDSXFrom= -1;
- int obsDPXTo= -1, obsDPXFrom= -1;
- for (int i=0; i< obstacles_rows; i++){
- or = obstacles[i][0];
- oc = obstacles[i][1];
- //printf("ostacolo %d %d\n", or, oc);
- if (or==r_q){
- // l'ostacolo si trova sulla stessa riga della regina
- if (oc > c_q && oc < obsXFrom){
- //AX-=(obsXFrom-oc);
- obsXFrom = oc;
- } else if (oc < c_q && oc > obsXTo){
- //AX-=(oc-obsXTo);
- obsXTo = oc;
- }
- } else if(oc==c_q){
- // l'ostacolo si trova sulla stessa colonna della regina
- if (or > r_q && or < obsYFrom){
- //AY-=(obsYFrom-or);
- obsYFrom = or;
- } else if (or < r_q && or > obsYTo){
- //AY-=(or-obsYTo);
- obsYTo = or;
- }
- } else if(or==oc+Y_DS_intercept){
- // l'ostacolo si trova sulla stessa diagonale secondaria della regina
- if(oc<c_q && or<r_q){ // lower left
- obsDSXTo = (obsDSXTo < oc? oc: obsDSXTo);
- } else if(oc >c_q && or>r_q){ // upper right
- obsDSXFrom = (obsDSXFrom > oc || obsDSXFrom == -1 ? oc :obsDSXFrom) ;
- }
- } else if(or==-oc+Y_DP_intercept){
- // l'ostacolo si trova sulla stessa diagonale principale della regina
- if(oc<c_q && or>r_q){ // Upper left
- obsDPXTo = (obsDPXTo ==-1 || obsDPXTo < oc? oc: obsDPXTo);
- } else if(oc>c_q && or<r_q){ // lower right
- obsDPXFrom = (obsDPXFrom ==-1 || obsDPXFrom > oc? oc: obsDPXFrom);
- }
- }
- /*
- 88587 9
- 20001 20003
- 20001 20002
- 20001 20004
- 20000 20003
- 20002 20003
- 20000 20004
- 20000 20002
- 20002 20004
- 20002 20002
- 564 323
- ------
- 0
- manca diagonale principale
- */
- }
- int XX=0;
- XX+=obsXFrom-c_q-1;
- XX+=c_q-obsXTo-1;
- AX=XX;
- int YY = 0;
- YY+=obsYFrom-r_q-1;
- YY+= r_q - obsYTo -1;
- AY=YY;
- int DDS=0;
- if (obsDSXFrom !=-1 ) // upper right
- DS-=((Y_DS_intercept<0?n:n-Y_DS_intercept)-obsDSXFrom+1);
- //DDS+= obsDSXFrom - c_q - 1;
- if (obsDSXTo !=-1) // lower left
- DS-=(obsDSXTo+(Y_DS_intercept<0?Y_DS_intercept:0));
- DDS+= c_q - obsDSXTo - 1;
- int DDP = 0;
- if (obsDPXFrom !=-1 ) // lower right
- DP-=(Y_DP_intercept<=n?Y_DP_intercept-obsDPXFrom:n-obsDPXFrom+1);
- if (obsDPXTo !=-1) // upper left
- DP-=(obsDPXTo + (Y_DP_intercept<=n?0:n-Y_DP_intercept+1));
- return DP + DS + AX + AY ;
- }
- int main()
- {
- FILE* fptr = fopen(getenv("OUTPUT_PATH"), "w");
- char** nk = split_string(readline());
- char* n_endptr;
- char* n_str = nk[0];
- int n = strtol(n_str, &n_endptr, 10);
- if (n_endptr == n_str || *n_endptr != '\0') { exit(EXIT_FAILURE); }
- char* k_endptr;
- char* k_str = nk[1];
- int k = strtol(k_str, &k_endptr, 10);
- if (k_endptr == k_str || *k_endptr != '\0') { exit(EXIT_FAILURE); }
- char** r_qC_q = split_string(readline());
- char* r_q_endptr;
- char* r_q_str = r_qC_q[0];
- int r_q = strtol(r_q_str, &r_q_endptr, 10);
- if (r_q_endptr == r_q_str || *r_q_endptr != '\0') { exit(EXIT_FAILURE); }
- char* c_q_endptr;
- char* c_q_str = r_qC_q[1];
- int c_q = strtol(c_q_str, &c_q_endptr, 10);
- if (c_q_endptr == c_q_str || *c_q_endptr != '\0') { exit(EXIT_FAILURE); }
- int** obstacles = malloc(k * sizeof(int*));
- for (int i = 0; i < k; i++) {
- *(obstacles + i) = malloc(2 * (sizeof(int)));
- char** obstacles_item_temp = split_string(readline());
- for (int j = 0; j < 2; j++) {
- char* obstacles_item_endptr;
- char* obstacles_item_str = *(obstacles_item_temp + j);
- int obstacles_item = strtol(obstacles_item_str, &obstacles_item_endptr, 10);
- if (obstacles_item_endptr == obstacles_item_str || *obstacles_item_endptr != '\0') { exit(EXIT_FAILURE); }
- *(*(obstacles + i) + j) = obstacles_item;
- }
- }
- int obstacles_rows = k;
- int obstacles_columns = 2;
- int result = queensAttack(n, k, r_q, c_q, obstacles_rows, obstacles_columns, obstacles);
- fprintf(fptr, "%d\n", result);
- fclose(fptr);
- return 0;
- }
- char* readline() {
- size_t alloc_length = 1024;
- size_t data_length = 0;
- char* data = malloc(alloc_length);
- while (true) {
- char* cursor = data + data_length;
- char* line = fgets(cursor, alloc_length - data_length, stdin);
- if (!line) { break; }
- data_length += strlen(cursor);
- if (data_length < alloc_length - 1 || data[data_length - 1] == '\n') { break; }
- size_t new_length = alloc_length << 1;
- data = realloc(data, new_length);
- if (!data) { break; }
- alloc_length = new_length;
- }
- if (data[data_length - 1] == '\n') {
- data[data_length - 1] = '\0';
- }
- data = realloc(data, data_length);
- return data;
- }
- char** split_string(char* str) {
- char** splits = NULL;
- char* token = strtok(str, " ");
- int spaces = 0;
- while (token) {
- splits = realloc(splits, sizeof(char*) * ++spaces);
- if (!splits) {
- return splits;
- }
- splits[spaces - 1] = token;
- token = strtok(NULL, " ");
- }
- return splits;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement