题解 | #地下迷宫#
地下迷宫
https://www.nowcoder.com/practice/571cfbe764824f03b5c0bfd2eb0a8ddf
package main import ( "bufio" "fmt" "os" "strconv" "strings" ) var end [2]int var start [3]int var Maxline []int var migong [][]int var indexI int var indexJ int var energy int var now [][]int func findout() { now = make([][]int,1) now[0]=make([]int,3) copy(now[0],start[:]) for len(now)!=0{ tmp := make([][]int,0) for _,i:=range now{ leni := len(i) x:=i[leni-2] y:=i[leni-1] if x==end[0]&&y==end[1]&&(len(Maxline)==0||i[0]>Maxline[0]){ Maxline = i continue } if x + 1<=indexI-1&&migong[x+1][y]==1{ aa :=true for t:=1;t<leni-2;t = t+2{ if x+1 ==i[t]&&y==i[t+1]{ aa = false } } if aa{ var tmps []int tmps= append(tmps,i...) tmps = append(tmps,x+1,y) tmp = append(tmp, tmps) } } if x - 1>=0&&migong[x-1][y]==1&&i[0] - 3>=0{ aa :=true for t:=1;t<leni-2;t = t+2{ if x-1 ==i[t]&&y==i[t+1]{ aa = false } } if aa{ var tmps []int tmps= append(tmps,i...) tmps = append(tmps,x-1,y) tmps[0] = tmps[0] - 3 tmp = append(tmp, tmps) } } if y + 1<=indexJ-1&&migong[x][y+1]==1&&i[0] - 1>=0{ aa :=true for t:=1;t<leni-2;t = t+2{ if i[leni-2] ==i[t]&&i[leni-1]+1==i[t+1]{ aa = false } } if aa{ var tmps []int tmps= append(tmps,i...) tmps = append(tmps,x,y+1) tmps[0] = tmps[0] - 1 tmp = append(tmp, tmps) } } if y - 1>=0&&migong[x][y-1]==1&&i[0] - 1>=0{ aa :=true for t:=1;t<leni-2;t = t+2{ if i[leni-2] ==i[t]&&i[leni-1]-1==i[t+1]{ aa = false } } if aa{ var tmps []int tmps= append(tmps,i...) tmps = append(tmps,x,y-1) tmps[0] = tmps[0] - 1 tmp = append(tmp, tmps) } } } now = tmp } return } func main() { input := bufio.NewScanner(os.Stdin) input.Scan() data:=input.Text() d:=strings.Split(data," ") indexI,_=strconv.Atoi(d[0]) indexJ,_=strconv.Atoi(d[1]) energy,_=strconv.Atoi(d[2]) migong=make([][]int,indexI) for i:=0;i<indexI;i++{ input.Scan() data:=input.Text() d:=strings.Split(data," ") migong[i] = make([]int,indexJ) for j:=0;j<indexJ;j++{ a,_:=strconv.Atoi(d[j]) migong[i][j] = a } } end=[2]int{0,indexJ - 1} start=[3]int{energy,0,0} Maxline = make([]int,0) findout() if len(Maxline)==0{ fmt.Println("Can not escape!") }else { lenMaxlin:=len(Maxline) for i:=1;i<lenMaxlin-1-2;i = i+2{ fmt.Printf("[%d,%d],",Maxline[i],Maxline[i+1]) } fmt.Printf("[%d,%d]",Maxline[lenMaxlin-2],Maxline[lenMaxlin-1]) } }