Created
November 17, 2012 09:34
-
-
Save paul-english/4094507 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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