Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

PROGRAM IN JAVA PLEASE Honey Honey Honey!!! The Queen Bee has asked the Worker Bees to write a program to answer queries regarding her T

PROGRAM IN JAVA PLEASE

Honey Honey Honey!!!

The Queen Bee has asked the Worker Bees to write a program to answer queries regarding her T hives. A hive can be visualized using a rectangular matrix of size N X M. Figure below shows the coordinates of each cell. Each cell contains honey which is depicted by a number in the corresponding 2D array ( <= 100).

The Queen Bee asks Q queries which need to be answered by the worker bees, they are of the following types:

1 X Y -- To sum the honey over all cells which are at a 1-hop distance from cell (X, Y) i.e. it takes only 1 hop in any direction from cell (X, Y) to reach these

2 X Y -- To sum the honey over all cells which are at a 2-hop distance from cell (X, Y) i.e. it takes only 2 hops in any direction from cell (X, Y) to reach these

A hop is defined as moving from a cell (X, Y) to a cell it shares a common edge with i.e. its immediate neighbor.

Input Format:

Take input from a file named in.txt. The first line is T, the number of test cases, followed by space separated N, M, the size of the matrix/hive. The next N lines contain M space separated integers each consisting the honey amount in each cell. This is followed by Q, the number of queries. The next Q lines are of the type 1 X Y and 2 X Y where (X, Y) is the cell location.

Output Format:

Output every query in a new line, which is the total amount of honey.

Constraints on input:

1 <= T <= 10

1 <= N, M,

Q <= 200

0 <= X < N and 0 <= Y < M

Sample Input:

1

4 4

1 2 3 4

5 6 7 8

9 1 3 4

1 2 3 4

2

1 0 1

2 0 1

Sample Output:

22

25

Explanation

For the first query, the cells at a 1-hop distance from (0, 1) are (0, 0), (1, 0), (1, 1), (1, 2) and (0, 2). For the second query, the cells at a 2-hop distance from (0, 1) are (2, 0), (2, 1), (2, 2), (1, 3) and (0, 3)

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Data Analysis Using SQL And Excel

Authors: Gordon S Linoff

2nd Edition

111902143X, 9781119021438

More Books

Students also viewed these Databases questions

Question

Describe who reads a business plan and what they are looking for.

Answered: 1 week ago

Question

Describe Table Structures in RDMSs.

Answered: 1 week ago