-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.rs
131 lines (111 loc) · 3.68 KB
/
main.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
use std::{
collections::{HashMap, HashSet},
io::{self, BufRead},
};
use rusttype::Point as RPoint;
type Point = RPoint<i32>;
type Points = Vec<Point>;
type Cost = i32;
type CostMap = HashMap<Point, Cost>;
type Map = Vec<Vec<Cost>>;
fn main() {
let stdin = io::stdin();
let lines = stdin.lock().lines();
// parse input
let mut map = Map::new();
for _line in lines {
let line = _line.unwrap();
let mut row: Vec<Cost> = Vec::new();
for char in line.chars() {
let cell = char.to_string().parse::<Cost>().unwrap();
row.push(cell);
}
map.push(row);
}
let start = Point { x: 0, y: 0 };
let gorl = Point {
x: (map[0].len() - 1) as i32,
y: (map.len() - 1) as i32,
};
// A* search
let mut next_points: HashSet<Point> = HashSet::new();
let mut estimated_costs = CostMap::new();
let mut visited_points: HashSet<Point> = HashSet::new();
let mut parents: HashMap<Point, Point> = HashMap::new();
estimated_costs.insert(start, heuristics(start, &map));
next_points.insert(start);
loop {
if next_points.len() == 0 {
panic!("path not found...");
}
let point = *next_points
.iter()
.reduce(|x, y| {
if estimated_costs.get(x).unwrap() < estimated_costs.get(y).unwrap() {
x
} else {
y
}
})
.unwrap();
if point == gorl {
break;
}
next_points.remove(&point);
visited_points.insert(point);
let cost = *estimated_costs.get(&point).unwrap();
let goal_cost = heuristics(point, &map);
for around_point in get_around_points(point, &map) {
let around_cost = cost - goal_cost
+ map[around_point.y as usize][around_point.x as usize]
+ heuristics(around_point, &map);
let visited = visited_points.contains(&around_point);
let is_next_point = next_points.contains(&around_point);
if !visited && !is_next_point {
*estimated_costs.entry(around_point).or_default() = around_cost;
next_points.insert(around_point);
parents.insert(around_point, point);
} else {
let prev_around_cost = *estimated_costs.get(&around_point).unwrap();
if around_cost < prev_around_cost {
*estimated_costs.entry(around_point).or_default() = around_cost;
visited_points.remove(&around_point);
next_points.insert(around_point);
parents.insert(around_point, point);
}
}
}
}
let mut total_cost = 0;
let mut current_point = gorl;
while current_point != start {
total_cost += map[current_point.y as usize][current_point.x as usize];
current_point = *parents.get(¤t_point).unwrap();
}
println!("ans {}", total_cost);
}
fn heuristics(point: Point, map: &Map) -> Cost {
let max_x = map[0].len() - 1;
let max_y = map.len() - 1;
max_x as i32 - point.x + max_y as i32 - point.y
}
fn get_around_points(point: Point, map: &Map) -> Points {
let mut points = Points::new();
let x = point.x;
let y = point.y;
let max_x = map[0].len() - 1;
let max_y = map.len() - 1;
if x > 0 {
points.push(Point { x: x - 1, y: y })
}
if y > 0 {
points.push(Point { x: x, y: y - 1 })
}
if x < max_x as i32 {
points.push(Point { x: x + 1, y: y })
}
if y < max_y as i32 {
points.push(Point { x: x, y: y + 1 })
}
points
}