迷宫问题bsf ACM第三次培训

#include<stdio.h>
#include<string.h>
#include<math.h>
#include<stdlib.h>
#include<algorithm>
#include<iostream>
#include<queue>
using namespace std;
int maze[5][5];
int map_sign[5][5] = { 0 };
int d[4][2] = { 0,1,1,0,0,-1,-1,0 };
struct node
{
	int x, y, p;
};
struct node_record
{
	int x, y;
	int last;
};
node_record s1[1000];
int extend()
{
	int i = 0;
	queue <node> q;
	queue<int> q_num;
	node s,e;
	s.x = 0;
	s.y = 0;
	s1[0].last = -1;
	//s.p=0
	s1[0].x = 0;
	s1[0].y = 0;
	//node_record *p=&s1[0];
	map_sign[0][0] = 1;
	q.push(s);
	int r = 0;
	q_num.push(r);
	int t;
	while(!q.empty())
	{
		e = q.front();
		q.pop();
		if(e.x==4 && e.y==4)
		{
			return q_num.front(); 
		}
		 t = q_num.front();
		for(int k=0;k<4;k++)
		{
			s.x = e.x + d[k][0];
			s.y = e.y + d[k][1];
			if(s.x<0 || s.x>4 || s.y<0 || s.y>4 || map_sign[s.x][s.y]==1 ||maze[s.x][s.y]==1 )
			{
				continue;
			}
			i++;
			s1[i].x = s.x;
			s1[i].y = s.y;
			s1[i].last = t;
			//p = &s1[i];
			map_sign[s.x][s.y] = 1;
			q.push(s);
			q_num.push(i);
		}
		q_num.pop();
	}
}
void reprintf(int x)
{
	if(s1[x].last==-1)
	{
		printf("(%d, %d)\n", s1[x].x, s1[x].y);
	}
	else
	{
		reprintf(s1[x].last);
		printf("(%d, %d)\n", s1[x].x, s1[x].y);
	}
}
int main()
{
	int n, m;
	n = m = 5;
	for(int i=0;i<n;i++)
	{
		for(int j=0;j<m;j++)
		{
			scanf("%d", &maze[i][j]);
		}
	}
	int last = extend();
	reprintf(last);
	return 0;
}

全部评论

相关推荐

kyw_:接好运
点赞 评论 收藏
分享
评论
点赞
收藏
分享
牛客网
牛客企业服务