Last active
April 13, 2017 02:34
-
-
Save jackmott/5933c888b703d94c6d40678d20f721dd to your computer and use it in GitHub Desktop.
learning rust
This file contains hidden or 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
| #[derive(Clone,PartialEq,Eq)] | |
| 24 enum Tile { | |
| 25 MeasuredWater, | |
| 26 Water, | |
| 27 Land, | |
| 28 Finalized(usize), | |
| 29 } | |
| 30 | |
| 31 | |
| 32 fn get_area(map: &mut Vec<Vec<Tile>>, startx: usize, starty: usize) -> usize { | |
| 33 match map[starty][startx] { | |
| 34 Tile::Water => { | |
| 35 let mut area: usize = 0; | |
| 36 let mut memo = Vec::new(); | |
| 37 let mut queue = Vec::new(); | |
| 38 queue.push((startx, starty)); | |
| 39 while queue.len() > 0 { | |
| 40 let (x, y) = queue.pop().unwrap(); | |
| 41 if map[y][x] == Tile::Water { | |
| 42 let mut w = x; | |
| 43 let mut e = x; | |
| 44 while w > 0 && map[y][w - 1] == Tile::Water { | |
| 45 w -= 1; | |
| 46 } | |
| 47 while e < map[0].len() - 1 && map[y][e + 1] == Tile::Water { | |
| 48 e += 1; | |
| 49 } | |
| 50 | |
| 51 for i in w..e + 1 { | |
| 52 area += 1; | |
| 53 map[y][i] = Tile::MeasuredWater; | |
| 54 memo.push((i, y)); | |
| 55 if y > 0 && map[y - 1][i] == Tile::Water { | |
| 56 queue.push((i, y - 1)); | |
| 57 } | |
| 58 if y < map.len() - 1 && map[y + 1][i] == Tile::Water { | |
| 59 queue.push((i, y + 1)); | |
| 60 } | |
| 61 } | |
| 62 } | |
| 63 } | |
| 64 for (x, y) in memo { | |
| 65 map[y][x] = Tile::Finalized(area); | |
| 66 } | |
| 67 area | |
| 68 } | |
| 69 Tile::Finalized(area) => area, | |
| 70 _ => 0, | |
| 71 } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment