Explore and draw a unknown map with DFS

#include <stdio.h>



char *_map[]={
"                        ",
" 00000 0    000000000   ",
"   00000    0   0  0000 ",
"    0  00   0   0  0    ",
"    0   0   0   0  000  ",
"  0000  000000000    00 ",
"                        ",
};

char map[2000][2000];

int _x = 16;
int _y = 5;
//map[1][3];
//map[2][3];;
void print_map()
{
	int i,j;

	printf("\033[2J");

	print__map();

	for(i=980;i<1010;i++){
		for(j=980;j<1030;j++){
			printf("%c",map[i][j]);
		}
		printf("\r\n");
	}
	printf("\r\n");
	usleep(500000);
}

void print__map()
{
	int i,j;
	int xx,yy;
	xx = 0;
	yy = 0;
	
	for(i=0;i<7;i++){
		for(j=0;j<24;j++){
			printf("%c",_map[yy][xx]);
			xx++;
		}
		xx=0;
		yy++;
		printf("\r\n");
	}
}

int try_move_left()
{
	if(_map[_y][_x - 1] == '0')
	{
		_x = _x - 1;
		return 1;
	}

	return 0;
}

void move_left()
{
	_x = _x - 1;
}

int try_move_right()
{
	if(_map[_y][_x + 1] == '0')
	{
		_x = _x + 1;
		return 1;
	}

	return 0;
}

void move_right()
{
	_x = _x + 1;
}

int try_move_up()
{
	if(_map[_y - 1][_x] == '0')
	{
		_y = _y - 1;
		return 1;
	}

	return 0;
}

void move_up()
{
	_y = _y - 1;
}

int try_move_down()
{
	if(_map[_y + 1][_x] == '0')
	{
		_y = _y + 1;
		return 1;
	}

	return 0;
}

void move_down()
{
	_y = _y + 1;
}

int main()
{
	int i,j;
	int cur_x, cur_y;	
	int need_out = 0;
	int safe_cnt = 500;
	int state = 0;
	
	for(i=0;i<2000;i++){
		for(j=0;j<2000;j++){
			map[i][j] = ' ';
		}
	}

	cur_x = 1000;
	cur_y = 1000;

	//print_map();

	//print__map();

	//return 0;

	while(safe_cnt-- > 0){

	state = 0;

	left_again:
		//左面未知
		if(map[cur_y][cur_x - 1] == ' '){
			state = 0;
			if(try_move_left() == 1){
				if(need_out){
					break;
				}
				if(map[cur_y][cur_x] == ' '){
					map[cur_y][cur_x] = 'L';
				}				
				//printf("go left\r\n");
				map[cur_y][cur_x - 1] = 'R';
				cur_x = cur_x - 1;print_map();
				goto left_again;
			}else{
				//printf("left is wall\r\n");
				map[cur_y][cur_x - 1] =  '*';print_map();
				continue;
			}
		}else{
			state |= 1;
		}

	right_again:
		//右面未知
		if(map[cur_y][cur_x + 1] == ' '){
			state = 0;
			if(try_move_right() == 1){
				if(need_out){
					break;
				}
				if(map[cur_y][cur_x] == ' '){
					map[cur_y][cur_x] = 'R';
				}
				//printf("go right\r\n");
				map[cur_y][cur_x + 1] = 'L';
				cur_x = cur_x + 1;print_map();
				goto right_again;
			}else{
				//printf("right is wall\r\n");
				map[cur_y][cur_x + 1] =  '*';print_map();
				continue;
			}
		}else{
			state |= 2;
		}

	up_again:
		//上面未知
		if(map[cur_y - 1][cur_x] == ' '){
			state = 0;
			if(try_move_up() == 1){
				if(need_out){
					break;
				}
				if(map[cur_y][cur_x] == ' '){
					map[cur_y][cur_x] = 'U';
				}
				//printf("go up\r\n");
				map[cur_y - 1][cur_x] = 'D';
				cur_y = cur_y - 1;print_map();
				goto up_again;
			}else{
				//printf("up is wall\r\n");
				map[cur_y - 1][cur_x] =  '*';print_map();
				continue;
			}
		}else{
			state |= 4;
		}

	down_again:
		//下面未知
		if(map[cur_y + 1][cur_x] == ' '){
			state = 0;
			if(try_move_down() == 1){
				if(need_out){
					break;
				}
				if(map[cur_y][cur_x] == ' '){
					map[cur_y][cur_x] = 'D';
				}
				//printf("go down\r\n");
				map[cur_y + 1][cur_x] = 'U';
				cur_y = cur_y + 1;print_map();
				goto down_again;
			}else{
				//printf("down is wall\r\n");
				map[cur_y + 1][cur_x] =  '*';print_map();
				continue;
			}
		}else{
			state |= 8;
		}

		if(state == 0xf){
			//死胡同,回退
			if(map[cur_y][cur_x] == 'L'){
				if(map[cur_y][cur_x - 1] == 'R'){
					//break;
					map[cur_y][cur_x - 1] = ' ';
				}
				cur_x = cur_x - 1;
				move_left();
			}else if(map[cur_y][cur_x] == 'R'){
				if(map[cur_y][cur_x + 1] == 'L'){
					//break;
					map[cur_y][cur_x + 1] = ' ';
				}
				cur_x = cur_x + 1;
				move_right();
			}else if(map[cur_y][cur_x] == 'U'){
				if(map[cur_y - 1][cur_x] == 'D'){
					//break;
					map[cur_y - 1][cur_x] = ' ';
				}
				cur_y = cur_y - 1;
				move_up();
			}else if(map[cur_y][cur_x] == 'D'){
				if(map[cur_y + 1][cur_x] == 'U'){
					//break;
					map[cur_y + 1][cur_x] = ' ';
				}
				cur_y = cur_y + 1;
				move_down();
			}else{
				break;
			}
		}
	}

	print_map();
	printf("%d",safe_cnt);
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值