Ejemplo n.º 1
0
int
main(int argc, char* argv[]) {
    maze_t m;
    
    read_maze(&m);
    print_stage1(&m);
    assign_reachability(&m);
    print_stage2(&m);
    
    return 0;
}
Ejemplo n.º 2
0
Archivo: main.c Proyecto: aijkoopmans/C
int main (int argc, char **argv) {
    int solver, direction = NORTH, steps = 0;
    maze_t* maze;   
    walker_t* walker;

    if (argc < 2){
        printf("Error: filename should be an argument");
        return 0;
    }
    maze = read_maze(argv[1]);
    walker = init_walker(maze);
    printf("[0] Random solver\n[1] Left-hand solver\n[2] Breadcrum solver\n"
    "Choice: ");
    scanf("%d", &solver);
    
    while ((*maze).solved != 1){
        switch(solver){
            case 0:
                direction =  random_solver(maze, walker, direction);
                break;
            case 1:
                direction =  left_hand_solver(maze, walker, direction);
                break;
            case 2:
                direction =  breadcrum_solver(maze, walker, direction);
                break;
            default:
                printf("Error: that's not a choice\n");
                return 0;
        }
        steps++;
        print_maze(maze);
        if (steps >= MAX_STEPS)
            break;
    }

    if ((*maze).solved == 1)
        printf("Found exit in %d steps\n", steps);
    else
        printf("\nCould not find exit after %d steps\n", steps);

    return 0;
}
Ejemplo n.º 3
0
Archivo: main.c Proyecto: JelteF/C
/*
 * In this function the input gets checked and the other functions are called
 * in the correct order.
 */
