c - 路线定位在像二维数组这样的盒子中

标签 c algorithm

我的代码:

#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/

相关文章:

c - 在 cygwin 中与 gcc 动态链接时 undefined reference

c - 编译 Lua 时未解析的外部符号 _LoadLibraryExA

algorithm - 如何找到数组中的峰值?

algorithm - 返回数组中最多的元素个数 "expensive"

c++ - 使用网格中的顶点(2D 和 3D)查找边的算法

algorithm - 布隆过滤器实现如何保持清洁?

algorithm - 如何调整K-means聚类?

c - C中数组指针的奇怪行为

c - 当添加小数字时,程序给出了巨大的数字

c - c 中的二维数组排序