[Solved] How to add a value in a matrix and make it decrease in radial way? [closed]


[EDIT]

So based on you’re edit i thought this solution. Since you don’t specify any programming language i’ll use some c-like functional programming. I’ll leave you the work of transforming it to object oriented if you need it.

Input:

  • Global maxtrix that starts in M[0,0] and ends in M[100’000, 100’000] (Note that, to make it easier to understand, i want check if i reach board, you should check)
  • Position for first number (X,Y) (in your example 5,5)
  • First number called N (in your example 16)
  • Decrement for along axis D (in your example 3)

    main(){
       computeValues(M, X, Y, N, D)
    }
    
    computeValues(M, X, Y, N, D){
       M[X,Y] = N
       if( N-D <= 0 ) return;
       if( M[X,Y-1] == 0 ){
          computeValues(M, X, Y-1, N-D, D)
       }
       if( M[X,Y+1] == 0 ){
          computeValues(M, X, Y+1, N-D, D)
       }
       if( M[X-1,Y] == 0 ){
          computeValues(M, X-1, Y, N-D, D)
       }
       if( M[X+1,Y] == 0 ){
          computeValues(M, X+1, Y, N-D, D)
       }
    }
    

It should be pretty self-explanatory, anyway this function ends when reach 0 with the N-D <= 0 control. Once a position recive the number, it check for near position not yet evaluated and assign them N-D number, the new position if N-D <= 0 will continue to check for near position not evaluated and so on…

IMPORTANT NOTE: This function return the matrix as you asked in the text of your answer, which is a little bit different from the image you posted (Example M[4,4] in your example is 11 but it should be 10 and M[5,0] should be 1)


OLD ANSWER

This should not to much difficult. The only things you missed to say is how to compute value by value.

To accomplish this algorithm you need to know (and tell us, if you want) the rule or maybe the function that allow as to get the right value.

An example to make my point clearer:

  Y
Y X Y
  Y

If X=17 how do we know if Y, for example, need to be 15 or 14?

[If you edit you’re answer whit those information i’ll try to answer you properly]

2

solved How to add a value in a matrix and make it decrease in radial way? [closed]