c++ – Hackerrank’s Queen’s Attack II

https://www.hackerrank.com/challenges/queens-attack-2/problem

A queen is standing on an nxn chessboard. The chessboard’s rows are numbered from 1 to n, going from bottom to top; its columns are numbered from 1 to n, going from left to right. Each square on the board is denoted by a tuple, (r,c), describing the row, r, and column, c, where the square is located.

The queen is standing at position (rq,cq) and, in a single move, she can attack any square in any of the eight directions (left, right, up, down, or the four diagonals). In the diagram below, the green circles denote all the cells the queen can attack from (4,4):

enter image description here

There are $k$ obstacles on the chessboard preventing the queen from attacking any square that has an obstacle blocking the the queen’s path to it. For example, an obstacle at location $(3,5)$ in the diagram above would prevent the queen from attacking cells $(3,5)$, $(2,6)$, and $(1,7)$:

enter image description here

Given the queen’s position and the locations of all the obstacles, find and print the number of squares the queen can attack from her position at $(r_q,c_q)$.

Input Format

The first line contains two space-separated integers describing the respective values of $n$ (the side length of the board) and $k$ (the number of obstacles).

The next line contains two space-separated integers describing the respective values of $r_q$ and $c_q$, denoting the position of the queen.

Each line $i$ of the $k$ subsequent lines contains two space-separated integers describing the respective values $r_i$ of $c_i$ and , denoting the position of obstacle $i$.

Constraints

$ 0 leq n leq 100000$

$ 0 leq k leq 100000$

A single cell may contain more than one obstacle; however, it is guaranteed that there will never be an obstacle at position $(r_q,c_q)$ where the queen is located.

Output Format

Print the number of squares that the queen can attack from position .

Sample Input 0

$4$ $0$

$4$ $4$

Sample Output 0

$9$

Explanation 0

The queen is standing at position $(4,4)$ on a $4$x$4$ chessboard with no obstacles:

enter image description here

We then print the number of squares she can attack from that position, which is $9$.

My approach:

Instead of iterating through every single point in the queens path as that will be resource intensive when n is very high, I went with separating the paths into 8 different directions (up left, up, up right, right, etc).

int u, d, l, r, ul, ur, dl, dr;
u = d = l = r = ul = ur = dl = dr = 0;
bool modified(8) = { false };

Than I checked if there is an obstacle in the path by checking if the queens x = obstacles x or queens y = obstacles y and if its on the vertical/horizontal path of the queens I would find the distance by calculating the delta – 1 and to find the diagonal points I know since the points either have to have a 1 or -1 slope to be in the queens path so I checked if |queen’s y – obstacle’s y| = |queen’s x – obstacle’s x| and if it is true than I find the delta between either the x or y as either work and if there is no obstacles I would just use the edge to find the distance. I only checks to see if obstacle was in path then calculate the possible points than mark the direction as solved so if it is unmarked than it means there is no obstacles in the path so I find the distance from edge using:

if (!modified(0)) u = n - qy;
if (!modified(1)) d = qy - 1;
if (!modified(2)) l = qx - 1;
if (!modified(3)) r = n - qx;
if (!modified(4) && qy != n && qx != 1) ul = (qx - 1 < n - qy) ? qx - 1 : n - qy;
if (!modified(5) && qy != n && qx != n) ur = (n - qx < n - qy) ? n - qx : n - qy;
if (!modified(6) && qy != 1 && qx != 1) dl = (qx - 1 < qy - 1) ? qx - 1 : qy - 1;
if (!modified(7) && qy != 1 && qx != n) dr = (n - qx < qy - 1) ? n - qx : qy - 1;

Sorry for messy style, this is my first time on stackoverflow/stackexchange.

Full code:

int queensAttack(const int &n, const int &k, const int & qy, const int & qx, const vector<vector<int>> &obstacles) {
int u, d, l, r, ul, ur, dl, dr;
u = d = l = r = ul = ur = dl = dr = 0;
bool modified(8) = { false };

for (int i = 0; i < obstacles.size(); i++) {
    int temp{};
    if (obstacles(i)(1) == qx) {
        if (obstacles(i)(0) > qy) {
            temp = obstacles(i)(0) - qy - 1;
            if (modified(0) && u > temp) {
                u = temp;
            }
            else if (!modified(0)) u = temp;
            modified(0) = true;
        }
        else {
            temp = qy - obstacles(i)(0) - 1;
            if (modified(1) && d > temp) {
                d = temp;
            }
            else if (!modified(1)) d = temp;
            modified(1) = true;
        }
    }
    if (obstacles(i)(0) == qy) {
        if (obstacles(i)(1) < qx) {
            temp = qx - obstacles(i)(1) - 1;
            if (modified(2) && l > temp) {
                l = temp;
            }
            else if (!modified(2)) l = temp;
            modified(2) = true;
        }
        else {
            temp = obstacles(i)(1) - qx - 1;
            if (modified(3) && r > temp) {
                r = temp;
            }
            else if (!modified(3)) r = temp;
            modified(3) = true;
        }
    }
    if (abs(qy - obstacles(i)(0)) == abs(qx - obstacles(i)(1))) {
        if (obstacles(i)(0) > qy && obstacles(i)(1) < qx) {
            temp = qx - obstacles(i)(1) - 1;
            if (modified(4) && ul > temp) {
                ul = temp;
            }
            else if (!modified(4)) ul = temp;
            modified(4) = true;
        }
        if (obstacles(i)(0) > qy && obstacles(i)(1) > qx) {
            temp = obstacles(i)(1) - qx - 1;
            if (modified(5) && ur > temp) {
                ur = temp;
            }
            else if (!modified(5)) ur = temp;
            modified(5) = true;
        }
        if (obstacles(i)(0) < qy && obstacles(i)(1) < qx) {
            temp = qx - obstacles(i)(1) - 1;
            if (modified(6) && dl > temp) {
                dl = temp;
            }
            else if (!modified(6)) dl = temp;
            modified(6) = true;
        }
        if (obstacles(i)(0) < qy && obstacles(i)(1) > qx) {
            temp = obstacles(i)(1) - qx - 1;
            if (modified(7) && dr > temp) {
                dr = temp;
            }
            else if (!modified(7)) dr = temp;
            modified(7) = true;
        }
    }
}
if (!modified(0)) u = n - qy;
if (!modified(1)) d = qy - 1;
if (!modified(2)) l = qx - 1;
if (!modified(3)) r = n - qx;
if (!modified(4) && qy != n && qx != 1) ul = (qx - 1 < n - qy) ? qx - 1 : n - qy;
if (!modified(5) && qy != n && qx != n) ur = (n - qx < n - qy) ? n - qx : n - qy;
if (!modified(6) && qy != 1 && qx != 1) dl = (qx - 1 < qy - 1) ? qx - 1 : qy - 1;
if (!modified(7) && qy != 1 && qx != n) dr = (n - qx < qy - 1) ? n - qx : qy - 1;

return u + d + l + r + ul + ur + dl + dr;

}

Forgot to mention I loop through the obstacles and only replaces the current distance if the new one is smaller as the obstacles in the array aren’t in order from closest to farthest.

dnd 5e – Does Blink grant advantage on the first attack roll after you return?

No. It doesn’t grant advantage on your attack, because you reappear at the start of your turn.

The description of the blink spell says:

Roll a d20 at the end of each of your turns for the duration of the
spell. On a roll of 11 or higher, you vanish from your current plane
of existence and appear in the Ethereal Plane (…) At the start of
your next turn
, and when the spell ends if you are on the Ethereal
Plane, you return to an unoccupied space of your choice that you can
see within 10 feet of the space you vanished from.