int main (int argc, char **argv) {
    maze_t* maze;
    char *solver[] = {"random solver", "smart random solver", "left wall solver",
                      "right wall solver"
                     };
    walker_t* walker;
    int i, count, dir, printmaze;
    if (argc < 2) {
        printf("The filename of the maze should be passed as an argument\n");
        return 0;
    }

    if (argc > 2) {
        i = atoi(argv[2]);
        if (i < 0) {
            printf("Don't use negative numbers");
            return 0;
        }
    }
    else
        i = 0;

    if (argc > 3) {
        printmaze = atoi(argv[3]);
    }
    else
        printmaze = 1;

    for(; i < AMNT_SOLVERS; i++) {
        srand(SEED);
        dir = NORTH;
        maze = read_maze(argv[1]);
        walker = init_walker(maze);
        count = 0;
        while (count < MAX_STEPS) {
            count++;
            switch(i) {
            case 0:
                dir = random_solver(maze, walker);
                break;
            case 1:
                dir = smart_random_solver_1(maze, walker, dir);
                break;
            case 2:
                dir = left_wall_solver(maze, walker, dir);
                break;
            case 3:
                dir = right_wall_solver(maze, walker, dir);
                break;
            }
            if(printmaze) {
                print_maze(maze, walker->row, walker->col);
                printf("%d\n", count);
            }

            if (at_exit(maze, walker))
                break;
        }
        if (count < MAX_STEPS)
            printf("Found exit using the %s after %d steps\n", solver[i], count);
        printf("Press key to continue\n");
        getchar();
    }
    return 0;

}
Ejemplo n.º 4
0
int main(void)
{
    int n, m;
    char **maze = read_maze(&n, &m);

    // initialization
    fringe *f = fringe_init();
    fringe_enqueue(f, make_fringe_item(make_state(0, 0), NULL));
    int **closed_set = init_closed_set(n, m);

    // set up search helper-variables
    fringe_item *current_item;
    state *current_state;
    action *current_strategy;
    state *next_state;
    int num_actions, i;
    int *available_actions;
    int strategy_found = 0;
    action *strategy;

    // perform breadth first search
    while (!is_empty(f)) {
        current_item = fringe_dequeue(f);
        current_state = current_item->state;
        current_strategy = current_item->strategy;

        // goal state-check
        if (is_goal_state(current_state, n, m)) {
            strategy = current_strategy;
            strategy_found = 1;
            break;
        }

        // only proceed if the node has not been visited
        if (is_in_closed_set(current_state, closed_set)) {
            // free item?
            continue;
        }

        // put the current node into the closed set
        insert_into_closed_set(current_state, closed_set);


        // expand current node
        available_actions = get_actions(current_state, maze, &num_actions);

        for (i = 0; i < num_actions; i++) {
            next_state = apply(current_state, available_actions[i]);

            // add this state/action-pair to the fringe
            action *new_action = make_action(available_actions[i], current_strategy);
            fringe_enqueue(f, make_fringe_item(next_state, new_action));
        }

        free(current_item);
    }


    if (!strategy_found) {
        // don't do anything
    }
    else {
        display_and_free(reverse(strategy), make_state(0, 0));
        printf("[%d, %d]\n", n - 1, m - 1);
    }

    // free maze memory
    return 0;
}
int main()
{
    /*freopen("816.in", "r", stdin);
    freopen("my.out", "w", stdout);*/
    char name[30];
    char tempC;

    scanf("%s", name);
    while(strcmp(name, "END")!=0)
    {
        initialize_maze();
        Qhead = Qtail = 0;

        int start_row, start_col, start_direction;
        int end_row, end_col;
        scanf("%d %d %c", &start_row, &start_col, &tempC);

        int start_row2 = start_row, start_col2 = start_col, start_direction2;
        /* translate input (starting destination) into my meaning of node in que(starting source) */
        if(tempC=='E') {
            start_col2++;
            start_direction2 = WEST;
        } else if(tempC=='S') {
            start_row2++;
            start_direction2 = NORTH;
        } else if(tempC=='W') {
            start_col2--;
            start_direction2 = EAST;
        } else if(tempC=='N') {
            start_row2--;
            start_direction2 = SOUTH;
        } else fprintf(stderr, "no such start direction : %c\n", tempC);
        pushQ(NULL, &maze[start_row2][start_col2][start_direction2]);

        scanf("%d %d", &end_row, &end_col);
        read_maze();
        /* print_maze(); */

        BFS(end_row, end_col);
        /* if maze[end_row][end_col][0~3].visited are all 0 */
        int i;
        Node *current = NULL;
        for(i=0 ; i<4 ; i++){
            if( maze[end_row][end_col][i].visited ){
                current = &maze[end_row][end_col][i];
            }
        }

        printf("%s\n", name);
        if(current==NULL){
            printf("  No Solution Possible");
        }
        else
        {
            Node * Nstack[500];
            int NstackSize = 0;
            Nstack[NstackSize++] = current;
            while(current->parent != NULL){
                current = current->parent;
                Nstack[NstackSize++] = current;
            }
            int count = 1;
            i = NstackSize-1;
            printf("  (%d,%d)", start_row, start_col);
            if(i >= 0) putchar(' ');
            while(i >= 0)
            {
                count++;
                printf("(%d,%d)", Nstack[i]->row, Nstack[i]->col);
                if(i!=0){
                    if(count%10 == 0) printf("\n  ");
                    else putchar(' ');
                }
                i--;
            }
        }
        putchar('\n');

        scanf("%s", name);
    }

    return 0;
}
Ejemplo n.º 6
0
int main()
{
    freopen("maze.txt", "r", stdin);
    freopen("result.txt", "w", stdout);

    // get input from file
    read_maze();

    // print out the maze
    const char conversion_to_maze[3] = {'X', '.', 'o'};
    for (int i = 0; i < 2; i++) {
        for (int j = 0; j < MAZE_ROW; j++) {
            for (int k = 0; k < MAZE_COL; k++) {
                printf("%c", conversion_to_maze[maze[i][j][k]]);
            }
            printf("\n");
        }
    }

    // initialize the stacks and map
    State ratA = {0, 1, 1, 0};
    State ratB = {1, 99, 99, 0};

    posA = posB = 0;
    stackA[posA++] = ratA;
    stackB[posB++] = ratB;

    bool visitedA[2][MAZE_ROW][MAZE_COL], visitedB[2][MAZE_ROW][MAZE_COL];
    memset(visitedA, 0, sizeof(visitedA));
    memset(visitedB, 0, sizeof(visitedB));

    // if one of them reached the dest. or they meet, terminate
    bool need_continue = true;
    while (need_continue) {
        bool reach_A_dest = false, reach_B_dest = false;

        State curr;
        // walk mouse A

        // printf("Walk Mouse A\n");
        while (posA != 0) {
            curr = stackA[posA - 1];
            visitedA[curr.f][curr.x][curr.y] = true;
            // printf("curr -> %d %d %d %d\n", curr.f, curr.x, curr.y, curr.next_dir);

            bool has_next_step = false;
            for (int i = curr.next_dir; i < 4; i++) {
                // printf("Trying %d dirA\n", i);
                State tmp = curr;
                tmp.x = curr.x + dirA[i][0];
                tmp.y = curr.y + dirA[i][1];
                tmp.next_dir = 0;
                // printf("tmp -> %d %d %d %d\n", tmp.f, tmp.x, tmp.y, tmp.next_dir);
                if (is_bounded(tmp) && maze[tmp.f][tmp.x][tmp.y] != WALL &&
                    visitedA[tmp.f][tmp.x][tmp.y] == false) {
                    // printf("posA %d\n", posA);
                    if (maze[tmp.f][tmp.x][tmp.y] == STAIR && tmp.f == 0) {
                        // RatA can only go up once!
                        tmp.f = 1;
                    } else {
                        stackA[posA - 1].next_dir = i + 1;
                    }

                    stackA[posA++] = tmp;
                    has_next_step = true;
                    break;
                }
            }

            if (is_A_dest(stackA[posA - 1])) {
                reach_A_dest = true;
                need_continue = false;
                break;
            }

            if (has_next_step == false) {
                posA--;
                break;
            } else {
                break;
            }
        }

        // walk mouse B (walk one mouse first)

        // printf("Walk Mouse B\n");
        while (posB != 0) {
            curr = stackB[posB - 1];
            visitedB[curr.f][curr.x][curr.y] = true;
            // printf("curr -> %d %d %d %d\n", curr.f, curr.x, curr.y, curr.next_dir);

            bool has_next_step = false;
            for (int i = curr.next_dir; i < 4; i++) {
                // printf("Trying %d dirB\n", i);
                State tmp = curr;
                tmp.x = curr.x + dirB[i][0];
                tmp.y = curr.y + dirB[i][1];
                tmp.next_dir = 0;
                // printf("tmp -> %d %d %d %d\n", tmp.f, tmp.x, tmp.y, tmp.next_dir);
                if (is_bounded(tmp) && maze[tmp.f][tmp.x][tmp.y] != WALL &&
                    visitedB[tmp.f][tmp.x][tmp.y] == false) {
                    // printf("posB %d\n", posB);
                    if (maze[tmp.f][tmp.x][tmp.y] == STAIR && tmp.f == 1) {
                        // RatB can only go down once!
                        tmp.f = 0;
                    } else {
                        stackB[posB - 1].next_dir = i + 1;
                    }

                    stackB[posB++] = tmp;
                    has_next_step = true;
                    break;
                }
            }

            if (is_B_dest(stackB[posB - 1])) {
                reach_B_dest = true;
                need_continue = false;
                break;
            }

            if (has_next_step == false) {
                posB--;
                break;
            } else {
                break;
            }
        }

        if (reach_A_dest || reach_B_dest) {
            // rat(s) reached dest.
            // According to the ans., when the rat reaches the dest., that coor.
            // doesn't need to be printed out!

            if (reach_A_dest && reach_B_dest) {
                // reach dest. at the same time, no coor. to be printed
                printf("rats didn't encounter each other in this maze\n");
            } else if (reach_A_dest) {
                // A reaches the dest. only (ans1), print msg. and terminate the program
                // now!
                printf("rats didn't encounter each other in this maze\n");
            } else {
                // B reaches the dest. only, so print A's coor and the msg., then
                // terminate the program
                printf("ratA(%d,%d,%d)\n", stackA[posA - 1].f, stackA[posA - 1].x,
                       stackA[posA - 1].y);
                printf("rats didn't encounter each other in this maze\n");
            }
        } else if (is_same_location(stackA[posA - 1], stackB[posB - 1])) {
#if DEBUG <= 2
            printf("meet!\n");
            printf("ratA(%d,%d,%d)\n", stackA[posA - 1].f, stackA[posA - 1].x,
                   stackA[posA - 1].y);
            printf("ratB(%d,%d,%d)\n", stackB[posB - 1].f, stackB[posB - 1].x,
                   stackB[posB - 1].y);
#endif
            // print out A's coor and then print the msg, then terminate the program
            printf("ratA(%d,%d,%d)\n", stackA[posA - 1].f, stackA[posA - 1].x,
                   stackA[posA - 1].y);
            // Bug fixed, thanks to Bemg
            printf("rats encounter each other in (%d,%d,%d)\n", stackB[posB - 1].f,
                   stackB[posB - 1].x, stackB[posB - 1].y);
            need_continue = false;
        } else {
            printf("ratA(%d,%d,%d)\n", stackA[posA - 1].f, stackA[posA - 1].x,
                   stackA[posA - 1].y);
            printf("ratB(%d,%d,%d)\n", stackB[posB - 1].f, stackB[posB - 1].x,
                   stackB[posB - 1].y);
        }
    }

    return 0;
}