English 中文(简体)
右手马maz十字路口
原标题:Right hand maze traversal in C
  • 时间:2012-04-12 21:56:19
  •  标签:
  • c

这是我的第一个问题,是,这是“家务”任务。 我一直在工作数小时,但不能用算法工作。 第一期书面节目本应包含一项功能,接受12个阵列,并找到了结束的道路。 主要职能是马奇的开始,将其改为“x”,以代表“角色”的地位。 然后,主要职能指马齐蓬特功能,该功能在清理屏幕后接受阵列打印。 然后,它称为接受阵列的马齐-特拉佛功能。 麻木动物运动的第一部分试图确定“x”的位置,代之以“......”。 下一个部分应确定“x”所面临的方向。 我使用了4、5、6和8(西部、南部、东部和北部(在座标上角)来代表它所面临的方式。 根据它所面临的方式,马齐德河试图确定是否存在一条通往权利的开放道路,然后是前线,然后是左边,然后将X置于这一位置,并改变X正面临的方式。 在我管理该方案的第二次迁移之后,一些事情是错误的。 感谢任何帮助,如果不是这些问题的发生地,则感到担忧。

#include <stdio.h>
#include <stdlib.h>

void mazePrint(char *maze[12][12]);
void mazeTraversal(char *maze[12][12]);
static int face = 6;

main()
{
    int i = 0;
    int j = 0; 
    int k = 0;
    int start;
    int xpos;

    char *mazeone[12][12] = {
       //0///1///2///3///4///5///6///7///8///9///10//11///
        "#","#","#","#","#","#","#","#","#","#","#","#",//0
        "#",".",".",".","#",".",".",".",".",".",".","#",//1
        ".",".","#",".","#",".","#","#","#","#",".","#",//2
        "#","#","#",".","#",".",".",".",".","#",".","#",//3
        "#",".",".",".",".","#","#","#",".","#",".",".",//4
        "#","#","#","#",".","#",".","#",".","#",".","#",//5
        "#","#",".","#",".","#",".","#",".","#",".","#",//6
        "#","#",".","#",".","#",".","#",".","#",".","#",//7
        "#",".",".",".",".",".",".",".",".","#",".","#",//8
        "#","#","#","#","#","#",".","#","#","#",".","#",//9
        "#",".",".",".",".",".",".","#",".",".",".","#",//10
        "#","#","#","#","#","#","#","#","#","#","#","#",};//11

    for (i = 0; i <12; i++)
        if (mazeone[i][0] == "." ) {
            start = i; 
            mazeone[start][0] = "x";
            xpos = start;
            break;
        }

    printf("X is the starting point.
");
    printf("Press Space Bar to watch the X move.


");
    getchar();
    mazePrint(mazeone);
    getchar();
    return 0;
}

void mazePrint(char *maze[12][12])
{   
    int x = 0; 
    int y = 0;

    system("cls");
    for (x = 0; x < 12; x++) {
        for (y = 0; y < 12; y++) {
            printf("%s", maze[x][y]);
        }
        printf("
"); 
    }
    getchar(); 
    mazeTraversal(maze);
}

void mazeTraversal(char *maze[12][12])
{
    int x = 0; 
    int y = 0;

    for (x = 0; x < 12; x++) {
        for (y = 0; y < 12; y++) {
            if (maze[y][x] == "x")
                break;
        } 
        if(maze[y][x] == "x")
            break;
    }

    for (y = 0; y < 12; y++) {
        for (x = 0; x < 12; x++) {
            if (maze[y][x] == "x")
                break;
        } 
        if (maze[y][x] == "x")
            break;
    }

    maze[y][x] = ".";

    switch (face) {
        case 6:{
            if (maze[y][x-1] == ".") {
                maze[y][x-1] = "x"; 
                face = 5;
            } else if (maze[y + 1][x] == ".") {
                maze[y + 1][x] = "x"; 
                face = 6;
            } else if (maze[y][x+1] == ".") {
                maze[y][x+1] = "x";
                face = 8;
            } else if (maze[y - 1][x] == ".") {
                maze[y - 1][x] = "x"; 
                face = 4;
            }
        }
        case 8:{
            if (maze[y + 1][x] == ".") {
                maze[y + 1][x] = "x"; 
                face = 6;
            } else if (maze[y][x+1] == ".") {
                maze[y][x+1] = "x";
                face = 8;
            } else if (maze[y - 1][x] == ".") {
                maze[y - 1][x] = "x"; 
                face = 4;
            } else if (maze[y][x-1] == ".") {
                maze[y][x-1] = "x"; 
                face = 5;
            }
        }
        case 4:{
            if (maze[y][x+1] == ".") {
                maze[y][x+1] = "x";
                face = 8;
            } else if (maze[y - 1][x] == ".") {
                maze[y - 1][x] = "x"; 
                face = 4;
            } else if (maze[y][x-1] == ".") {
                maze[y][x-1] = "x"; 
                face = 5;
            } else if (maze[y + 1][x] == ".") {
                maze[y + 1][x] = "x"; 
                face = 6;
            }
        }
        case 5:{
            if (maze[y - 1][x] == ".") {
                maze[y - 1][x] = "x";
                face = 4;
            } else if (maze[y][x-1] == ".") {
                maze[y][x-1] = "x"; 
                face = 5;
            } else if (maze[y + 1][x] == ".") {
                maze[y + 1][x] = "x"; 
                face = 6;
            } else if (maze[y][x+1] == ".") {
                maze[y][x+1] = "x";
                face = 8;
            }
        }
    }

    mazePrint(maze);
}
最佳回答

页: 1 如果没有<条码>;说明,则您的代码将随附下一个<条码> :,不是你想要的。

问题回答

我认为,你的方向是错误的。 您应从maze/[0]开始,到处6。 您希望提前到maze ***>,但仍然面临6。 但是,如果你看<条码>马齐-特拉弗勒>。 图6

if(maze[y][x+1] == "."){     
   maze[y][x+1] = "x";     
   face = 8;
 }

So you are setting the resulting direction incorrectly.
One thing that might help keep it straight is to use an enumeration instead of random numeric codes:

enum Facing {
   face_EAST,
   face_SOUTH,
   face_WEST, 
   face_NORTH } face = face_EAST;

我甚至可能忘记这个问题的具体方向和方向。 这应有助于使守则的方向直截了当。

enum Facing {
   face_Xplus,
   face_Yplus, 
   face_Xminus, 
   face_Yminus } face = face_Xplus;




相关问题
Fastest method for running a binary search on a file in C?

For example, let s say I want to find a particular word or number in a file. The contents are in sorted order (obviously). Since I want to run a binary search on the file, it seems like a real waste ...

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

Tips for debugging a made-for-linux application on windows?

I m trying to find the source of a bug I have found in an open-source application. I have managed to get a build up and running on my Windows machine, but I m having trouble finding the spot in the ...

Trying to split by two delimiters and it doesn t work - C

I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago and then split each line by ; delimiter and print each record. Then in yet another loop I try to ...

Good, free, easy-to-use C graphics libraries? [closed]

I was wondering if there were any good free graphics libraries for C that are easy to use? It s for plotting 2d and 3d graphs and then saving to a file. It s on a Linux system and there s no gnuplot ...

Encoding, decoding an integer to a char array

Please note that this is not homework and i did search before starting this new thread. I got Store an int in a char array? I was looking for an answer but didn t get any satisfactory answer in the ...

热门标签