Skip to content

Instantly share code, notes, and snippets.

@paul-english
Created November 17, 2012 09:34
Show Gist options
  • Save paul-english/4094507 to your computer and use it in GitHub Desktop.
Save paul-english/4094507 to your computer and use it in GitHub Desktop.
var assert = require('assert');
function rice_chessboard (A) {
if (A.length === 0) return 0;
if (A[0].length === 0) return 0;
var count = A[0][0];
var left_path = extract_sub(0, A);
var right_path = extract_sub(1, A);
console.log('paths');
console.log(left_path);
console.log(right_path);
var left_total = rice_chessboard(left_path);
var right_total = rice_chessboard(right_path);
return count + ((left_total > right_total) ? left_total : right_total);
function extract_sub(direction, matrix) {
if (direction === 0) {
return matrix.slice(1, matrix.length);
} else {
var sub = [];
for (var i = 0, len = matrix.length; i < len; i++) {
sub.push(matrix[i].slice(1, matrix[i].length));
}
return sub;
}
}
}
var testMatrix = [
[2, 2, 3, 0],
[0, 3, 1, 1],
[1, 2, 2, 1],
[4, 1, 2, 2]
];
console.log('run');
var out = rice_chessboard(testMatrix);
console.log('out', out)
assert.equal(out, 15);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment