1001.网格照明
1001.网格照明
题解
行 (横坐标 x )
列 (纵坐标 y )
撇 ( 记录 x-y )
捺 ( 记录 x+y )
其实就是模拟题,很简单,不知道为什么算hard
代码
package main func gridIllumination(n int, lamps [][]int, queries [][]int) []int { type pair struct { x, y int } points := make(map[pair]bool) row := make(map[int]int) col := make(map[int]int) diagonal := make(map[int]int) antiDiagonal := make(map[int]int) for _, lamp := range lamps { r, c := lamp[0], lamp[1] p := pair{r, c} if points[p] { continue } points[p] = true row[r]++ col[c]++ diagonal[r-c]++ antiDiagonal[r+c]++ } ans := make([]int, len(queries)) for i, quire := range queries { r, c := quire[0], quire[1] if row[r] > 0 || col[c] > 0 || diagonal[r-c] > 0 || antiDiagonal[r+c] > 0 { ans[i] = 1 } for x := r - 1; x <= r+1; x++ { for y := c - 1; y <= c+1; y++ { if x < 0 || x >= n || y < 0 || y >= n || !points[pair{x, y}] { continue } delete(points, pair{x, y}) row[x]-- col[y]-- diagonal[x-y]-- antiDiagonal[x+y]-- } } } return ans }