百練 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;
} 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章