Maze Escape

download Maze Escape

of 23

Transcript of Maze Escape

  • 8/11/2019 Maze Escape

    1/23

    #include

    #include

    #include

    #include

    #define MAX_R 100

    #define MAX_C 100

    #define MAX_C 100

    char input_maze[3][3];

    char land[MAX_R][MAX_C];

    char facing[10];

    int bot_x,bot_y;

    char direction_to_move[20];

    void transpose(char facing[])

    {

    //Rotate the Input Maze so that it faces UP

    int i,j;

    char temp[3][3];

    if(strcmp(facing,"UP")==0)

    {

    for(i=0;i

  • 8/11/2019 Maze Escape

    2/23

    }

    else

    if(strcmp(facing,"DOWN")==0)

    {

    for(i=0;i

  • 8/11/2019 Maze Escape

    3/23

  • 8/11/2019 Maze Escape

    4/23

    {

    FILE *f_explore;

    int i,j;

    int line_no=0;

    char str[200];

    if((f_explore=fopen("orion_maze_escape.txt","r"))!=NULL)

    {

    fscanf(f_explore," %d %d %s",&bot_x,&bot_y,&facing);

    while(fscanf(f_explore," %s",&str[0])>0&&line_no

  • 8/11/2019 Maze Escape

    5/23

    strcpy(facing,"UP");

    }

    }

    void write_land_to_file()

    {

    FILE *f_explore;

    FILE *log;

    log=fopen("n_maze_escape.txt","a");

    int i,j;

    if((f_explore=fopen("orion_maze_escape.txt","w"))!=NULL)

    {

    fprintf(f_explore,"%d %d %s\n",bot_x,bot_y,direction_to_move);

    fprintf(log,"%d %d %s\n",bot_x,bot_y,direction_to_move);

    for(i=0;i

  • 8/11/2019 Maze Escape

    6/23

    }

    fclose( f_explore );

    fclose( log );

    }

    }

    void write_land_to_file2()

    {

    FILE *f_explore;

    int i,j;

    if((f_explore=fopen("orion_maze_escape.dat","w"))!=NULL)

    {

    fprintf(f_explore,"%d %d %s\n",bot_x,bot_y,direction_to_move);

    for(i=0;i

  • 8/11/2019 Maze Escape

    7/23

    }

    char next_move[20];

    void get_next_facing()

    {

    if(strcmp(facing,"UP")==0)

    {

    if(strcmp(direction_to_move,"UP")==0)

    strcpy(next_move,"UP");

    else

    if(strcmp(direction_to_move,"DOWN")==0)

    strcpy(next_move,"DOWN");

    else

    if(strcmp(direction_to_move,"LEFT")==0)

    strcpy(next_move,"LEFT");

    else

    if(strcmp(direction_to_move,"RIGHT")==0)

    strcpy(next_move,"RIGHT");

    }

    else

    if(strcmp(facing,"DOWN")==0)

    {

    if(strcmp(direction_to_move,"UP")==0)

    strcpy(next_move,"DOWN");

  • 8/11/2019 Maze Escape

    8/23

    else

    if(strcmp(direction_to_move,"DOWN")==0)

    strcpy(next_move,"UP");

    else

    if(strcmp(direction_to_move,"LEFT")==0)

    strcpy(next_move,"RIGHT");

    else

    if(strcmp(direction_to_move,"RIGHT")==0)

    strcpy(next_move,"LEFT");

    }

    else

    if(strcmp(facing,"LEFT")==0)

    {

    if(strcmp(direction_to_move,"UP")==0)

    strcpy(next_move,"RIGHT");

    else

    if(strcmp(direction_to_move,"DOWN")==0)

    strcpy(next_move,"LEFT");

    else

    if(strcmp(direction_to_move,"LEFT")==0)

    strcpy(next_move,"UP");

    else

    if(strcmp(direction_to_move,"RIGHT")==0)

    strcpy(next_move,"DOWN");

    }

    else

    if(strcmp(facing,"RIGHT")==0)

  • 8/11/2019 Maze Escape

    9/23

    {

    if(strcmp(direction_to_move,"UP")==0)

    strcpy(next_move,"LEFT");

    else

    if(strcmp(direction_to_move,"DOWN")==0)

    strcpy(next_move,"RIGHT");

    else

    if(strcmp(direction_to_move,"LEFT")==0)

    strcpy(next_move,"DOWN");

    else

    if(strcmp(direction_to_move,"RIGHT")==0)

    strcpy(next_move,"UP");

    }

    }

    /**********************************/

    //COMPUTE PATH TO NEXT UNEXPLORED POINT (X) or EXIT (e) using BFS

    /*******/

    //STRUCTURE TO KEEP TRACK OF GOALS = X and e

    struct GOALS

    {

    int x;

    int y;

    }goal[100];

  • 8/11/2019 Maze Escape

    10/23

    int goal_N=0;

    void add_goal(int x,int y)

    {

    goal[goal_N].x=x;

    goal[goal_N].y=y;

    goal_N++;

    }

    //END

    struct QUEUE

    {

    int x;

    int y;

    }stk[MAX_R*MAX_C*10];

    int stk_N=0;

    int stk_beg=0;

    void push(int x,int y)

    {

    stk[stk_N].x=x;

    stk[stk_N].y=y;

    stk_N++;

  • 8/11/2019 Maze Escape

    11/23

    }

    void pop(int *x,int *y)

    {

    *x=stk[stk_beg].x;

    *y=stk[stk_beg].y;

    stk_beg++;

    }

    struct NODE

    {

    int visited;

    int distance;

    int visitedfrom_x;

    int visitedfrom_y;

    }node[MAX_R][MAX_C];

    void init_bfs()

    {

    int i,j;

    for(i=0;i

  • 8/11/2019 Maze Escape

    12/23

    }

    void insert_node(int to_x,int to_y,int from_x,int from_y)

    {

    if(to_x>=0 && to_x=0 && to_y

  • 8/11/2019 Maze Escape

    13/23

    }

    }

    }

    void get_next_goal(int *x,int *y)

    {

    int min_dist=99999;

    int index=0;

    int i;

    int p,q,d;

    for(i=0;i

  • 8/11/2019 Maze Escape

    14/23

    }

    }

    *x=goal[index].x;

    *y=goal[index].y;

    }

    void get_next_goal2(int *x,int *y)

    {

    //ADD HEURISTICS

    //Walk closer to the wall #

    //Walk to the X having maximum number of adjacent #

    int max_wall=0;

    int index=0;

    int i,j,k;

    int p,q,d;

    for(i=0;i

  • 8/11/2019 Maze Escape

    15/23

    }

    if(land[p][q]=='e')

    {

    *x=p;

    *y=q;

    return;

    }

    if(d>max_wall)

    {

    max_wall=d;

    index=i;

    }

    }

    *x=goal[index].x;

    *y=goal[index].y;

    }

    void get_next_goal3(int *x,int *y)

    {

    //HEURISTICS

    //Go to X which is furthest from start point (assuming exit will be places furthestfrom entry)

    int max_dist=0;

    int index=0;

    int i;

    int p,q,d;

    int startx=MAX_R/2;

    int starty=MAX_C/2;

  • 8/11/2019 Maze Escape

    16/23

    for(i=0;imax_dist)

    {

    max_dist=d;

    index=i;

    }

    }

    *x=goal[index].x;

    *y=goal[index].y;

    }

    void next_visit(int goalx,int goaly,int* nextx, int* nexty)

    {

    //BACKTRACK

    int next_x,next_y;

    int curr_x,curr_y;

  • 8/11/2019 Maze Escape

    17/23

    int prev_x,prev_y;

    curr_x=goalx;

    curr_y=goaly;

    while(curr_x!=bot_x||curr_y!=bot_y)

    {

    prev_x=curr_x;

    prev_y=curr_y;

    next_x=node[curr_x][curr_y].visitedfrom_x;

    next_y=node[curr_x][curr_y].visitedfrom_y;

    curr_x=next_x;

    curr_y=next_y;

    }

    *nextx=prev_x;

    *nexty=prev_y;

    }

    void bfs()

    {

    //logic here

    int i,j;

    int x,y;

    int found=0;

  • 8/11/2019 Maze Escape

    18/23

    init_bfs();

    push(bot_x,bot_y);

    node[bot_x][bot_y].visited=1;

    while(stk_N>stk_beg&&!found)

    {

    pop(&x,&y);

    if(land[x][y]=='e')

    {

    found=1;

    break;

    }

    insert_node(x-1,y,x,y);

    insert_node(x,y-1,x,y);

    insert_node(x,y+1,x,y);

    insert_node(x+1,y,x,y);

    }

    }

    //END BFS

  • 8/11/2019 Maze Escape

    19/23

    /*****************************************************/

    void get_destination()

    {

    int p,q;

    int m,n;

    bfs();

    get_next_goal3(&m,&n);

    next_visit(m,n,&p,&q);

    //printf("%d %d\n",p,q);

    if(p==bot_x+1)

    {

    strcpy(direction_to_move,"DOWN");

    bot_x++;

    }

    if(p==bot_x-1)

    {

    strcpy(direction_to_move,"UP");

    bot_x--;

    }

    if(q==bot_y+1)

    {

  • 8/11/2019 Maze Escape

    20/23

    strcpy(direction_to_move,"RIGHT");

    bot_y++;

    }

    if(q==bot_y-1)

    {

    strcpy(direction_to_move,"LEFT");

    bot_y--;

    }

    get_next_facing();

    // if(strcmp(facing,"UP")==0)

    // bot_x--;

    // if(strcmp(facing,"DOWN")==0)

    // bot_x++;

    // if(strcmp(facing,"LEFT")==0)

    // bot_y--;

    // if(strcmp(facing,"RIGHT")==0)

    // bot_y++;

    }

    int main()

    {

  • 8/11/2019 Maze Escape

    21/23

    /* Enter your code here. Read input from STDIN. Print output to STDOUT */

    int bot_id;

    int i,j,k,l;

    char str[200];

    scanf(" %d",&bot_id);

    for(i=0;i

  • 8/11/2019 Maze Escape

    22/23

    get_destination();

    write_land_to_file();

    printf("%s\n",next_move);

    return 0;

    }

    /*

    void display_maze()

    {

    int i,j;

    for(i=0;i

  • 8/11/2019 Maze Escape

    23/23

    {

    scanf("%s",&str);

    for(j=0;j