You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
48 lines
1.5 KiB
48 lines
1.5 KiB
use std::fs::File; |
|
use std::io::{ self, BufRead }; |
|
|
|
struct Tr { |
|
nums: Vec<Vec<u8>>, |
|
sums: Vec<Vec<u32>> |
|
} |
|
|
|
impl Tr { |
|
pub fn new() -> Tr { |
|
let mut nums: Vec<Vec<u8>> = Vec::new(); |
|
let mut sums: Vec<Vec<u32>> = Vec::new(); |
|
let file = File::open("tr.txt").unwrap(); |
|
let lines = io::BufReader::new(file).lines(); |
|
for ln in lines { |
|
//let sline = ln.split(' '); |
|
let ns: Vec<u8> = ln.unwrap().split(' ').map(|x| x.parse::<u8>().unwrap()).collect(); |
|
sums.push(vec!(0; ns.len())); |
|
nums.push(ns); |
|
} |
|
return Tr { nums: nums, sums }; |
|
} |
|
pub fn get(&self, r: usize, c: usize) -> u8 { |
|
return self.nums[r][c]; |
|
} |
|
pub fn compute(&mut self) { |
|
self.sums[0][0] = self.get(0, 0) as u32; |
|
for c in 1..self.nums.len() { |
|
for r in 0..self.nums[c].len() { |
|
let left_top_candidate = if r > 0 { self.sums[c-1][r-1] } else { 0 }; |
|
let right_top_candidate = if r < self.sums[c-1].len() { self.sums[c-1][r] } else { 0 }; |
|
self.sums[c][r] = left_top_candidate.max(right_top_candidate) + self.get(c, r) as u32 |
|
} |
|
} |
|
} |
|
pub fn best_bottom_sum(&self) -> u32 { |
|
self.sums.last().unwrap().iter().max().unwrap().clone() |
|
} |
|
} |
|
|
|
|
|
fn main() { |
|
let mut tr = Tr::new(); |
|
tr.compute(); |
|
//let n = tr.get(2,2); |
|
let best_sum = tr.best_bottom_sum(); |
|
println!("best: {}", best_sum); |
|
}
|
|
|