Showing posts with label Matrix. Show all posts
Showing posts with label Matrix. Show all posts

acosl() in C++

acosl(): This function is in the library math.h. This function takes one parameter.

It takes an argument (parameter) whose inverse cosine value to be fined. It takes an argument

in the range [-1,1]. This function returns the inverse cosine value in the range [0, π].

If the value is not in the range [-1,1] then it returns nan.

Returned Value for different range parameters.

1. x<-1: It returns nan.

2. x=[-1,1]: It returns the value in the range [0, π].

3. x>1: It returns nan.

Parameters:

__X: The value whose inverse cosine value is to be fined.

Syntax:

acosl(__X)

For Example:

1. __X = 0.5

acosl(__X) = > It returns 1.0472

2. __X = 1.5

acosl(__X) => It returns nan

Approach

C++

#include <bits/stdc++.h>
using namespace std;

int main()
{
    long double x = 0.5;

    cout << acosl(x<< "\n";

    return 0;
}

Number of ways to move from top left corner to bottom right corner

You are given an N by M matrix of 0s and 1s. Starting from the top left corner, how many ways are there to reach the bottom right corner?

You can only move right and down. 0 represents an empty space while 1 represents a wall you cannot walk through.

For example, given the following matrix:

[[0, 0, 1],
 [0, 0, 1],
 [1, 0, 0]]

Return two, as there are only two ways to get to the bottom right:

  • Right, down, down, right
  • Down, right, down, right

The top left corner and bottom right corner will always be 0

Example:

Input:  matrix = {{0, 0, 1}, {0, 0, 1}, {1, 0, 0}}
Output: 2

Approach

C++

#include <bits/stdc++.h>
using namespace std;

int numberOfWays(vector<vector<int>> &matrix)
{
    int n = matrix.size();
    if (n == 0)
        return 0;
    int m = matrix[0].size();
    int dp[n][m];
    for (int i = 0i < ni++)
    {
        if (matrix[i][0] == 1)
        {
            while (i < n)
            {
                dp[i][0] = 0;
                i++;
            }
        }
        else
            dp[i][0] = 1;
    }
    for (int i = 0i < mi++)
    {
        if (matrix[0][i] == 1)
        {
            while (i < m)
            {
                dp[0][i] = 0;
                i++;
            }
        }
        else
            dp[0][i] = 1;
    }
    for (int i = 1i < ni++)
    {
        for (int j = 1j < mj++)
        {
            if (matrix[i][j] == 1)
                dp[i][j] = 0;
            else
                dp[i][j] = dp[i - 1][j] + dp[i][j - 1];
        }
    }
    return dp[n - 1][m - 1];
}
int main()
{

    vector<vector<int>> matrix = {{001},
                                  {001},
                                  {100}};

    cout << numberOfWays(matrix<< "\n";

    return 0;
}


Determine Whether Matrix Can Be Obtained By Rotation

Given two n x n binary matrices mat and target, return true if it is possible to make mat equal to target by rotating mat in 90-degree increments, or false otherwise.

Example :

Input: mat = [[0,1],[1,0]], target = [[1,0],[0,1]]
Output: true
Explanation: We can rotate mat 90 degrees clockwise to make mat equal target.

Approach

C++

#include <bits/stdc++.h>
using namespace std;

void rotate(vector<vector<int>> &matrix)
{
    for (int i = 0i < matrix.size(); i++)
    {
        for (int j = i + 1j < matrix.size(); j++)
        {
            swap(matrix[i][j]matrix[j][i]);
        }
    }
    for (int i = 0i < matrix.size(); i++)
    {
        for (int j = 0j < matrix.size() / 2j++)
        {
            swap(matrix[i][j]matrix[i][matrix.size() - j - 1]);
        }
    }
}
bool findRotation(vector<vector<int>> &mat,
                  vector<vector<int>> &target)
{

    for (int i = 0i < 4i++)
    {
        rotate(mat);

        if (mat == target)
            return true;
    }

    return false;
}

int main()
{
    vector<vector<int>> mat = {{01}, {10}};
    vector<vector<int>> target = {{10}, {01}};

    if (findRotation(mattarget))
        cout << "true\n";
    else
        cout << "false\n";

    return 0;
}


Special Matrix

You are given a square matrix of size n (it will be an odd integer). Rows are indexed 0 to n-1 from top to bottom and columns are indexed 0 to n-1 from left to right. The matrix consists of only '*' and '.'. '*' appears only once in the matrix while all other positions are occupied by '.'

Your task is to convert this matrix to a special matrix by following any of two operations any number of times.

  1. you can swap any two adjacent rows, i and i+1 (0<= i < n-1)

  2. you can swap any two adjacent columns, j and j+1 (0<= j < n-1)

Special Matrix is one that contains '*' at the middle of the matrix. e.g following is a size 7 special matrix

    .......
    .......
    .......
    ...*...
    .......
    .......
    .......

Output no of steps to convert given matrix to special matrix.

Example:

Input: n = 7, arr = {{".......", ".*.....", ".......", ".......", ".......", ".......", "......."}}
Output: 4

Approach

C++

#include <bits/stdc++.h>
using namespace std;

int specialMatrix(int nvector<stringarr)
{
    int rowcolumnmiddle;
    row = column = middle = 0;

    for (int i = 0i < ni++)
    {
        for (int j = 0j < nj++)
        {
            if (arr[i][j] == '*')
            {
                row = i;
                column = j;
            }
        }
    }
    middle = n / 2;
    row -= middle;
    column -= middle;
    if (row < 0)
    {
        row = -row;
    }
    if (column < 0)
    {
        column = -column;
    }
    return row + column;
}

int main()
{

    int n = 7;
    vector<stringarr = {{".......",
                           ".*.....",
                           ".......",
                           ".......",
                           ".......",
                           ".......",
                           "......."}};

    cout << specialMatrix(narr<< "\n";

    return 0;
}


Prison Break

Alfie was a prisoner in mythland. Though Alfie was a witty and intelligent guy.He was confident of escaping prison.After few days of observation,He figured out that the prison consists of (N×N) cells.i.e The shape of prison was (N×N) matrix. Few of the cells of the prison contained motion detectors.So Alfie planned that while escaping the prison he will avoid those cells containing motion detectors.Yet before executing his plan,Alfie wants to know the total number of unique possible paths which he can take to escape the prison.Initially Alfie is in cell
(1,1) while the exit of the cell (N,N).

note:->Alfie can move in all four direction{ if his current location is (X,Y), he can move to either
(X+1,Y)(X1,Y)(X,Y+1)(X,Y1) }. If the first cell (1,1) and the last cell(N,N) contain motion detectors,then Alfie can't break out of the prison.

Example:

Input:  n = 4, arr = {{0, 1, 1, 0}, {0, 0, 1, 0}, {0, 0, 0, 0}, {0, 1, 1, 0}}
Output: 2

Approach

C++

#include <bits/stdc++.h>
using namespace std;

const int N = 22;

int a[N][N];

bool vis[N][N];

//all four directions
int dx[] = {-1100};
int dy[] = {00, -11};
int ansn;

void dfs(int iint j)
{

    //mark as visited
    vis[i][j] = 1;

    //if we reach to the destination
    if (i == n && j == n)
        ans++;

    for (int z = 0z < 4z++)
    {
        int x = i + dx[z], y = j + dy[z];

        //check for valid cell
        if (x >= 1 && x <= n and y >= 1 && y <= n)
        {

            //if not visited and value is 0
            //then call for dfs
            if (!a[x][y] and !vis[x][y])
                dfs(xy);
        }
    }

    //mark as unvisited (backtrack)
    vis[i][j] = 0;
}

int main()
{

    ans = 0;

    n = 4;
    vector<vector<int>> arr = {{0110},
                               {0010},
                               {0000},
                               {0110}};

    for (int i = 1i <= ni++)
    {
        for (int j = 1j <= nj++)
        {
            a[i][j] = arr[i - 1][j - 1];
        }
    }
    dfs(11);

    cout << ans << "\n";

    return 0;
}