Home>

I perform a simple task here: https://stepik.org /lesson/13025/step/15?auth=login& Unit= 4377.

It is known that on the 8 × 8 board you can arrange 8 queens so that they do not beat each other. You are given an arrangement of 8 queens on the board, determine if there are a couple of them beat each other.

My code:

#include <
iostream >
#Include <
Vector >
#Include <
stdio.h >
#Include <
stdlib.h >
Using Namespace STD;
INT MAIN () {
  BOOL IN_DANGER= FALSE;
  Vector <
INT >
 x (8), y (8);
    for (int i= 0; i <
 sixteen; I ++) {
        if (I% 2== 0) {
            CIN >
≫
 x [i];
    }
        ELSE {
            CIN >
≫
 y [i];
    }
  }
  for (int i= 0; i <
 eight; I += 2) {
      if ((x [i]== x [i + 1] || y [i + 1]== y [i + 1]) || (ABS (x [i] -y [i])== ABS (x [i + 1] -y [i + 1]))) {
          IN_DANGER= TRUE;
      }
  }
  if (in_danger) {
      COUT <
≪
 "YES";
  } ELSE {
      COUT <
≪
 "NO";
  }
  Return 0;
}

and its execution causes an error:

*** Error in `./main ': munmap_chunk (): Invalid Pointer: 0x0000000002537860 ***
======= Backtrace:=========
[0x4ad787]
[0x4b3012]
[0x4006c6]
[0x496743]
[0x496b02]
[0x400EEA]
======= Memory Map:========
00400000-00583000 R-XP 00000000 Fe: 01 525472 /Sandbox /Main
00782000-0078B000 RW-P 00182000 Fe: 01 525472 /Sandbox /Main
0078B000-00790000 RW-P 00000000 00:00 0
02523000-02562000 RW-P 00000000 00:00 0 [HEAP]
7F1F90573000-7F1F90574000 RW-P 00000000 00:00 0
7FFD2C44A000-7FFD2C46B000 RW-P 00000000 00:00 0 [Stack]
7FFD2C548000-7FFD2C54B000 R -P 00000000 00:00 0 [VVAR]
7FFD2C54B000-7FFD2C54D000 R-XP 00000000 00:00 0 [VDSO]
FFFFFFFFFF600000-FFFFFFFFFFF601000 R-XP 00000000 00:00 0 [VSYSCALL]
ABORTED (Core Dumped)

I tried to check what was recorded in arrays:

for (int i= 0; i <
 sixteen; I ++) {
    COUT <
≪
 x [i] <
≪
 "" ≪
≪
 y [i];
}

and received the following: 1 00 72 00 43 00 24 00 8, although he himself introduced: 1 7 2 4 3 2 4 8

Why so?

Possible duplicate question: it is necessary to determine whether a couple of queens attack each other if the coordinates of 8 queens

aleksandr barakin2021-06-14 20:52:31