百练 4127:迷宫问题

#include <bits/stdc++.h>
using namespace std;
struct Pos{
	int r, c;
	int f;
	Pos(int rr = 0, int cc = 0, int ff = 0) : r(rr), c(cc), f(ff){}
};
const int maxLofMaze = 8;
const int maxLofqueue = 100;
int maze[maxLofMaze + 1][maxLofMaze + 1];
Pos que[maxLofqueue + 1];
int head = 0, tail = 1;
Pos dir[4] = {Pos(-1, 0), Pos(1, 0), Pos(0, -1), Pos(0, 1)}; //用来移动方向
int main()
{
	memset(maze,0xff,sizeof(maze)); 
	int i, j;
	for(i = 1; i <= 5; i++){
		for(j = 1; j <= 5; j++){
			scanf("%d", &maze[i][j]);
		}
	} 
	que[0] = Pos(1, 1, -1);
	
	while(head != tail){
		
		Pos ps = que[head];
		
		if(ps.r == 5 && ps.c == 5){
			
			vector<Pos> vect; 
			
			while(true){
				vect.push_back(Pos(ps.r, ps.c, 0));
				if(ps.f == -1) break;
				ps = que[ps.f]; 
			}
			
			for(i = vect.size() - 1; i >= 0; i--){
			
				printf("(%d, ", vect[i].r - 1);
				printf("%d)\n", vect[i].c - 1); 
			
			}
			
			return 0;
		}else{
			int r = ps.r, c = ps.c;
			
			for(i = 0; i < 4; i++){
				if(!maze[r + dir[i].r][c + dir[i].c]){
					que[tail++] = Pos(r + dir[i].r, c + dir[i].c, head);
					maze[r + dir[i].r][c + dir[i].c] = 1;
				}
			}
			head++;
		}
	}
	
	return 0;
} 
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值