Create a matrix with all elements having the value of the shortest path to a given position You're given the number of rows and columns and the indices for the reference point. Reference point is = 0.
i.e:
3 4 3 3
output:
4 3 2 3
3 2 1 2
2 1 0 1

I managed to solve the problem using the easiest solution there is. I calculated the elements from the same column, and then using 2 for loops I used for every single element the already calculated value from the respective row as a new reference point.
I was stuck on this at first because I tried to come up with a formula so I asked on stackoverflow, and told me to submit a code, any solution.. so I came up with this:

#include &lt;iostream&gt;
using namespace std;
int main()
{
    int v[501][501], i, j, m, n, o, p;
    cin &gt;&gt; m &gt;&gt; n &gt;&gt; o &gt;&gt; p;
    for (i = o; i &gt;= 1; i--)
            v[i][p] = o - i;
    for(i = o;i &lt;= m; i++)
        v[i][p] = i - o;
    for(i = 1; i &lt;= m; i++)
        for(j = 1; j &lt;= n; j++){
            if(j &lt; p)
                v[i][j] = -j + p + v[i][p];
            else if(j &gt; p)
                v[i][j] = j - p + v[i][p];            
        }
    for(i = 1 ; i &lt;= m; i++){
        for(j = 1; j &lt;= n; j++)
            cout &lt;&lt; v[i][j] &lt;&lt; " ";
        cout &lt;&lt; '\n';
    }
}


How can I improve it?
Some suggestions:


Use variable names that tells the reader what they are.
Dont do using namespace std; in the global namespace.
Always check that &lt;stream&gt; &gt;&gt; variable actually worked or else your program will run with uninitialized variables and cause undefined behaviour if they are read.
Use an unsigned type when dealing with subscripting.
If you use a hardcoded array size, check that the values entered by the user actually fits in the array.
Don't use a hardcoded array size when the required size is unknown at compile time.
Use 0-based arrays instead of 1-based.


When it comes to the actual algorithm, it seems like that the shortest path from any point is
 abs(point.row - reference.row) + abs(point.column - reference.column)
which would give code like this:

#include &lt;cstdlib&gt;
#include &lt;iostream&gt;
#include &lt;limits&gt;
#include &lt;vector&gt;

// a helper type to keep the reference point
class point_t {
public:
    point_t() : point_t(0, 0) {}
    point_t(unsigned Row, unsigned Col) : row(Row), col(Col) {}

    // a function to calculate the distance to another point
    unsigned distance_to(const point_t&amp; p) const {
        return static_cast&lt;unsigned&gt;(
            std::abs(static_cast&lt;int&gt;(row) - static_cast&lt;int&gt;(p.row)) +
            std::abs(static_cast&lt;int&gt;(col) - static_cast&lt;int&gt;(p.col)));
    }

    // reading the point from an istream
    friend std::istream&amp; operator&gt;&gt;(std::istream&amp; is, point_t&amp; p) {
        return is &gt;&gt; p.row &gt;&gt; p.col;
    }

private:
    unsigned row;
    unsigned col;
};

int main() {
    unsigned rows;
    unsigned cols;
    point_t ref;

    if(std::cin &gt;&gt; rows &gt;&gt; cols &gt;&gt; ref) {
        // check that it's not too big
        if(rows &gt; std::numeric_limits&lt;int&gt;::max() ||
           cols &gt; std::numeric_limits&lt;int&gt;::max()) {
            std::cerr &lt;&lt; "matrix too big\n";
            return 1;
        }

        // create a 2D vector
        std::vector&lt;std::vector&lt;unsigned&gt;&gt; mat(rows, std::vector&lt;unsigned&gt;(cols, 0));

        // fill the matrix
        for(unsigned row = 0; row &lt; rows; ++row) {
            for(unsigned col = 0; col &lt; cols; ++col) {
                // use the distance_to function
                mat[row][col] = ref.distance_to(point_t(row, col));
            }
        }

        // print the result
        for(const auto&amp; row : mat) {
            for(auto col : row) std::cout &lt;&lt; col;
            std::cout &lt;&lt; '\n';
        }
    }
}


Input (with a 0-based reference point)

3 4 2 2


Output

4323
3212
2101