Parallel search in a two-dimensional matrix using goroutines and channels
Go
Hard
goroutines
Task «Parallel search in a two-dimensional matrix using goroutines and channels»
Given a rectangular matrix of integers. It is necessary to implement a parallel algorithm for counting the occurrences of a given number in the matrix. The algorithm should divide the matrix into N equal parts (by rows), launch N goroutines for parallel processing of each part, collect the results through a channel, and output the total count. If the number is not found, output 0. Use `sync.WaitGroup` to synchronize the goroutines.
Input Format
First line: two integers M and N (1 ≤ M, N ≤ 1000) — the dimensions of the matrix. The next M lines: N integers — the elements of the matrix. Last line: an integer X — the value to search for.
Output Format
One integer — the number of occurrences of X in the matrix.
Examples
Example 1
INPUT
3 3
1 2 3
4 5 6
7 8 9
5
OUTPUT
1
Example 2
INPUT
2 4
1 2 3 4
2 3 4 5
10
OUTPUT
0
Example 3
INPUT
0
2
next
next
reset
next
next
OUTPUT
0
2
0
2
Example 4
INPUT
3
2 3 4
OUTPUT
12
main.go