

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Count of cells in a matrix which give a Fibonacci number when the count of adjacent cells is added in C++
<p>Given a matrix [ ][ ] having dimensions as row x col. The goal is to find the count of cells of matrix that meet the given condition:</p><p>Value of cell matrix [i][j] + no. of adjacent cells to it = a Fibonacci number</p><p>Numbers in Fibonacci series:- 0, 1, 1, 2, 3, 5, 8, 13, 21, 43 …..</p><h2>Let us understand with examples.</h2><p><strong>For Example</strong></p><p><strong>Input -</strong> matrix[row][col] = {{1, 4, 1}, {2, 0, 1}, {5, 1, 1}</p><p><strong>Output - </strong>Count of cells in a matrix which give a Fibonacci number when the count of adjacent cells is added are: 4</p><p><strong>Explanation</strong></p><p> 0 1 2 </p><p>0 1 4 1 </p><p>1 2 0 1 </p><p>2 5 1 1 </p><p>Cell(0,0) → 1+2=3 ( 2 adjacent cells (1,0) and (0,1) )</p><p>Cell(0,2) → 1+2=3</p><p>Cell(1,0) → 2+3=5</p><p>Cell(2,2) → 1+2=3</p><p><strong>Input -</strong> matrix[row][col] = {{0,0,0}, {0, 1, 0}, {0, 0, 0} }</p><p><strong>Output -</strong>Count of cells in a matrix which give a Fibonacci number when the count of adjacent cells is added are: 9</p><p><strong>Explanation</strong></p><p> 0 1 2 </p><p>0 0 0 0 </p><p>1 0 1 0 </p><p>2 0 0 0 </p><p>Cell(0,0) → 0+2=2 ( 2 adjacent cells (1,0) and (0,1) ) Similarly cells (0,2), (2,2) and (2,0)</p><p>Cell(0,1) → 0+3=3 ( 3 adjacent cells (0,1) and (0,2) and (1,1) ) Similarly cells (1,0), (1,2) and (2,1)</p><p>Cell (1,1) → 1+4=5 </p><p>All 9 cells are counted.</p><h2>Approach used in the below program is as follows</h2><p>In the matrix of any kind there will be three types of cells only. The corner cells will have 2 adjacent cells, the cells with 3 adjacent cells and the cells with 4 adjacent cells only. Add 2, 3 or 4 to the value of these cells and check if the sum is a fibonacci number using the function check_fibonacci(int num).</p><ul class="list"><li>Take a matrix[][] and initialize it. </li><li>Function check_square(long double num) takes a number and returns true if it is a perfect square.</li><li>Function check_fibonacci(int num) returns true if num is a Fibonacci number.</li><li>If check_square(5 * num * num + 4) || check_square(5 * num * num - 4) returns true then num is a Fibonacci number.</li><li>Function Fibonacci_cells(int matrix[row][col]) returns the count of cells in a matrix which gives a Fibonacci number when the count of adjacent cells is added.</li><li>Take the initial count as 0.</li><li>Traverse using for loops from i=0 to i<row and j=0 to j<col. Take total = matrix[i][j].</li><li>Add 2 ,3 or 4 to total based on the cells adjacent to it.</li><li>If the new total is a fibonacci number then check_fibonacci(total) wil return true, so increment count.</li><li>At the end of all for loops return count as result.</li></ul><h2>Example</h2><p><a class="demo" href="http://tpcg.io/fW0u2qq9" rel="nofollow" target="_blank">Live Demo</a></p><pre class="prettyprint notranslate">#include <bits/stdc++.h> using namespace std; #define row 3 #define col 3 bool check_square(long double num) { long double val = sqrt(num); return ((val - floor(val)) == 0); } bool check_fibonacci(int num) { return check_square(5 * num * num + 4) || check_square(5 * num * num - 4); } int Fibonacci_cells(int matrix[row][col]) { int count = 0; for (int i = 0; i < row; i++) { for (int j = 0; j < col; j++) { int total = matrix[i][j]; if ((i == 0 && j == 0) || (i == row - 1 && j == 0) || (i == 0 && j == col - 1) || (i == row - 1 && j == col - 1)) { total = total + 2; } else if (i == 0 || j == 0 || i == row - 1 || j == col - 1) { total = total + 3; } else { total = total + 4; } if (check_fibonacci(total)) { count++; } } } return count; } int main() { int matrix[row][col] = {{1, 4,1},{2,0,1},{5,1,1}}; cout << "Count of cells in a matrix which give a Fibonacci number when the count of adjacent cells is added are: " << Fibonacci_cells(matrix); return 0; }</pre><p>If we run the above code it will generate the following output −</p><h2>Output</h2><pre class="result notranslate">Count of cells in a matrix which give a Fibonacci number when the count of adjacent cells is added are: </pre>
- Related Questions & Answers
- Find safe cells in a matrix in C++
- Program to count number of operations required to all cells into same color in Python
- Cells with Odd Values in a Matrix in C++
- C++ Program to find out the number of illuminated cells in a grid
- C++ Program to find out the number of border cells in a grid
- Program to count number of walls required to partition top-left and bottom-right cells in Python
- Count the number of ways to traverse a Matrix in C++
- Count the nodes whose sum with X is a Fibonacci number in C++
- Print cells with same rectangular sums in a matrix in C++
- Find a number which give minimum sum when XOR with every number of array of integer in Python
- Find a number which give minimum sum when XOR with every number of array of integer in C++
- Count the number of intervals in which a given value lies in C++
- C++ program to find whether there is a path between two cells in matrix
- Usage of CSS empty-cells property
- Program to count number of perfect squares are added up to form a number in C++
Advertisements