As you can see, the spell description states that if you do vanish from your current plane and appear in the Ethereal Plane, you return at the start of your next turn. (The spell’s duration is “1 minute” and does not require concentration, so if you’re still in combat when the spell ends, it still ends at the start of your turn, 10 rounds after you initially cast the spell.)

The rules on unseen attackers and targets state, in part (emphasis mine):

When a creature can’t see you, you have advantage on attack rolls
against it.
If you are hidden — both unseen and unheard — when you
make an attack, you give away your location when the attack hits or
misses.

However, if you have the effect of blink active on you and you vanish into the Ethereal Plane at the end of one turn, you reappear in the Material Plane at the start of your next turn – which happens before you have even started to make any attacks at all. As such, you clearly don’t have advantage simply by virtue of the blink spell.

The benefit of blink is not offensive, but defensive (in the sense that it allows you to potentially avoid danger). As the second paragraph of the spell description states:

While on the Ethereal Plane, you can see and hear the plane you
originated from, which is cast in shades of gray, and you can’t see
anything there more than 60 feet away. You can only affect and be
affected by other creatures on the Ethereal Plane. Creatures that
aren’t there can’t perceive you or interact with you, unless they have
the ability to do so.

The blink spell makes it so that, if you roll an 11 on higher on the d20 at the end of your turn, you simply can’t be affected by most harmful attacks, spells, or other abilities. (Of course, your allies are still in harm’s way – and there’s also only a 50% that this happens, so you can’t rely solely on blink to keep you out of trouble.) However, blink doesn’t inherently grant any benefit to your attacks, because you reappear before those attacks happen – not as you’re attacking, and not as a result of the attack.

rop – Is it possible to run with orp attack legitim function that shouldn’t run even if stack is not executable?

I read about the hardware protection that blocks the CPU from jumping to stack address. But hacker may still edit the return address to an address in code memory that shouldn’t run at that moment.

For example;

    #include<stdio.h>

    void ath_secuss()
    {
        printf("You have successfully logged inn");
    }
    int main()
    {
         char password(10);
         gets(password);
         if(password=="password")
         {
             ath_secuss();
         }
    }

Can the hacker edit the return address to ath_sucess()? Also, does the return address allays in the end so we can check the data that coming in is not bigger then the ebx-1 register?

sql injection – Can attack to a website from a parked subdomain with different hosts?

I have a website like maindomain.com that hosted on a different hosting like 192.168.1.1 i want to add a subdomain from a different host like 192.168.1.2 to my main domain which my address will be like subdomain.mainwebhost.com

If on Subdomain host i have a script with a security bug , Can attackers setup any kind of attacks against my data that hosted on my main host ?

privacy – What mitigations are there against a timing attack done to find which devices are communicating on a set of devices?

I know the title isn’t good at all but allow me to explain. In this model I have n devices on different networks that are able to communicate with each other. A supervisor is able to see every packet a device sends or receives. How can two devices A and B communicate in such a way that the supervisor knows A and B are talking to someone but doesn’t know they are talking to each other?

I have thought about the mitigation where the communicating devices also send packets to another random device and receives a response. So A sends packets to both B and C; B sends packets to both A and D. Would this work? What are the other methods of mitigation?

Attack against OTP Cipher

What would be an attack against an insecure instance of the OTP cipher given two challenge ciphertexts using the same key in order to get the plaintext? I’ve tried to implement some approaches with Python but it did not work.(I’m a beginner in cyber security)

dnd 5e – Does the reaction melee attack from the Sentinel feat’s third benefit trigger through an Echo Knight fighter’s echo?

Not unless the attacking enemy is within 5 feet of you.

The third benefit of the Sentinel feat reads (PHB, p. 169-170):

When a creature within 5 feet of you makes an attack against a
target other than you (and that target doesn’t have this feat), you
can use your reaction to make a melee weapon attack
against the
attacking creature.

The description of the Echo Knight fighter’s Manifest Echo feature says (EGtW, p. 183):

