/**
1892 · Mine-sweeping
Algorithms
Medium
Accepted Rate
36%
DescriptionSolutionNotesDiscussLeaderboard
Description
It’s an easy mine-sweeping game.You are given a n*m matrix as the map of game.
Each position has a value (0 or 1, 1 means there is no thunder, 0 means there is thunder).
You are given a start position (x, y). X is the number of rows and Y is the number of columns(x, y are both counting from 0).
If there is no mine at current position then you can reach in four directions, otherwise, can’t reach the new position.
Returns all accessible coordinates.
0<n,m<=200.
You should return an array in any order. The array includes all reachable position coordinates.
Example
Example 1
input:
[[1,0,0,0],[1,0,0,0],[0,1,1,1],[0,1,0,0]]
[0,1]
output:
[[0,1]]
explain:
There is 0 at [0,1], so you can’t move,then the answer is [[0,1]].
Example 2
input:
[[1,0,0,0],[1,0,0,0],[0,1,1,1],[0,1,0,0]]
[1,0]
output:
[[0,0],[1,0],[1,1],[2,0],[0,1]]
explain:
You can arrive [[0,0],[1,1],[2,0]] from [1,0], then arrive [[0,1]] from [0,0].
Tags
Company
Bloomberg
https://blog.csdn.net/qq_46105170/article/details/111714652
https://www.lintcode.com/problem/1892/
*/
貌似我搞明白之前的队列的使用问题了,对go还是不熟啊,稍微改了下类型就可以用了,没毛病。
/**
队列:
基本操作是入队(Enqueue),在表的末端插入一个元素
出队(Dequeue),删除(或返回)在表头的元素
*/
type Item []int
// 队列结构体
type Queue struct {
Items []Item
}
type IQueue interface {
New() Queue
Enqueue(t Item)
Dequeue(t Item)
IsEmpty() bool
Size() int
}
// 新建
func (q *Queue)New() *Queue {
q.Items = []Item{}
return q
}
// 入队
func (q *Queue) Enqueue(data Item) {
q.Items = append(q.Items, data)
}
// 出队
func (q *Queue) Dequeue() *Item {
// 由于是先进先出,0为队首
item := q.Items[0]
q.Items = q.Items[1: len(q.Items)]
return &item
}
// 队列是否为空
func (q *Queue) IsEmpty() bool {
return len(q.Items) == 0
}
// 队列长度
func (q *Queue) Size() int {
return len(q.Items)
}
var q Queue
func initQueue() *Queue {
if q.Items == nil{
q = Queue{}
q.New()
}
return &q
}
/**
* @param Mine_map: an array represents the map.
* @param Start: the start position.
* @return: return an array including all reachable positions.
*/
func Mine_sweeping (Mine_map [][]int, Start []int) [][]int {
// write your code here
var res [][]int
var x int = Start[0]
var y int = Start[1]
if Mine_map[x][y] == 0 {
res = append(res, []int{x, y})
return res
}
var m int = len(Mine_map)
var n int = len(Mine_map[0])
queue := initQueue()
var visited = make([][]bool, m)
for i := 0; i < m; i++ {
visited[i] = make([]bool, n)
for j := 0; j < n; j++ {
visited[i][j] = false
}
}
visited[x][y] = true
queue.Enqueue(Start)
res = append(res, []int{x, y})
var d []int = []int{1, 0, -1, 0, 1}
for {
if queue.IsEmpty() {
break
}
var cur *Item = queue.Dequeue()
for i := 0; i < 4; i++ {
var nextX int = (*cur)[0] + d[i]
var nextY int = (*cur)[1] + d[i + 1]
if 0 <= nextX && nextX < m && 0 <= nextY && nextY < n && !visited[nextX][nextY] {
visited[nextX][nextY] = true
res = append(res, []int{nextX, nextY})
if Mine_map[nextX][nextY] == 1 {
queue.Enqueue([]int{nextX, nextY})
}
}
}
}
return res
}