Created
March 13, 2014 08:53
-
-
Save Meyermagic/9524658 to your computer and use it in GitHub Desktop.
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
extern crate collections; | |
use collections::HashSet; | |
#[deriving(Eq, Hash)] | |
struct XYZ { | |
x: int, | |
y: int, | |
z: int | |
} | |
fn main() { | |
let mut connected = HashSet::new(); | |
let mut border = HashSet::new(); | |
let middle = XYZ{x: 0, y: 0, z: 0}; | |
border.insert(middle); | |
while border.len() > 0 { | |
let choice = *(border.iter().next().unwrap()); | |
border.remove(&choice); | |
connected.insert(choice); | |
let cxp = XYZ{x: choice.x + 1, y: choice.y, z: choice.z}; | |
let cxm = XYZ{x: choice.x - 1, y: choice.y, z: choice.z}; | |
let cyp = XYZ{x: choice.x, y: choice.y + 1, z: choice.z}; | |
let cym = XYZ{x: choice.x, y: choice.y - 1, z: choice.z}; | |
let czp = XYZ{x: choice.x, y: choice.y, z: choice.z + 1}; | |
let czm = XYZ{x: choice.x, y: choice.y, z: choice.z - 1}; | |
if !connected.contains(&cxp) { | |
border.insert(cxp); | |
} | |
if !connected.contains(&cxm){ | |
border.insert(cxm); | |
} | |
if !connected.contains(&cyp){ | |
border.insert(cyp); | |
} | |
if !connected.contains(&cym) { | |
border.insert(cym); | |
} | |
if !connected.contains(&czp){ | |
border.insert(czp); | |
} | |
if !connected.contains(&czm) { | |
border.insert(czm); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment