我的代码可以通过3个用例,但我不知道为什么无法通过其他用例。

huangapple go评论66阅读模式
英文:

My code can pass through 3 usage but I don't know why I can't pass the other

问题

问题是:
小码最近一直在玩冰雪冒险游戏,这是一个n行m列的网格游戏。有一些不可通过的障碍物在一些网格上,其余的网格是光滑的冰面。

玩家最初位于其中一个非障碍物网格上,并保持静止不动。然后他将跟随角色向上、向下、向左、向右移动;当他按住角色朝一个方向移动时,角色将在冰面上滑动。在这里,我们假设直到角色移动的下一个网格是障碍物或超出地图边界之前,角色都会保持滑动。然后,角色将继续等待小码兄弟的下一个动作。

现在给出地图上所有障碍物的位置、小码的初始位置和小码的动作序列,请找出小码最后停留的位置。

以下是我的代码:

package main

import "fmt"

func yi(x, y int, s byte, a [][]int) (x1, y1 int) {
	switch s {

	case 'L':
		for {
			if y == 0 || a[x][y-1] == 1 {
				return x, y
			}
			y--

		}
	case 'R':
		for {
			if y == 4 || a[x][y+1] == 1 {
				return x, y
			}
			y++

		}
	case 'U':
		for {
			if x == 0 || a[x-1][y] == 1 {
				return x, y
			}
			x--

		}
	case 'D':
		for {
			if x == 4 || a[x+1][y] == 1 {
				return x, y
			}
			x++

		}

	}
	return x1, y1
}

func main() {

	var x, y, xc, yc, count int
	fmt.Scanf("%d %d", &x, &y)
	fmt.Scanf("%d %d", &xc, &yc)
	fmt.Scanf("%d", &count)

	arr := make([][]int, x)
	for i := 0; i < x; i++ {
		arr[i] = make([]int, y)
	}

	for j := 0; j < count; j++ {
		var xt, yt int
		fmt.Scanf("%d %d", &xt, &yt)
		arr[xt-1][yt-1] = 1
	}

	var kong string
	fmt.Scanf("%s", &kong)
	xf, yf := xc-1, yc-1

	for i := 0; i < 8; i++ {
		xf, yf = yi(xf, yf, kong[i], arr)
	}

	fmt.Println(xf+1, yf+1)

}

我试图弄清楚问题所在,但我需要找出问题出在哪里。你能帮我找出问题所在吗(我不了解教授使用的方法)?

英文:

The question is:
Little Code has recently been playing Ice Adventure, an n-line m-column grid game. There are some impassable obstacles on some of the grids, and the rest of the grids are smooth ice.

The player is in it,ally on one of the non-obstacle grids, and remains stationary. Then he will following the character to move up and down, left and right; when he holds the characters move in one direction, the character will be on the ice surface to slide. The nature have for you here top moving until the next grid in the direction the character is moving is an obstacle or beyond the map boundary, then the surface will continue to wait for the next action of the little yard brother.

Now given the positions of all the obstacles in the map, the initial position of Little Yard and the sequence of Little Yard's actions, please find the position where Little Yard finally stays.

And here is my code

package main
import &quot;fmt&quot;
func yi(x,y int, s byte, a [][]int) (x1,y1 int){
switch(s){
case &#39;L&#39;:
for{
if y==0 || a[x][y-1]==1{
return x,y
}
y--
}
case &#39;R&#39;:
for{
if y==4 || a[x][y+1]==1{
return x,y
}
y++
}
case &#39;U&#39;:
for{
if x==0 || a[x-1][y]==1{
return x,y
}
x--
}
case &#39;D&#39;:
for{
if x==4 || a[x+1][y]==1{
return x,y
}
x++
}
}
return x1,y1
}
func main() {
var x,y,xc,yc,count int
fmt.Scanf(&quot;%d %d&quot;,&amp;x,&amp;y)
fmt.Scanf(&quot;%d %d&quot;,&amp;xc,&amp;yc)
fmt.Scanf(&quot;%d&quot;,&amp;count)
// s:= make([]int,1,1)
i:=count
// }
arr := make([][]int, x)
for i := 0; i &lt; x; i++ {
arr[i] = make([]int, y)
}
// var arr [6][6]int
for j:=0;j&lt;i;j++{   
var xt,yt int
fmt.Scanf(&quot;%d %d&quot;,&amp;xt,&amp;yt)
arr [xt-1][yt-1]=1
}
// for i:=0;i&lt;x;i++{
//     for j:=0;j&lt;y;j++{
//         fmt.Print(arr[i][j],&quot; &quot;)
//     }
//     fmt.Println()
// }
var kong string
fmt.Scanf(&quot;%s&quot;,&amp;kong)
xf,yf:=xc-1,yc-1
// xf,yf=yi(xf,yf,&#39;R&#39;,arr)
// fmt.Println(string(kong[7]))
for i:=0; i&lt;8; i++{
xf,yf=yi(xf,yf,kong[i],arr)
// fmt.Println(xf,yf)
// func yi(x,y int, i int, s string, a *[5][5]int) (x1,y1 int){
} 
fmt.Println(xf+1,yf+1)
}

