连接迷宫/网格的墙壁,以便所有这些都相互连接 [英] Connecting a maze/grid's walls so all are interconnected

查看:21
本文介绍了连接迷宫/网格的墙壁,以便所有这些都相互连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 2d 网格,我正在尝试在所有墙壁之间创建链接.

I have a 2d grid that I'm trying to create links between all walls.

网格的构造如下:

    grid = new State[8][8];
    for (int i = 0; i < 8; i++) {
        for (int j = 0; j < 8; j++) {
            grid[i][j] = State.blank;
        }
    }

我有一个机器人,它应该能够像玩蛇游戏一样穿过墙壁到对面.

I have a robot that should be able to pass through the walls to the opposite side like on a game of snake.

例如,如果机器人面向北并且在位置 x[0]y[1],那么它应该连接到 x[7]y[1].

So for example if the robot is facing NORTH and is in position x[0]y[1] then it should connect to x[7]y[1].

机器人还应该能够读取它前面三个方块中的内容,一个在左边,一个在右边,一个在正前方.

The robot should also be able to read whats in the three blocks in front of it, one to the left, one to the right and one directly infront.

# x = empty space
# R = robot
# S = spaces robots sensors pick up

如果它朝北,这就是机器人会捡到的:

If it were facing north this is what the robot would pick up:

[S][S][S][x][x][x][x][x]
[x][R][x][x][x][x][x][x]
[x][x][x][x][x][x][x][x]
[x][x][x][x][x][x][x][x]
[x][x][x][x][x][x][x][x]
[x][x][x][x][x][x][x][x]
[x][x][x][x][x][x][x][x]
[x][x][x][x][x][x][x][x]

同样,如果机器人面向东,这就是它会捡到的:

Like wise if the robot was facing east this is what it would pick up:

[x][x][S][x][x][x][x][x]
[x][R][S][x][x][x][x][x]
[x][x][S][x][x][x][x][x]
[x][x][x][x][x][x][x][x]
[x][x][x][x][x][x][x][x]
[x][x][x][x][x][x][x][x]
[x][x][x][x][x][x][x][x]
[x][x][x][x][x][x][x][x]

我遇到的问题是找到正确的算法,以确保机器人不仅可以穿过墙壁,还可以通过墙壁读取传感器.

The problem I'm having is finding the right algorithm to make sure that the robot can not only pass through walls but also read sensors through the walls.

如果机器人在左上角并面向北方,那么它会像这样读取墙壁:

If the robot was in the top left corner and facing NORTH then it would read through the wall like so:

[R][x][x][x][x][x][x][x]
[x][x][x][x][x][x][x][x]
[x][x][x][x][x][x][x][x]
[x][x][x][x][x][x][x][x]
[x][x][x][x][x][x][x][x]
[x][x][x][x][x][x][x][x]
[x][x][x][x][x][x][x][x]
[S][S][x][x][x][x][x][S]

正如你所想象的,我已经尝试过做大量的 IF 语句,但是有太多的可能性来覆盖它们而不会发疯!

As you can imagine I've already tried doing length chunks of IF statements but there is too many possiblities to cover them all without going insane!

我还写下了对 X & 的更改在某些情况下,在纸上是 Y,但我真的看不到任何暗示算法的模式.

I've also wrote down the changes to X & Y on paper when placed in certain circumstances but I can't really see any pattern that hints toward an algorithm.

任何帮助将不胜感激!

推荐答案

public class Robot {
    public int x;
    public int y;
    public Robot(int x,int y) {
        this.x = x;
        this.y = y;
    }
    public void move(int direction, int steps) {
        switch(direction) {
            case 1: //north
                int temp1 = (x-steps)%8;
                x = temp1<0?(temp1+8):temp1;
                break;
            case 2: //south
                x = (x+steps)%8;
                break;
            case 3: //west
                int temp3 = (y-steps)%8;
                y = temp3<0?(temp3+8):temp3;
                break;
            case 4: //east
                y = (y+steps)%8;
                break;
            default:
                System.out.println("I'm not smart enough to handle the direciton provided!");
        }
    }

    public static void main(String[] args) {
        int[][] grid = new int[8][8];
        Robot robot = new Robot(0,0);
        System.out.println("I'm starting at (0,0).");
        robot.move(3, 9);
        System.out.println("I'm moving west by 9 steps.");
        System.out.println("I've arrived at ("+robot.x+","+robot.y+").");
    }
}

希望上面的代码给出了一个想法.我已经测试过了.随意尝试.机器人前面三个方块的计算类似.你可以自己解决.

Hope the code above gives an idea. I've tested it. Feel free to have a try. The computation of the three blocks in front of the robot is similar. You can figure it out on your own.

这篇关于连接迷宫/网格的墙壁,以便所有这些都相互连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