Norway


#include <iostream>

#include <algorithm>

#include <climits>

using namespace std;

 

// M x N

#define M 4

#define N 4

 

// Recursive function to fix the tableau property in a MxN Young tableau

void fixTableau(int tableau[][N], int i, int j)

{

    // get the values present at the bottom and right cell of the current cell

    int bottom = (i + 1 < M) ? tableau[i + 1][j] : INT_MAX;

    int right = (j + 1 < N) ? tableau[i][j + 1] : INT_MAX;

 

    if (bottom < right)    // go down

    {

        swap(tableau[i][j], tableau[i + 1][j]);

        fixTableau(tableau, i + 1, j);

    }

 

    if (bottom > right)    // go right

    {

        swap(tableau[i][j], tableau[i][j + 1]);

        fixTableau(tableau, i, j + 1);

    }

}

 

// Recursive function to a new element into a non-full MxN Young tableau

void insert(int tableau[][N], int i, int j)

{

    // base case

    if (i == 0 && j == 0) {

        return;

    }

 

    // handle separately for the first row

    if (i == 0)

    {

        if (tableau[i][j] < tableau[i][j1]) {

            swap(tableau[i][j], tableau[i][j1]);

            insert(tableau, i, j 1);

        }

        return;

    }

 

    // handle separately for the first column

    if (j == 0)

    {

        if (tableau[i][j] < tableau[i1][j]) {

            swap(tableau[i][j], tableau[i1][j]);

            insert(tableau, i 1, j);

        }

        return;

    }

 

    if (tableau[i][j] < tableau[i1][j])    // go up

    {

        swap(tableau[i][j], tableau[i1][j]);

        insert(tableau, i 1, j);

    }

 

    if (tableau[i][j] < tableau[i][j1])    // go left

    {

        swap(tableau[i][j], tableau[i][j1]);

        insert(tableau, i, j 1);

    }

}

 

// Function to a given element in the Young tableau

void replace(int tableau[][N], int i, int j, int key)

{

    // element at cell (i, j)

    tableau[i][j] = INT_MAX;

 

    // fix the young tableau property

    fixTableau(tableau, i, j);

 

    // place the given key at the bottom right corner of the tableau

    tableau[M1][N1] = key;

 

    // move the key to its correct position in the tableau

    insert(tableau, M1, N1);

}

 

// A utility function to an element in a Young tableau.

bool search(int tableau[][N], int key, int value)

{

    // start from the top-rightmost cell of tableau i.e. (0, N-1) cell

    int i = 0, j = N 1;

 

    // run till tableau boundary is reached

    while (i < M && j >= 0)

    {

        // if current element is less than the key, increment the row index

        if (tableau[i][j] < key)

            i++;

 

        // if current element is more than the key, decrement the column index

        else if (tableau[i][j] > key)

            j;

 

        // current element is equal to the key

        else {

            replace(tableau, i, j, value);

            return true;

        }

    }

 

    return false;

}

 

// Utility function to print a Young tableau

void printTableau(int tableau[][N])

{

    for (int i = 0; i < M; i++) {

        for (int j = 0; j < N; j++) {

            cout << tableau[i][j] << ‘ ‘;

        }

        cout << endl;

    }

}

 

// main function

int main()

{

    // M x N Young tableau

    int tableau[M][N] =

    {

        { 10, 12, 15, 16 },

        { 11, 18, 20, 25 },

        { 22, 2, 30, 35 },

        { 34, 40, 44, 45 }

    };

 

    search(tableau, 20, );    // replace 20 by 14

    printTableau(tableau);

 

    return 0;

}



Source link

LEAVE A REPLY

Please enter your comment!
Please enter your name here