//find cycles for every empty cell (X[i][j] == 0) find the loop starting and ending at the empty cell that
//             only turns at full cells (goes through empty cells) and is shortest (closest to beginning cell))
//then for each cycle do:
//mark C's with -, then +, and so on, beginning from empty cell, and calculate sum of C's with corresponding signs
//    if > 0 -> add to list
//           find max element in list
//           for the cycle that begins with that element:
//              mark cells values (X's) starting with +, then -, and so on
//              find the min value (X[i][j]) from the ones with -
//              write that value in the beginning of the cycle
//              recalculate the rest of the cells' values by either adding or subtracting that value from their value
//           after recalculation, if there are cells whose value is 0 (the number of empty cells in the matrix
//           	is no longer height + width - 1), one of them must become a "base 0" and be treated as a full cell
//           then start from beginning of algorithm
//calculate the sum of the values of the full cells multiplied by it's C (X[i][j] * C[i][j])
find cycles for every empty cell (X[i][j] == 0) find the loop starting and ending at the empty cell that
only turns at full cells (goes through empty cells) and is shortest (closest to beginning cell))
then for each cycle do:
mark C's with -, then +, and so on, beginning from empty cell, and calculate sum of C's with corresponding signs
if > 0 -> add to list
find max element in list
for the cycle that begins with that element:
mark cells values (X's) starting with +, then -, and so on
find the min value (X[i][j]) from the ones with
write that value in the beginning of the cycle
recalculate the rest of the cells' values by either adding or subtracting that value from their value
after recalculation, if there are cells whose value is 0 (the number of empty cells in the matrix
is no longer height + width - 1), one of them must become a "base 0" and be treated as a full cell
then start from beginning of algorithm
calculate the sum of the values of the full cells multiplied by it's C (X[i][j] * C[i][j])
// find cycles for every empty cell ( X [ i ] [ j ] == 0 ) find the loop starting and ending at the empty cell that // only turns at full cells ( goes through empty cells ) and is shortest ( closest to beginning cell )) // then for each cycle do : // mark C ' s with - then + and so on beginning from empty cell and calculate sum of C ' s with corresponding signs // if > 0 - > add to list // find max element in list // for the cycle that begins with that element : // mark cells values ( X ' s ) starting with + then - and so on // find the min value ( X [ i ] [ j ] ) from the ones with - // write that value in the beginning of the cycle // recalculate the rest of the cells ' values by either adding or subtracting that value from their value // after recalculation if there are cells whose value is 0 ( the number of empty cells in the matrix // is no longer height + width - 1 ) one of them must become a " base 0 " and be treated as a full cell // then start from beginning of algorithm // calculate the sum of the values of the full cells multiplied by it ' s C ( X [ i ] [ j ] * C [ i ] [ j ] )
func distributionMethod(C, X *[height][width]int) ([]int, int) {
	matrix := newMatrix(X)
	emptyCells := getEmptyCells(matrix)
	invalidDeltas := make(map[int][]Cell)
	var validDeltas []int
	for _, cell := range emptyCells {
		loop := []Cell{cell}
		hasLoop := findLoop(cell, cell, matrix, &loop)
		if !hasLoop {
			fmt.Fprintf(os.Stderr, "\ncycle for cell(%d,%d) not found\n", cell.row+1, cell.col+1)
		}
		clearVisitedCells(&matrix)
		delta := findLoopDelta(loop, *C)
		if delta > 0 {
			invalidDeltas[delta] = loop
		} else {
			validDeltas = append(validDeltas, delta)
		}
	}
	if len(invalidDeltas) != 0 {
		recalculateCells(invalidDeltas, X)
		return distributionMethod(C, X)
	}
	sum := 0
	for i := 0; i < height; i++ {
		for j := 0; j < width; j++ {
			if X[i][j] != 0 {
				sum += X[i][j] * C[i][j]
			}
		}
	}
	return validDeltas, sum
}
func distributionMethod ( C , X * [ height ] [ width ] int ) ( [ ] int , int ) { matrix := newMatrix ( X ) emptyCells := getEmptyCells ( matrix ) invalidDeltas := make ( map [ int ] [ ] Cell ) var validDeltas [ ] int for _ , cell := range emptyCells { loop := [ ] Cell { cell } hasLoop := findLoop ( cell , cell , matrix , & loop ) if ! hasLoop { fmt . Fprintf ( os . Stderr , " \n \n " , cell . row + 1 , cell . col + 1 ) } clearVisitedCells ( & matrix ) delta := findLoopDelta ( loop , * C ) if delta > 0 { invalidDeltas [ delta ] = loop } else { validDeltas = append ( validDeltas , delta ) } } if len ( invalidDeltas ) != 0 { recalculateCells ( invalidDeltas , X ) return distributionMethod ( C , X ) } sum := 0 for i := 0 ; i < height ; i ++ { for j := 0 ; j < width ; j ++ { if X [ i ] [ j ] != 0 { sum += X [ i ] [ j ] * C [ i ] [ j ] } } } return validDeltas , sum }
find cycles for every empty cell (X[i][j] == 0) find the loop starting and ending at the empty cell that
only turns at full cells (goes through empty cells) and is shortest (closest to beginning cell))
then for each cycle do:
mark C's with -, then +, and so on, beginning from empty cell, and calculate sum of C's with corresponding signs
if > 0 -> add to list
find max element in list
for the cycle that begins with that element:
mark cells values (X's) starting with +, then -, and so on
find the min value (X[i][j]) from the ones with
write that value in the beginning of the cycle
recalculate the rest of the cells' values by either adding or subtracting that value from their value
after recalculation, if there are cells whose value is 0 (the number of empty cells in the matrix
is no longer height + width - 1), one of them must become a "base 0" and be treated as a full cell
then start from beginning of algorithm
calculate the sum of the values of the full cells multiplied by it's C (X[i][j] * C[i][j])
find cycles for every empty cell ( X [ i ] [ j ] == 0 ) find the loop starting and ending at the empty cell that only turns at full cells ( goes through empty cells ) and is shortest ( closest to beginning cell )) then for each cycle do : mark C ' s with - then + and so on beginning from empty cell and calculate sum of C ' s with corresponding signs if > 0 - > add to list find max element in list for the cycle that begins with that element : mark cells values ( X ' s ) starting with + then - and so on find the min value ( X [ i ] [ j ] ) from the ones with write that value in the beginning of the cycle recalculate the rest of the cells ' values by either adding or subtracting that value from their value after recalculation if there are cells whose value is 0 ( the number of empty cells in the matrix is no longer height + width - 1 ) one of them must become a " base 0 " and be treated as a full cell then start from beginning of algorithm calculate the sum of the values of the full cells multiplied by it ' s C ( X [ i ] [ j ] * C [ i ] [ j ] )
//retry with different loops? and/or different base zero