Reputation: 21
Input data: number of rows, number of columns, and the point's coordinates
the named position will be given the value 0.
note: indexing starts from 1 (bad practice, I know, but that's the requirement)
i.e: for this input 4 5 1 1
this matrix will be generated:
0 1 2 3 4
1 2 3 4 5
2 3 4 5 6
3 4 5 6 7
I don't want the code, but if someone could give me a hint or something to help, it would be greatly appreciated!! If code is easier than explaining a way to solve it, then please don't hesitate to post it.(C++) Edit: I got a solution, how could I make this faster?
#include <iostream>
using namespace std;
int main()
{
int v[501][501], i, j, m, n, o, p;
cin >> m >> n >> o >> p;
for (i = o; i >= 1; i--)
v[i][p] = o - i;
for(i = o;i <= m; i++)
v[i][p] = i - o;
for(i = 1; i <= m; i++)
for(j = 1; j <= n; j++){
if(j < p)
v[i][j] = -j + p + v[i][p];
else if(j > p)
v[i][j] = j - p + v[i][p];
}
for(i = 1 ; i <= m; i++){
for(j = 1; j <= n; j++)
cout << v[i][j] << " ";
cout << '\n';
}
}
Upvotes: 0
Views: 61
Reputation: 546
Try to draw the answer for a few examples and you will see a pattern that you can mimic.
For example:
5 5 3 3
4 3 2 3 4
3 2 1 2 3
2 1 0 1 2
3 2 1 2 3
4 3 2 3 4
By the way this is a standard question to solve with BFS.
Upvotes: 1