I try to figure it out, but I need to find out where the problem is. Can you help me to figure it out(I don't know about the usage the professor used)

答案1

得分: 2

如果我理解正确的话,我认为你的代码可能忘记使用变量。在代码中,你写了:

for i:=0; i&lt;8; i++{
        xf,yf=yi(xf,yf,kong[i],arr)
        // fmt.Println(xf,yf)
        // func yi(x,y int, i int, s string, a *[5][5]int) (x1,y1 int){

    } 

我认为8是一个主观的常量,我认为它应该被更改。
这是我的解决方案,希望能帮到你:

package main

import "fmt"

func yi(x, y int, s byte, a [][]int) (x1, y1 int) {
    switch s {

    case 'L':
        for {
            if y == 0 || a[x][y-1] == 1 {
                return x, y
            }
            y--

        }
    case 'R':
        for {
            if y == 4 || a[x][y+1] == 1 {
                return x, y
            }
            y++

        }
    case 'U':
        for {
            if x == 0 || a[x-1][y] == 1 {
                return x, y
            }
            x--

        }
    case 'D':
        for {
            if x == 4 || a[x+1][y] == 1 {
                return x, y
            }
            x++

        }

    }
    return x1, y1
}

func main() {

    var x, y, xc, yc, count int
    fmt.Scanf("%d %d", &x, &y)
    fmt.Scanf("%d %d", &xc, &yc)
    fmt.Scanf("%d", &count)
    // s:= make([]int,1,1)
    i := count

    // }
    arr := make([][]int, x)
    for i := 0; i < x; i++ {
        arr[i] = make([]int, y)
    }
    // var arr [6][6]int
    for j := 0; j < i; j++ {
        var xt, yt int
        fmt.Scanf("%d %d", &xt, &yt)
        arr[xt-1][yt-1] = 1
    }
    // for i:=0;i&lt;x;i++{
    //     for j:=0;j&lt;y;j++{
    //         fmt.Print(arr[i][j],&quot; &quot;)
    //     }
    //     fmt.Println()
    // }

    var kong string
    fmt.Scanf("%s", &kong)
    xf, yf := xc-1, yc-1
    // xf,yf=yi(xf,yf,&#39;R&#39;,arr)
    // fmt.Println(string(kong[7]))
    for i := 0; i < count; i++ {
        xf, yf = yi(xf, yf, kong[i], arr)
        // fmt.Println(xf,yf)
        // func yi(x,y int, i int, s string, a *[5][5]int) (x1,y1 int){

    }
    fmt.Println(xf+1, yf+1)

}
英文:

If I understand you correctly, I think maybe your code forget to use the variety. In the code, you wrote:

for i:=0; i&lt;8; i++{
xf,yf=yi(xf,yf,kong[i],arr)
// fmt.Println(xf,yf)
// func yi(x,y int, i int, s string, a *[5][5]int) (x1,y1 int){
} 

I think 8 is a subjective const and I think it should be changed.
Here is my solution, hope to help you:

package main
import &quot;fmt&quot;
func yi(x,y int, s byte, a [][]int) (x1,y1 int){
switch(s){
case &#39;L&#39;:
for{
if y==0 || a[x][y-1]==1{
return x,y
}
y--
}
case &#39;R&#39;:
for{
if y==4 || a[x][y+1]==1{
return x,y
}
y++
}
case &#39;U&#39;:
for{
if x==0 || a[x-1][y]==1{
return x,y
}
x--
}
case &#39;D&#39;:
for{
if x==4 || a[x+1][y]==1{
return x,y
}
x++
}
}
return x1,y1
}
func main() {
var x,y,xc,yc,count int
fmt.Scanf(&quot;%d %d&quot;,&amp;x,&amp;y)
fmt.Scanf(&quot;%d %d&quot;,&amp;xc,&amp;yc)
fmt.Scanf(&quot;%d&quot;,&amp;count)
// s:= make([]int,1,1)
i:=count
// }
arr := make([][]int, x)
for i := 0; i &lt; x; i++ {
arr[i] = make([]int, y)
}
// var arr [6][6]int
for j:=0;j&lt;i;j++{   
var xt,yt int
fmt.Scanf(&quot;%d %d&quot;,&amp;xt,&amp;yt)
arr [xt-1][yt-1]=1
}
// for i:=0;i&lt;x;i++{
//     for j:=0;j&lt;y;j++{
//         fmt.Print(arr[i][j],&quot; &quot;)
//     }
//     fmt.Println()
// }
var kong string
fmt.Scanf(&quot;%s&quot;,&amp;kong)
xf,yf:=xc-1,yc-1
// xf,yf=yi(xf,yf,&#39;R&#39;,arr)
// fmt.Println(string(kong[7]))
for i:=0; i&lt;count; i++{
xf,yf=yi(xf,yf,kong[i],arr)
// fmt.Println(xf,yf)
// func yi(x,y int, i int, s string, a *[5][5]int) (x1,y1 int){
} 
fmt.Println(xf+1,yf+1)
}

huangapple
  • 本文由 发表于 2023年2月19日 12:37:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/75498034.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定