Untitled
raw download clone
TEXT
views 13
,
size 6255 b
#pragma warning(disable:4996)
#pragma warning(disable:4996)
#include <stdio.h>
#include <string.h>

#define EAST 0
#define SOUTH 1
#define WEST 2
#define NORTH 3

char map[105][105];
char actions[105];
int coin_amount = 0;

// tank's initial direction
char init_dir;
// tank's direction now
int dir_now;
// tank's center x and y
int center_x, center_y; //一開始讀近來會知道

void decide_initial_direction()
{
    /// Decide tank's initial direction
    /// Using init_dir
    /// To determine dir_now
    if (init_dir == 'N') dir_now = NORTH;
    if (init_dir == 'S') dir_now = SOUTH;
    if (init_dir == 'E') dir_now = EAST;
    if (init_dir == 'W') dir_now = WEST;

}

void take_a_step()
{
    if (dir_now == NORTH) {
        /// Detect wall first
        if (map[center_x - 2][center_y] == '#') {
            return;
        }
        /// And then detect hill
        else if (map[center_x - 2][center_y] == '^' || map[center_x - 2][center_y - 1] == '^'\
            || map[center_x - 2][center_y + 1] == '^') {
            return;
        }
        /// If there is no obstacle, take a step
        else {
            center_x -= 1;
        }
    }
    else if (dir_now == SOUTH) {
        /// Detect wall first
        if (map[center_x + 2][center_y] == '#') {
            return;
        }
        /// And then detect hill
        else if (map[center_x + 2][center_y] == '^' || map[center_x + 2][center_y - 1] == '^'\
            || map[center_x + 2][center_y + 1] == '^') {
            return;
        }
        /// If there is no obstacle, take a step
        else {
            center_x += 1;
        }
    }
    else if (dir_now == EAST) {
        /// Detect wall first
        if (map[center_x][center_y + 2] == '#') {
            return;
        }
        /// And then detect hill
        else if (map[center_x][center_y + 2] == '^' || map[center_x - 1][center_y + 2] == '^'\
            || map[center_x + 1][center_y + 2] == '^') {
            return;
        }
        /// If there is no obstacle, take a step
        else {
            center_y += 1;
        }

    }
    else if (dir_now == WEST) {
        /// Detect wall first
        if (map[center_x][center_y - 2] == '#') {
            return;
        }
        else if (map[center_x][center_y - 2] == '^' || map[center_x - 1][center_y - 2] == '^'\
            || map[center_x + 1][center_y - 2] == '^') {
            return;
        }
        /// If there is no obstacle, take a step
        else {
            center_y -= 1;
        }
    }
}

void pick_the_coins()
{
    int j, k;
    for (j = center_x - 1; j <= center_x + 1; j++) {
        for (k = center_y - 1; k <= center_y + 1; k++) {
            /// determine whether there are coins under the tank
            if (map[j][k] == '$') {
                coin_amount += 1;
                map[j][k] = '=';
            }
        }
    }
}

void turn_right()
{
    /// Change direction depending on dir_now
    dir_now = (dir_now + 1) % 4;
}



void turn_left()
{
    /// Change direction depending on dir_now
    dir_now = (dir_now - 1) % 4;
}
int check()
{
    if (dir_now == EAST) {
        center_y += 2;
        for (int j = center_x - 1; j <= center_x + 1; j++) {
            for (int k = center_y ; k <= center_y + 1; k++) {
                if (map[j][k] == '^' || map[j][k] == '#') {
                    center_y -= 2;
                    return 0;
                }
            }
        }
    }
    if (dir_now == SOUTH) {
        center_x += 2;
        for (int j = center_x ; j <= center_x + 1; j++) {
            for (int k = center_y - 1; k <= center_y + 1; k++) {
                if (map[j][k] == '^' || map[j][k] == '#') {

                    center_x -= 2;
                    return 0;
                }
            }
        }
    }
    if (dir_now == WEST) {
        center_y -= 2;
        for (int j = center_x - 1; j <= center_x + 1; j++) {
            for (int k = center_y ; k <= center_y + 1; k++) {
                if (map[j][k] == '^' || map[j][k] == '#') {

                    center_y += 2;
                    return 0;
                }
            }
        }
    }
    if (dir_now == NORTH) {
        center_x -= 2;
        for (int j = center_x ; j <= center_x + 1; j++) {
            for (int k = center_y - 1; k <= center_y + 1; k++) {
                if (map[j][k] == '^' || map[j][k] == '#') {
                    center_x += 2;
                    return 0;
                }
            }
        }
    }
    return 1;
}

int main()
{
    int i, j, k, rows, cols;
    int actions_number;
    int component = 0;

    /// Raed problem's input
    scanf("%d %d %d %c", &rows, &cols, &actions_number, &init_dir);
    while (getchar() != '\n');
    for (i = 0; i < actions_number; i++) {
        scanf("%c", &actions[i]);
    }

    /// Read map
    for (i = 1; i <= rows; i++) {
        while (getchar() != '\n');
        for (j = 1; j <= cols; j++) {
            scanf("%c", &map[i][j]);
            /// Find tank's center x and y
            if (((map[i][j]) == 'x') || ((map[i][j]) == 'o') || ((map[i][j]) == 'O')) {
                component++;
                if (component == 5) {
                    center_x = i;
                    center_y = j;
                }
            }
        }
    }

    decide_initial_direction();

    for (i = 0; i < actions_number; i++) {
        if (actions[i] == 'F') {
            take_a_step();
            pick_the_coins();

        }

        else if (actions[i] == 'J') {

            if (check()) {
                take_a_step();
                pick_the_coins();
                take_a_step();
                pick_the_coins();
            }
        }
        else {
            if (actions[i] == 'R') {

                turn_right();
            }
            if (actions[i] == 'L') {

                turn_left();
            }
        }
        //printf("%d %d %d\n",center_x,center_y,dir_now);
    }

    printf("%d\n", coin_amount);
    //printf("%d %d",center_x,center_y);

    return 0;
}
close fullscreen
Login or Register to edit or fork this paste. It's free.