U E D R , A S I H C RSS

미로찾기/김태훈

~cpp 
#include <stdio.h>
#include <time.h>
#include <conio.h>

#define MAP_X 5
#define MAP_Y 5

typedef struct ztype {
	int row;
	int col;
}ztype;

int move(ztype );
void prtpoint();


int map[MAP_X][MAP_Y]={
		{0,1,0,0,0},
		{1,0,0,0,1},
		{0,1,1,0,0},
		{1,1,0,1,1},
		{1,1,1,0,0}
};

/*
int map[MAP_X][MAP_Y]={

	{0,1,0,0,0,1,1,0,0,0,1,1,1,1,1},
	{1,0,0,0,1,1,0,1,1,1,0,0,1,1,1},
	{0,1,1,0,0,0,0,1,1,1,1,0,0,1,1},
	{1,1,0,1,1,1,1,0,1,1,0,1,1,0,0},
	{1,1,0,1,0,0,1,0,1,1,1,1,1,1,1},
	{0,0,1,1,0,1,1,1,0,1,0,0,1,0,1}, 
	{0,1,1,1,1,0,0,1,1,1,1,1,1,1,1}, 
	{0,0,1,1,0,1,1,0,1,1,1,1,1,0,1}, 
	{1,1,0,0,0,1,1,0,1,1,0,0,0,0,0}, 
	{0,0,1,1,1,1,1,0,0,0,1,1,1,1,0}, 
	{0,1,0,0,1,1,1,1,1,0,1,1,1,1,0}
};
*/
ztype p,go; //&#65415;&#58599;&#12539;&#65408;&#65383;&#65412;&#65377;

int main()
{

	int x,y,run=0;
	srand(time(NULL));

	//&#65411;&#65418;&#65393;&#31629;&#65386;;
	p.col =0;
	p.row =0;

	
	while(1) {
		system("cls");
		x = (rand()%3)-1;
		y = (rand()%3)-1;
		if(x==0 && y==0) continue;
		go.row = p.row + y;
		go.col = p.col + x;
		move(go);
		run++;

		printf("%d &#65401;&#58977;&#65392; &#65405;&#65415;&#65415;&#29408;&#65439;\n",run);
		prtpoint();
		
		//&#65400;&#65398;&#65409;&#58591;&#65399; &#65412;&#65389;&#65407;&#65377; &#65397;&#65397;&#65410;&#58982;&#65439;&#65408;&#65403; &#65392;&#35964;&#12539;
		if(p.col==MAP_X-1 && p.row ==MAP_Y-1) break;
	} 

	
	printf("%d&#65401;&#58967;&#65400;&#65407;&#65377; &#65413;&#65403;&#65411;&#12539;&#65404;&#65402;&#65392;&#58910;!\n",run);


	return 0;

}


int move(ztype to)
{

	//&#65401;霑&#65389;&#65408;&#65415; &#65401;&#12539;&#65383;&#65400;&#65382; &#65401;&#63730;&#65406;&#37607;&#65391; &#65398;&#65383; &#65380;&#65399;&#65380;&#65397;&#65380;&#65399;;;
	if(to.col<0 || to.col > MAP_X-1 || to.row<0 || to.row > MAP_Y-1)
	{
		return 1;
	}
	
	//&#65400;&#65418;&#65407;&#65377;&#65404;&#65389; &#65402;&#65390;(1)&#65408;&#65423;&#65398;&#65383;
	if(map[to.row][to.col]==1){
		return 1;
	}

	p = to;


	return 0;
}

void prtpoint()
{
	int i,j;

	for(i=0;i<MAP_Y;i++)
	{
		for(j=0;j<MAP_X;j++)
		{
			if(p.col==j &&p.row ==i) 
				printf("X ");
			else 
				printf("%d ",map[i][j]);
		}
		printf("\n");
	}
	printf("\n");


	getch();
	
}

Valid XHTML 1.0! Valid CSS! powered by MoniWiki
last modified 2021-02-07 05:29:31
Processing time 0.0151 sec