You will be given a square chess board with one queen and a number of obstacles placed on it. Determine how many squares the queen can attack. A queen is standing on an  chessboard. The chess board's rows are numbered from  to , going from bottom to top. Its columns are numbered from  to , going from left to right. Each square is referenced by a tuple, , describing the row, , and column, , where the square is located. The queen is standing at position . In a single move, she can attack any square in any of the eight directions (left, right, up, down, and the four diagonals). In the diagram below, the green circles denote all the cells the queen can attack from :   There are obstacles on the chessboard, each preventing the queen from attacking any square beyond it on that path. For example, an obstacle at location  in the diagram above prevents the queen from attacking cells , , and :   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 . In the board above, there are  such squares. Function Description Complete the queensAttack function in the editor below. queensAttack has the following parameters: - int n: the number of rows and columns in the board - nt k: the number of obstacles on the board - int r_q: the row number of the queen's position - int c_q: the column number of the queen's position - int obstacles[k][2]: each element is an array of  integers, the row and column of an obstacle Returns - int: the number of squares the queen can attack Input Format The first line contains two space-separated integers  and , the length of the board's sides and the number of obstacles. The next line contains two space-separated integers  and , the queen's row and column position. Each of the next  lines contains two space-separated integers  and , the row and column position of . Constraints     A single cell may contain more than one obstacle. There will never be an obstacle at the position where the queen is located. Subtasks For  of the maximum score:     For  of the maximum score:     Sample Input 0 4 04 4 Sample Output 0 9 Explanation 0 The queen is standing at position  on a  chessboard with no obstacles:   Sample Input 1 5 34 35 54 22 3 Sample Output 1 10 Explanation 1 The queen is standing at position  on a  chessboard with  obstacles:   The number of squares she can attack from that position is . Sample Input 2 1 01 1 Sample Output 2 0 Explanation 2 Since there is only one square, and the queen is on it, the queen can move 0 squares.

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

You will be given a square chess board with one queen and a number of obstacles placed on it. Determine how many squares the queen can attack.

A queen is standing on an  chessboard. The chess board's rows are numbered from  to , going from bottom to top. Its columns are numbered from  to , going from left to right. Each square is referenced by a tuple, , describing the row, , and column, , where the square is located.

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

 

There are obstacles on the chessboard, each preventing the queen from attacking any square beyond it on that path. For example, an obstacle at location  in the diagram above prevents the queen from attacking cells , , and :

 

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 . In the board above, there are  such squares.

Function Description

Complete the queensAttack function in the editor below.

queensAttack has the following parameters:
int n: the number of rows and columns in the board
nt k: the number of obstacles on the board
int r_q: the row number of the queen's position
int c_q: the column number of the queen's position
int obstacles[k][2]: each element is an array of  integers, the row and column of an obstacle

Returns
int: the number of squares the queen can attack

Input Format

The first line contains two space-separated integers  and , the length of the board's sides and the number of obstacles.
The next line contains two space-separated integers  and , the queen's row and column position.
Each of the next  lines contains two space-separated integers  and , the row and column position of .

Constraints

  •  
  •  
  • A single cell may contain more than one obstacle.
  • There will never be an obstacle at the position where the queen is located.

Subtasks

For  of the maximum score:

  •  
  •  

For  of the maximum score:

  •  
  •  

Sample Input 0

4 04 4

Sample Output 0

9

Explanation 0

The queen is standing at position  on a  chessboard with no obstacles:

 

Sample Input 1

5 34 35 54 22 3

Sample Output 1

10

Explanation 1

The queen is standing at position  on a  chessboard with  obstacles:

 

The number of squares she can attack from that position is .

Sample Input 2

1 01 1

Sample Output 2

0

Explanation 2

Since there is only one square, and the queen is on it, the queen can move 0 squares.

Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps

Blurred answer
Knowledge Booster
Time complexity
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education