首页 > 其他分享 >基站信号地图

基站信号地图

时间:2022-10-05 16:57:20浏览次数:61  
标签:commandVal lineBuf err int 地图 基站 信号 row result


/*
* Copyright (c) Huawei Technologies Co., Ltd. 2019-2021. All rights reserved.
* Description: 上机编程认证
* Note: 缺省代码仅供参考,可自行决定使用、修改或删除
* 只能import Go标准库
*/
package main

import (
"bufio"
"fmt"
"io"
"os"
"strconv"
"strings"
)

type command struct {
cmd string
row int
col int
}

// 待实现函数,在此函数中填入答题代码
func getMatrixSum(rows int, cols int, baseStations [][]int, commands []command) int {
maxRowHeight := 0
minRowHeight := 0
maxColHeight := 0
minColHeight := 0
// 获取所有基站
for _, commandVal := range commands {
fmt.Println(commandVal, commandVal.cmd)
if commandVal.cmd == "delete" {
for _, baseStation := range baseStations {
if commandVal.row + 1 > rows {
maxRowHeight = rows
} else {
maxRowHeight = commandVal.row + 1
}
if commandVal.row - 1 < 0 {
minRowHeight = 0
} else {
minRowHeight = commandVal.row - 1
}
if commandVal.col + 1 > cols {
maxColHeight = cols
} else {
maxColHeight = commandVal.col + 1
}
if commandVal.col - 1 < 0 {
minColHeight = 0
} else {
minColHeight = commandVal.col - 1
}
if baseStation[0] <= maxRowHeight && baseStation[0] >= minRowHeight &&
baseStation[1] <= maxColHeight && baseStation[1] >= minColHeight {
baseStation[0] = 0
baseStation[1] = 0
}
}
}
if commandVal.cmd == "add" {
addValue := make([]int, 2)
addValue[0] = commandVal.row
addValue[1] = commandVal.col
baseStations = append(baseStations, addValue)
}
}
// 求信号个数
count := 0
totalCount := 0
for _, baseStation := range baseStations {
fmt.Println(baseStation[0], baseStation[1])
if baseStation[0] != 0 {
totalCount += 8
count ++
}
}
return totalCount
}

func main() {
reader := bufio.NewReader(os.Stdin)
var rows, cols int
if _, err := fmt.Fscanf(reader, "%d %d\n", &rows, &cols); err != nil {
return
}
baseStationCnt := readInputInt(reader)
coordinates := readInputIntArrayFromNlines(reader, baseStationCnt, 2)
commands := readInputStrArrayFromNlines(reader)
result := getMatrixSum(rows, cols, coordinates, commands)
fmt.Println(result)
}

func readInputInt(reader *bufio.Reader) int {
var num int
if _, err := fmt.Fscanf(reader, "%d\n", &num); err != nil {
fmt.Println(err.Error())
return 0
}
return num
}

func readInputIntArrayFromNlines(reader *bufio.Reader, row int, col int) [][]int {
if row <= 0 {
return [][]int{}
}

result := make([][]int, 0, row)
for i := 0; i < row; i++ {
lineBuf, err := reader.ReadString('\n')
if err != nil && err != io.EOF {
fmt.Println(err.Error())
return nil
}
lineBuf = strings.TrimRight(lineBuf, "\r\n")
lineBuf = strings.TrimSpace(lineBuf)
ints := map2IntArray(lineBuf, " ")
if len(ints) != col {
fmt.Println("col len is not " + strconv.Itoa(col))
return [][]int{}
}
result = append(result, ints)
}

return result
}

func map2IntArray(str string, dem string) []int {
tempArray := strings.Split(str, dem)
result := make([]int, len(tempArray))
for index, value := range tempArray {
value = strings.TrimSpace(value)
intVal, _ := strconv.Atoi(value)
result[index] = intVal
}
return result
}

func readInputStrArrayFromNlines(reader *bufio.Reader) []command {
var line int
if _, err := fmt.Fscanf(reader, "%d\n", &line); err != nil {
return []command{}
}
result := make([]command, 0, line)
for i := 0; i < line; i++ {
lineBuf, err := reader.ReadString('\n')
if err != nil && err != io.EOF {
fmt.Println(err.Error())
return nil
}
lineBuf = strings.TrimRight(lineBuf, "\r\n")
lineBuf = strings.TrimSpace(lineBuf)
comm := strings.Split(lineBuf, " ")
row, _ := strconv.Atoi(comm[1])
col, _ := strconv.Atoi(comm[2])
result = append(result, command{comm[0], row, col})
}
}
return result
}




/***
4 6
3
2 2
3 3
4 4
3
delete 4 3
add 1 2
delete 2 5
*/

标签:commandVal,lineBuf,err,int,地图,基站,信号,row,result
From: https://www.cnblogs.com/gongxianjin/p/16755826.html

相关文章