Created
December 2, 2019 14:21
-
-
Save SyureNyanko/5b2b4e63d3ee200dfa72a827d5c2a869 to your computer and use it in GitHub Desktop.
This file contains 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
package main | |
import "fmt" | |
var LARGE = 100000 | |
var p = [][]string{ | |
[]string{"S", "#", "#", "#"}, | |
[]string{".", ".", ".", "#"}, | |
[]string{"#", ".", ".", "."}, | |
[]string{".", ".", "#", "E"}, | |
[]string{"#", ".", ".", "."}, | |
} | |
var sx, sy int = 0, 0 | |
var gx, gy int = 0, 0 | |
var lx, ly = len(p), len(p[0]) | |
type vector struct { | |
x int | |
y int | |
} | |
/* If it is wall, return false */ | |
func updateMap(q [][]int, x, y, r int) bool { | |
if x >= 0 && y >= 0 && x < lx && y < ly && (p[x][y] == "." || p[x][y] == "E") { | |
if q[x][y] > r+1 { | |
q[x][y] = r + 1 | |
return true | |
} | |
} | |
return false | |
} | |
func solve(q [][]int) { | |
type Vectors []vector | |
var queue Vectors | |
queue = append(queue, vector{sx, sy}) | |
for len(queue) > 0 { | |
/* pop from queue */ | |
v := queue[0] | |
queue = queue[1:] | |
/* pop end */ | |
if updateMap(q, v.x+1, v.y, q[v.x][v.y]) { | |
queue = append(queue, vector{v.x + 1, v.y}) | |
} | |
if updateMap(q, v.x, v.y+1, q[v.x][v.y]) { | |
queue = append(queue, vector{v.x, v.y + 1}) | |
} | |
if updateMap(q, v.x-1, v.y, q[v.x][v.y]) { | |
queue = append(queue, vector{v.x - 1, v.y}) | |
} | |
if updateMap(q, v.x, v.y-1, q[v.x][v.y]) { | |
queue = append(queue, vector{v.x, v.y - 1}) | |
} | |
} | |
} | |
func main() { | |
q := make([][]int, len(p)) | |
for i := range q { | |
q[i] = make([]int, len(p[0])) | |
for j := range q[i] { | |
q[i][j] = LARGE | |
if p[i][j] == "S" { | |
q[i][j] = 0 | |
sx, sy = i, j | |
} | |
if p[i][j] == "E" { | |
gx, gy = i, j | |
} | |
} | |
} | |
solve(q) | |
for _, v1 := range q { | |
for _, v2 := range v1 { | |
if LARGE == v2 { | |
fmt.Printf("#") | |
} else { | |
fmt.Printf("%d", v2) | |
} | |
} | |
fmt.Println() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment