📄 hw2-04.c
字号:
/*************************************************************
***************** HOMEWORK : 2 ****************************
************* PROGRAM : MAZE *************************
************** NAME : MOONSUNG KIM ***********************
************** ID : 02103012 ***********************
*********** DATE : 2005-04-17 ***********************
*************************************************************/
#include<stdio.h>
#include<string.h>
#define MAX 200
#define lon 25
/***************************************************************
**************** FUNCTION OF STRUCT ************************
***************************************************************/
typedef struct
{
int row;
int col;
}element;
element stack[MAX];
element position;
int startx,starty,endx,endy;
int fstartx,fstarty;
static int mrow;
static int mcol;
char map[lon][lon];
int top=-1;
/****************************************************************
**************** FUNCTION OF OPENFILE ***********************
****************************************************************/
void openfile(char *str)
{
int x=0,y=0,i=0,a=0;
char temp[lon*lon];
FILE *fp;
if((fp=fopen(str, "rb"))==NULL) // close file
{
fprintf(fp, "Failed to open %s\n", "mydata.txt");
//printf("shit");
}
while((fscanf(fp, "%s", temp))==1)
{
strcpy(map[y],temp);
mcol=strlen(map[y]); //get the column of the file
y++;
mrow=y; //get the row of the file
}
printf("THE ROW OF MAP : %d \n",mrow);
printf("THE COLUMN OF MAP : %d \n",mcol);
fclose(fp); // close the file
}
/****************************************************************
**************** FUNCTION OF PRINTMAP ***********************
****************************************************************/
void printmap() //print map
{
int y = 0, x = 0;
printf("\n");
for(y=0; y<mrow; y++)
{
for(x=0; x<mcol; x++)
{
printf("%c",map[y][x]);
}
printf("\n");
}
printf("\n");
}
/**************************************************************
*************** FUNCTION OF STACK (pop) ********************
***************************************************************/
void push(int *top ,element *stack,element position) //Input to the stack !!
{
if(*top>=MAX -1) //If the stack dosen't has space then
{
printf("\n stack overflow");
}
else //stack has space then
{
stack[++(*top)] = position; //make the position into the stack
// printf("%2d",*top);
// printf("[ %2d %2d ]\n",stack[(*top)]); //printf the value which in the stack
}
//printf("\n");
}
/**************************************************************
*************** FUNCTION OF STACK (pop) ********************
***************************************************************/
void pop(int *top)
{
if(*top == -1) //If the stack empty than
{
printf("stack is empty\n");
}
else //If the stack isn't empty pop the value
{ //which in the higest
stack[(*top)--];
}
}
/**************************************************************
**************** FUNCTION OF FIND ENTRANCE ******************
***************************************************************/
void entrance()
{
int fxen=0,fyen=0;
for(startx=0; startx<lon; startx++)
{
for(starty=0; starty<lon; starty++)
{
if(map[startx][starty] == '*') //Search the entrance
{
fstartx = position.row = startx; //take the entrance's value into position
fstarty = position.col = starty;
push(&top,stack,position); //push to stack
}
}
}
for(endx=0; endx<lon; endx++) //Search the Exit
{
for(endy=0; endy<lon; endy++)
{
if(map[endx][endy] == 'E')
{
fxen = position.row = endx;
fyen = position.col = endy;
}
}
}
}
/**************************************************************
************ FUNCTION OF fn(USE FOR MAKE TIME) **************
***************************************************************/
void fn() //just use for make time
{ //use for comfortable
int i;
for(i=0;i<40000000;i++)
{}
}
/**************************************************************
************ FUNCTION OF PATH(FIND THE WAY) ***************
***************************************************************/
void path()
{
int x1=0,y1=0;
printf("\nPATH WAY OF THE MAP USE THE SIGN OF : [ . ]");
printf("\nTHE EXIT USE THE SIGN OF : [ E ]");
printf("\n");
while(fstartx != endx || fstarty != endy) //loop funtion If the entrance isn't Exit then
{
if(map[fstartx][fstarty-1] == 'E') // the left of the point is Exit then
{
position.row = fstartx;
position.col = fstarty - 1;
fstarty = fstarty - 1;
map[fstartx][fstarty] = 'E';
push(&top,stack,position);
break;
}
else if(map[fstartx-1][fstarty] == 'E') // the high of the point is Exit then
{
position.row = fstartx - 1;
position.col = fstarty;
fstartx = fstartx - 1;
map[fstartx][fstarty] = 'E';
push(&top,stack,position);
break;
}
else if(map[fstartx][fstarty+1] == 'E') // the right of the point is Exit then
{
position.row = fstartx;
position.col = fstarty + 1;
fstarty = fstarty + 1;
map[fstartx][fstarty] = 'E';
push(&top,stack,position);
break;
}
else if(map[fstartx+1][fstarty] == 'E') // the down of the point is Exit then
{
position.row = fstartx + 1;
position.col = fstarty;
fstartx = fstartx + 1;
map[fstartx][fstarty] = 'E';
push(&top,stack,position);
break;
}
else if(map[fstartx][fstarty-1] == '0') // the left of the point has way then
{
position.row = fstartx;
position.col = fstarty - 1;
fstarty = fstarty - 1;
map[fstartx][fstarty] = '.';
push(&top,stack,position);
}
else if(map[fstartx-1][fstarty] == '0') // the high of the point has way then
{
position.row = fstartx - 1;
position.col = fstarty;
fstartx = fstartx - 1;
map[fstartx][fstarty] = '.';
push(&top,stack,position);
}
else if(map[fstartx][fstarty+1] == '0') // the right of the point has way then
{
position.row = fstartx;
position.col = fstarty + 1;
fstarty = fstarty + 1;
map[fstartx][fstarty] = '.';
push(&top,stack,position);
}
else if(map[fstartx+1][fstarty] == '0') // the down of the point has way then
{
position.row = fstartx + 1;
position.col = fstarty;
fstartx = fstartx + 1;
map[fstartx][fstarty] = '.';
push(&top,stack,position);
}
else if(map[fstartx][fstarty-1] == '.') // the left of the way hasn't way
{
map[fstartx][fstarty] = '#';
pop(&top);
position.row = fstartx;
position.col = fstarty - 1;
fstarty = fstarty - 1;
}
else if(map[fstartx-1][fstarty] == '.') // the high of the way hasn't way
{
map[fstartx][fstarty] = '#';
pop(&top);
position.row = fstartx - 1;
position.col = fstarty;
fstartx = fstartx - 1;
}
else if(map[fstartx][fstarty+1] == '.') // the right of the way hasn't way
{
map[fstartx][fstarty] = '#';
pop(&top);
position.row = fstartx;
position.col = fstarty + 1;
fstarty = fstarty + 1;
}
else if(map[fstartx+1][fstarty] == '.') // the down of the way hasn't way
{
map[fstartx][fstarty] = '#';
pop(&top);
position.row = fstartx + 1;
position.col = fstarty;
fstartx = fstartx + 1;
}
else // If hasn't the path way then
{
printf("THIS MAP HASN'T PATH !!\n");
break;
}
fn();
printmap();
}
printf("\n");
}
/**************************************************************
**************** FUNCTION OF PRINTTOP () ******************
**************** JUST TEST FOR PRINT THE WAY ****************
***************************************************************/
void printtop() // it can print satck point
{
int i;
for(i=0;i<=top;i++)
{
printf("[%2d %2d]\t",stack[(i)]);
}
}
/**************************************************************
**************** FUNCTION OF MAIN() ******************
***************************************************************/
int main(int argc,char *argv[])
{
printf("**********************************************************\n");
printf("*************** THE HOMEWORK OF TEAM C *****************\n");
printf("**********************************************************\n");
printf("YOUR FILE IS LIKE THIS :\n\n");
openfile(argv[1]);
printmap();
printf("\n**********************************************************\n");
printf("*************** OK LET'S GO *****************\n");
printf("**********************************************************\n\n");
entrance();
path();
printf("\n**********************************************************\n");
printf("*************** THE PATH WAY IS : ******************\n");
printf("**********************************************************\n");
printf("\nPATH WAY OF THE MAP USE THE SIGN OF : [ . ]");
printf("\nTHE EXIT USE THE SIGN OF : [ E ]\n");
printmap();
printtop();
printf("\n**********************************************************\n");
printf("*************** CONGRATULATION !!! *****************\n");
printf("*************** E N D !!! *****************\n");
printf("**********************************************************\n");
}
/******************************************************************
******************** E N D *********************
*******************************************************************/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -