kamacoder-solutions
kamacoder-solutions copied to clipboard
101. 总是提示 潜在的数组或指针越界,请检查代码
如题,更改过几版也不知是哪里出现错误了,状态里面点进去可以看到这些 不知道如何找到网站的测试案例
错误信息
/1173/test4.in:[ERROR] solution_id:3596478 called a Forbidden system call:24 [24]
TO FIX THIS , ask admin to add the CALLID into corresponding LANG_XXV[] located at okcalls32/64.h ,
and recompile judge_client.
/1173/test4.in:[ERROR] solution_id:3596478 called a Forbidden system call:24 [24]
TO FIX THIS , ask admin to add the CALLID into corresponding LANG_XXV[] located at okcalls32/64.h ,
and recompile judge_client.
/1173/test6.in:[ERROR] solution_id:3596478 called a Forbidden system call:24 [24]
TO FIX THIS , ask admin to add the CALLID into corresponding LANG_XXV[] located at okcalls32/64.h ,
and recompile judge_client.
/1173/test6.in:[ERROR] solution_id:3596478 called a Forbidden system call:24 [24]
TO FIX THIS , ask admin to add the CALLID into corresponding LANG_XXV[] located at okcalls32/64.h ,
and recompile judge_client.
----time_space_table:----
/1173/sample.in:AC mem=1760k time=9ms
/1173/test1.in:AC mem=1760k time=10ms
/1173/test2.in:AC mem=1800k time=11ms
/1173/test3.in:AC mem=1800k time=11ms
/1173/test4.in:RE mem=1800k time=9ms
/1173/test5.in:WA mem=1800k time=11ms
/1173/test6.in:RE mem=1800k time=7ms
/1173/test7.in:AC mem=1800k time=9ms
/1173/test8.in:WA mem=1812k time=11ms
/1173/test9.in:WA mem=1900k time=11ms
package main
import (
"bufio"
"os"
"strconv"
"strings"
)
func main() {
scanner := bufio.NewScanner(os.Stdin)
scanner.Scan()
lineList := strings.Fields(scanner.Text())
N, _ := strconv.Atoi(lineList[0])
M, _ := strconv.Atoi(lineList[1])
grid := make([][]int, N)
visited := make([][]bool, N)
for i := 0; i < N; i++ {
grid[i] = make([]int, M)
visited[i] = make([]bool, M)
scanner.Scan()
lineList = strings.Fields(scanner.Text())
for j := 0; j < M; j++ {
grid[i][j], _ = strconv.Atoi(lineList[j])
}
}
isEdge := func(x, y int) bool {
return x == 0 || x == N-1 || y == 0 || y == M-1
}
dir := [][]int{{0, 1}, {0, -1}, {1, 0}, {-1, 0}}
var bfs func(x int, y int) (int, bool)
bfs = func(x int, y int) (area int, isIsland bool) {
queue := make([][]int, 0)
queue = append(queue, []int{x, y})
isIsland = true
if isEdge(x, y) {
isIsland = false
}
visited[x][y] = true
area = 1
for len(queue) > 0 {
curr := queue[0]
queue = queue[1:]
for _, dir_ := range dir {
nextX := curr[0] + dir_[0]
nextY := curr[1] + dir_[1]
if nextX < 0 || nextX >= N || nextY < 0 || nextY >= M || visited[nextX][nextY] || grid[nextX][nextY] == 0 {
continue
}
if isIsland && isEdge(nextX, nextY) {
isIsland = false
}
visited[nextX][nextY] = true
area++
queue = append(queue, []int{nextX, nextY})
}
}
return area, isIsland
}
maxArea := 0
for i := 0; i < len(grid); i++ {
for j := 0; j < len(grid[0]); j++ {
if !visited[i][j] && grid[i][j] == 1 {
area, isIsland := bfs(i, j)
if isIsland && area > maxArea {
maxArea = area
}
}
}
}
println(maxArea)
}
这应该是【找出最大的封闭岛屿的面积】这一题,封闭岛屿指的是不接触边界的岛屿。我们使用BFS遍历每个岛屿,并检查是否接触边界,从而判断是否为封闭岛屿,并记录最大面积。
主要问题是在输入处理时未检查每行的元素数量是否足够,导致数组越界,从而引发运行时错误。因此,修改输入处理部分,添加必要的检查,可以解决这个问题。
要解决这个问题,我们需要确保输入处理部分正确读取数据,避免数组越界,并正确判断封闭岛屿的最大面积
package main
import (
"bufio"
"fmt"
"os"
"strconv"
"strings"
)
func main() {
scanner := bufio.NewScanner(os.Stdin)
if !scanner.Scan() {
fmt.Println("0")
return
}
firstLine := strings.Fields(scanner.Text())
if len(firstLine) < 2 {
fmt.Println("0")
return
}
N, _ := strconv.Atoi(firstLine[0])
M, _ := strconv.Atoi(firstLine[1])
grid := make([][]int, N)
visited := make([][]bool, N)
for i := 0; i < N; i++ {
grid[i] = make([]int, M)
visited[i] = make([]bool, M)
if !scanner.Scan() {
fmt.Println("0")
return
}
line := scanner.Text()
lineList := strings.Fields(line)
if len(lineList) < M {
fmt.Println("0")
return
}
for j := 0; j < M; j++ {
num, _ := strconv.Atoi(lineList[j])
grid[i][j] = num
}
}
isEdge := func(x, y int) bool {
return x == 0 || x == N-1 || y == 0 || y == M-1
}
dirs := [][]int{{0, 1}, {0, -1}, {1, 0}, {-1, 0}}
var bfs func(x, y int) (int, bool)
bfs = func(x, y int) (int, bool) {
queue := [][]int{{x, y}}
visited[x][y] = true
area := 1
isIsland := !isEdge(x, y)
for len(queue) > 0 {
curr := queue[0]
queue = queue[1:]
for _, d := range dirs {
nx, ny := curr[0]+d[0], curr[1]+d[1]
if nx < 0 || nx >= N || ny < 0 || ny >= M || visited[nx][ny] || grid[nx][ny] == 0 {
continue
}
visited[nx][ny] = true
area++
if isEdge(nx, ny) {
isIsland = false
}
queue = append(queue, []int{nx, ny})
}
}
return area, isIsland
}
maxArea := 0
for i := 0; i < N; i++ {
for j := 0; j < M; j++ {
if !visited[i][j] && grid[i][j] == 1 {
area, isIsland := bfs(i, j)
if isIsland && area > maxArea {
maxArea = area
}
}
}
}
fmt.Println(maxArea)
}
@yuuban007 这个问题解决了吗?我也遇到了。不知道哪里的问题。