You can use the echo in the following ways:

  • As a bonus action, you can teleport, magically swapping places with
    your echo at a cost of 15 feet of your movement, regardless of the
    distance between the two of you.
  • When you take the Attack action on your turn, any attack you make
    with that action can originate from your space or the echo’s space.
    You make this choice for each attack.
  • When a creature that you can see within 5 feet of your echo moves at
    least 5 feet away from it, you can use your reaction to make an
    opportunity attack against that creature as if you were in the echo’s
    space.

The second bullet point of the Manifest Echo feature lets the Echo Knight attack from their echo’s space when they take the Attack action. The third benefit lets them make an opportunity attack from the echo’s space when a creature moves away from it.

However, neither Manifest Echo nor any other Echo Knight feature lets you make attacks from the echo’s space in any other situation, as written. No feature lets your echo count as “you” for the purposes of other reactions that are based on a trigger occurring within a certain distance of you – so you can only take such a reaction if that trigger occurs within that distance of you (not just your echo).

As a result, the reaction from the third benefit of the Sentinel feat won’t be triggered by an enemy within 5 feet of your echo who’s attacking one of your allies – unless the attacking enemy is within 5 feet of you as well.

dnd 5e – If your target has an enemy within 5ft, can you sneak attack without using a finesse or ranged weapon?

A rogue and fighter are engaged in melee combat with a goblin. The rogue is unarmed. Can the rogue activate Sneak Attack?

The rules for Sneak Attack state:

Beginning at 1st level, you know how to strike subtly and exploit a foe’s distraction. Once per turn, you can deal an extra 1d6 damage to one creature you hit with an attack if you have advantage on the attack roll. The attack must use a finesse or a ranged weapon.

You don’t need advantage on the attack roll if another enemy of the target is within 5 feet of it, that enemy isn’t incapacitated, and you don’t have disadvantage on the attack roll.

The question arises from the difficulty in parsing the bolded sentence. I have heard 2 interpretations:

  1. You can sneak attack if (you have advantage and the attack is made using a finesse or ranged weapon) or (an enemy of the target is within 5ft, etc).
  2. You can sneak attack if ((you have advantage) or (an enemy of the target is within 5ft, etc)) and the attack is made using a finesse or ranged weapon.

The first interpretation hinges on the idea that when the second paragraph says “on the attack roll” it is still talking about the same “attack” as in the first paragraph. The second interpretation hinges on the idea that the first interpretation is bizarre and unnatural – if that was the intent, there are many ways that it could have been worded to be clearer.

Thematically, I am leaning towards the second – not having a finesse or ranged weapon shouldn’t stop the rogue from exploiting a distracted foe.

Considering RAW only (no twitter please), how should this feature be interpreted?

dnd 3.5e – Does Ocular spell make every eligible damage spells have a critical chance since it becomes a ranged touch attack (ray)?

Ocular spell states:

(…)When you release an ocular spell, its effect changes to a ray with a
range of up to 60 feet. If the spell previously would have affected
multiple creatures, it now affects only the creature struck by the
ray. You must succeed on a ranged touch attack to strike your target
with an ocular spell, and the target is still permitted any saving
throw allowed by the spell.(…)

Every spell that has a touch attack (melee/range) Do have a critical hit chance (20X2)

So If I was to release an Ocular Fireball (only affects 1 creature now and it still gets the Reflex Half saving throw though). It could indeed be a critical hit.

Correct?

Sidenote:

I know fireball might not be the best spell for this combination, it was just for the sake of the question.

denial of service – Will brute force attack exhaust web server, resulting in DOS

I used to own a shared hosting business and, while being at a party on a weekend night, I received an automated notification caused by a resource exhaustion. I immediately left to the office and when I arrived I found out a bruteforce attack against a client’s WordPress admin panel was the cause of it.

Always make sure your firewall rulesets are up to date and that a service can’t take others’ resources in case of an anomalous event.