Lintcode1892 · Mine-sweeping go

/**
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
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值