路线定位在二维数组之类的盒子中

路线定位在二维数组之类的盒子中

我的代码:

#include<stdio.h>
#include<stdlib.h>
int ch,a[100];
static int i=0,j;
int checkIfValidCoordinates(int x, int y, int n, char arr[]){
    if(x==a[i]&& y==a[i+1])
    {
        i+=2;
        return 0;
    }
    // arr for this location is invalid
    // then return 0;
    return 1;
}
void path(int u,int v,int n,char arr[],size_t s,size_t p)
{
    ch=1;
    int x;
    j=checkIfValidCoordinates(u,v,n,arr);
    if(j == 0)
        return;
    if(u==(n-1)&&v==(n-1))
    {
        p+=snprintf(arr+p,s-p,"( %d , %d )",u,v);
    }
    else
    {
         p+=snprintf(arr+p,s-p,"( %d , %d ) - ",u,v);
    }
    if(p>=s)
    {
        fprintf(stderr,"Small path\n");
        exit(1);
    }

    if(u==n-1&&v==n-1)
    {
        printf("%s\n",arr);
    }
    else
    {
    {

        if(u<n-1)
        {
            path(u+1,v,n,arr,s,p);
        }
        if(v<n-1)
        {
             path(u,v+1,n,arr,s,p);
        }
        if(u<n-1&&v<n-1)
        {
             path(u+1,v+1,n,arr,s,p);
        }
    }
    }
}
void main()
{
    char arr[1000];
    int size;
    printf("Enter the size of the grid");
    scanf("%d",&size);
    if(size<=0)
    {
        printf("\nInvalid Input");
        exit(1);
    }
    printf("\nEnter the grid points that are offsets");
    here1:
    scanf("%d %d",&a[i],&a[i+1]);
    if(a[i]==-1&&a[i+1]==-1)
    {
        goto here;
    }
    else
    {
    i+=2;
    goto here1;
    }
    here:
    printf("\nThe paths for the robot are\n");
    i=0;
    path(0,0,size,arr,sizeof arr,0);
}

问题描述:
机器人通过一个网格移动。机器人在三个方向上移动。方向是右下和对角向下。程序是从矩阵给定的左上角的源找到机器人到达目的地的路径。
我期望的是:
我的代码在三个方向上打印机器人的所有路径…如果我阻止矩阵的任何单元,那么路径的变化将如何…以及如何打印路径??
请。。帮我做这个…

最佳答案

这样地

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

typedef struct point {
    int r, c;
} Point;

void search_path(Point p, int n, char (*blocks)[n], Point *path, size_t path_len){
    if(p.r == n || p.c == n || blocks[p.r][p.c])
        return;//invalid point
    path[path_len++] = p;//add point to path
    if(p.r == n-1 && p.c == n-1){//goal! print path
        for(size_t i = 0; i < path_len; ++i){
            if(i)
                putchar('-');
            printf("( %d , %d )", path[i].r, path[i].c);
        }
        puts("");
        return;
    }
    search_path((Point){ p.r +1, p.c    }, n, blocks, path, path_len);//down
    search_path((Point){ p.r   , p.c +1 }, n, blocks, path, path_len);//right
    search_path((Point){ p.r +1, p.c +1 }, n, blocks, path, path_len);//diagonally down
}

int main(void){
    int size = 0;

    printf("Enter the size of the grid\n");
    scanf("%d", &size);
    if(size <= 0){
        printf("\nInvalid Input\n");
        exit(1);
    }
    Point *path = malloc((size * 2 - 1) * sizeof(*path));//check omitted
    char (*blocks)[size] = calloc(size, sizeof(*blocks));

    printf("\nEnter the grid points that are offsets\n");
    Point offset;
    while(scanf("%d %d", &offset.r, &offset.c)==2){
        if(offset.r == -1 && offset.c == -1)
            break;
        blocks[offset.r][offset.c] = 1;
    }

    printf("\nThe paths for the robot are\n");
    search_path((Point){0, 0}, size, blocks, path, 0);

    free(blocks);
    free(path);
}

关于c - 路线定位在二维数组之类的盒子中,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/45522468/

10-10 11:24