最短路径(C语言)

数据结构

#include<stdio.h>
#include<windows.h>
#include<stdlib.h>
#include<math.h>
int minStep=-1;
int Map[30][30];
int Book[30][30];//标记该点是否走过  
//队列先进先出  从队首加入新元素  
struct Queue {
	int nums = 0;//该队列所含的元素数量 
	int x,y;
	int data;
	int dire=0;//已经走过的方向   
	struct Queue* head = (Queue*)calloc(1,sizeof(struct Queue));//头结点 
	struct Queue* temp = (Queue*)calloc(1,sizeof(struct Queue));//尾结点 即栈顶元素 
	struct Queue* next = (Queue*)calloc(1,sizeof(struct Queue));
	struct Queue* front = (Queue*)calloc(1,sizeof(struct Queue));

	int stepPlus()
	{
		this->dire++;
	}
	//栈包含的元素 
	int getNum()
	{
		return this->nums;
	}
	//得到栈顶元素的x 
	int getX()
	{
		return this->x;
	}
	int getY()
	{
		return this->y;
	}
	//尾接 
	void add(int x1,int y1)
	{
		(this->nums)++;
		struct Queue* head2;
		//head2为栈顶元素 
		head2 = (Queue*)calloc(1,sizeof(struct Queue));
		head2->x = x1;
		head2->y= y1;
		head2->next = NULL;
		head2->front = NULL;
		if (this->head == NULL || nums == 1)
		{
			this->head = head2;
			this->temp = head2;
			return;
		}
		this->temp->next = head2;
		head2->front = this->temp;
		this->temp = head2;
	}
	//前出 
	struct Queue* delt()
	{
		(this->nums)--;
		if(this->nums==0)
		{
//			free(this->head);
//			free(this->temp);
			struct Queue* head2;
			head2 = (Queue*)calloc(1,sizeof(struct Queue));
			head2 = this->head;
			this->temp=(Queue*)calloc(1,sizeof(struct Queue));
			this->head=(Queue*)calloc(1,sizeof(struct Queue));
			return head2;
		}
		struct Queue* head2,*head3;
		head2 = (Queue*)calloc(1,sizeof(struct Queue));
		head3 = (Queue*)calloc(1,sizeof(struct Queue));
		head3 = this->head;
		head2 = this->head->next;
		head2->front = NULL;
		this->head = head2;
		return head3;
	}
	struct Queue* pop()
	{
		(this->nums)--;
		int num = this->temp->data;
		if(this->nums<=0)
		{
			struct Queue* head2;
			head2 = (Queue*)calloc(1, sizeof(struct Queue));
			head2=this->head;
			this->temp=(Queue*)calloc(1,sizeof(struct Queue));
			this->head=(Queue*)calloc(1,sizeof(struct Queue));
			return head2;
		}
		struct Queue* head2,*head3;
		head2 = (Queue*)calloc(1, sizeof(struct Queue));
		head3 = (Queue*)calloc(1, sizeof(struct Queue));
		head3 = this->temp;
		head2 = this->temp->front;
		head2->next = NULL;
		this->temp = head2;
		return head3;
	}
	//得到栈顶元素 
	struct Queue* getTop()
	{
		return this->temp;
	} 
	void showData()
	{
		if(this->nums<=0)
			return;
		if(this->head->next==NULL)
		{
			printf("%d  ", head->data);
			return;
		}
		struct Queue* head2;
		head2 = (Queue*)calloc(1,sizeof(struct Queue));
		head2 = this->head;
		while (1)
		{
			if (head2->next == NULL)
			{
				printf("%d  ", head2->data);
				break;
			}
			printf("%d  ", head2->data);
			head2 = head2->next;
		}
	}
	bool isEmpty()
	{
		if (this->nums == 0)
			return true;
		return false;
	}

};
void CreatMap(int column,int line)//行列  line:列  column:行 
{
	int i,j;
	for(i=0;i<column;i++)
	{
		for(j=0;j<line;j++)
		{
			Map[i][j]=rand()%2;
		//	Map[i][j]=0;
		}
	} 
	//建墙 
	for(i=0;i<line;i++)
	{
		Map[0][i]=3;
		Map[column-1][i]=3;
	} 
	for(i=0;i<column;i++)
	{
		Map[i][0]=3;
		Map[i][line-1]=3;
	}
	Map[1][1]=4;//起点 
	Map[column-2][line-2]=4;//终点 
};
void showMap(int column,int line)
{
	int i,j;
	for(i=0;i<column;i++)
	{
		for(j=0;j<line;j++)
		{
			printf("%d ",Map[i][j]);
		}
		printf("\n");
	} 
};
bool Judge(struct Queue* head,int column,int line)
{
	printf("\n");
	int k=0;
	int nextStep[][2]={{0,1},{1,0},{0,-1},{-1,0},{-1,1},{-1,-1},{1,-1},{1,1}};
	while(!head->isEmpty())
	{
		struct Queue* temp = head->getTop();
		
		for(int i = 0;i < 8;i ++)
		{
			int tx = temp->x + nextStep[i][0];
			int ty = temp->y + nextStep[i][1];
					
			if( Map[tx][ty] == 4&&tx==column-2&&ty==line-2)
			{
				if(head->getTop()<8)
				{
					if(Minstep==-1)
					{
						Minstep=head->getNum();
						continue;
					}
					if(head->getNum()<Minstep)
					{
						Minstep=head->getNum();
					}
				}
				return true; 
			}
						
			if(Map[tx][ty] == 0 && Book[tx][ty] != 1)
			{
				k=1;
				head->add(tx,ty);
				head->getTop()->stepPlus();
				Book[tx][ty] = 1;
				break;
			}						
		}
		if(k==0)	
		  free(head->pop());	
		if(k==1) 
			k=0;
	}
	return false;		   
};

int main()
{
	int line,column;
	bool a;
	//栈 
	struct Queue* head = (struct Queue*)calloc(1, sizeof(struct Queue));
	struct Queue* head2 = (struct Queue*)calloc(1, sizeof(struct Queue));
	printf("请输入需要创造的地图大小(行列):");
	scanf("%d %d",&column,&line);
	CreatMap(column,line);
	showMap(column,line);
	head->add(1,1);//压入起点 ;
	a=Judge(head,column,line);
	if(a)
	{
		printf("存在路径,路径坐标为:\n\n");
		while(!head->isEmpty())
		{
			head2=head->delt();	
			printf("{%d,%d}  ",head2->getX(),head2->getY());
			free(head2);
		}
	}
	else
		printf("\n没有路径。。。。");
	free(head);
	
}

*/
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值