AcWing 1015. 摘花生. (JavaScript )--麻烦的输入
原题链接
简单
作者:
cp777
,
2021-03-06 16:58:13
,
所有人可见
,
阅读 278
const N = 110;
let g = [];
g.push('');
let f = [];
for (let i = 0; i < N; i++) f[i] = new Int32Array(N);
let n = 0;
let cnt = 0; // T组数据
let cnt2 = 0; // 每组数据
let flag = false; //判断是每组数据的第一行还是接下来的r行数据
let r = 0,
c = 0;
let buf = '';
process.stdin.on('readable', function () {
let chunk = process.stdin.read();
if (chunk) buf += chunk.toString();
});
let getInputNums = line => line.split(' ').filter(s => s !== '').map(x => parseInt(x));
let getInputStr = line => line.split(' ').filter(s => s !== '');
process.stdin.on('end', function () {
buf.split('\n').forEach(function (line, lineIdx) {
if (lineIdx === 0) {
n = getInputNums(line)[0];
} else if (cnt <= n) {
if (!flag) {
let arr = getInputNums(line);
r = arr[0];
c = arr[1];
flag = true;
cnt2 = 0;
} else {
let arr = getInputNums(line);
// console.log('arr:'+arr);
arr.unshift(0);
g.push(arr);
cnt2++
if (cnt2 === r) {
for (let i = 1; i <= r; i++) {
for (let j = 1; j <= c; j++) {
let x = parseInt(g[i][j]);
f[i][j] = Math.max(f[i - 1][j], f[i][j - 1]) + x;
}
}
console.log(f[r][c]);
flag = false; //数据输入完毕 ,状态清理
cnt++;
g = []; //g恢复初始
g.push('');
}
}
}
});
});