编辑代码

#include<stdio.h>
#include<string.h>

int main() 
{ 
    int h, w;
    scanf("%d %d", &h, &w);
    int map[h][w];
    int position_x, position_y;
    memset(map,0,sizeof(map));

    scanf("%d %d", &position_x, &position_y);

    int obstacle_num, i, j;
    int obstacle_x, obstacle_y;
    scanf("%d", &obstacle_num);

    for(i = 0; i < obstacle_num; i++) {
        scanf("%d %d", &obstacle_x, &obstacle_y);
        map[obstacle_x - 1][obstacle_y - 1] = 1;
    }

    for(i = 0; i < h; i++) {
        for(j = 0; j < w; j++)
            printf("%d ", map[i][j]);
        printf("\n");
    }

    char move;
    while(scanf("%c", &move) != EOF) {
        if (move == 'U') {
            while(map[position_x - 1][position_y] == 0 && position_x > 0)
                position_x--;
        }
        else if(move == 'D') {
            while(map[position_x + 1][position_y] == 0 && position_x < h)
                position_x++;
        }
        else if(move == 'L') {
            while(map[position_x][position_y - 1] == 0 && position_y > 0)
                position_y--;
        }
        else if(move == 'R') {
            while(map[position_x][position_y + 1] == 0 && position_y < w)
                position_y++;
        }
    }

    printf("%d %d", position_x + 1, position_y + 1);

    return 0; 
}