• 迷宫出路


    #include<iostream>
    using namespace std;
    typedef struct node
    {
    	int i,j;
    	int pre;
    }*ptrn;
    void main()
    {
    	ptrn *quelist;
    	quelist=new ptrn[100];
    	int front=0,rear=0; 
         int matrix[10][10]={
    	{1,1,1,1,1,1,1,1,1,1},
    	 {1,0,0,1,0,0,0,1,0,1},
    	 {1,0,0,1,0,0,0,1,0,1},
    	 {1,0,0,0,0,1,1,0,0,1},
    	 {1,0,1,1,1,0,0,0,0,1},
    	 {1,0,0,0,1,0,0,0,0,1},
    	 {1,0,1,0,0,0,1,0,0,1},
    	 {1,0,1,1,1,0,1,1,0,1},
    	 {1,1,0,0,0,0,0,0,0,1},
    	 {1,1,1,1,1,1,1,1,1,1}};
    	cout<<"请输入入口在矩阵中的行位置和列位置"<<endl;
    	int i1,j1;
    	cin>>i1>>j1;
    	cout<<"请输入出口在矩阵中的行位置和列位置"<<endl;
    	int i2,j2;
    	cin>>i2>>j2;
    	ptrn stnode;
    	stnode=new node;
    	stnode->i=i1;
    	stnode->j=j1;
    	stnode->pre=-1;
    	quelist[0]=stnode;
    	rear=1;
    	while(1)
    	{
    		
    		if(quelist[front]->i!=i2||quelist[front]->j!=j2)
    		{
    			int i3=quelist[front]->i;
    			int j3=quelist[front]->j;
    			if(matrix[i3][j3+1]==0)
    			{
    				quelist[rear]=new node;
    				quelist[rear]->i=i3;
    				quelist[rear]->j=j3+1;
    				quelist[rear]->pre=front;
    				matrix[i3][j3+1]=1;
    				rear++;
    			}
    		    if(matrix[i3+1][j3]==0)
    			{
                   	quelist[rear]=new node;
                   quelist[rear]->i=i3+1;
    			   quelist[rear]->j=j3;
    			   quelist[rear]->pre=front;
    			   matrix[i3+1][j3]=1;
    			   rear++;
    			}
    			if(matrix[i3][j3-1]==0)
    			{
    				quelist[rear]=new node;
    				quelist[rear]->i=i3;
    			   quelist[rear]->j=j3-1;
    			   quelist[rear]->pre=front;
    			   matrix[i3][j3-1]=1;
    			   rear++;
    			}
    			if(matrix[i3-1][j3]==0)
    			{
                   	quelist[rear]=new node;
    				quelist[rear]->i=i3-1;
    			   quelist[rear]->j=j3;
    			   quelist[rear]->pre=front;
    			   matrix[i3-1][j3]=1;
    			   rear++;
    			}
    			front++;
    			if(front==rear+1)
    		    {
    			    cout<<"无路径"<<endl;
    			   break;
    		    }
    		}
    		else
    		{
    			break;
    		}
    	}
        while(1)
    	{
    		if(quelist[front]->pre==-1)
    		{
                cout<<"["<<quelist[front]->i<<","<<quelist[front]->j<<"]"<<endl;
    			break;
    		}
    		else
    		{
    			cout<<"["<<quelist[front]->i<<","<<quelist[front]->j<<"]"<<endl;
    		}
    	   front=quelist[front]->pre;
    	}
    }
    

  • 相关阅读:
    两种选择排序法
    三种方法求组合偶数字
    sizeof和mallo
    多态的概念与接口重用
    Delphi Exception的理解
    给老婆留着
    Delphi 一个简单的DELPHI自定义事件的例子
    Delphi 纯Pascal编写的程序,没有通过VCL
    Delphi 继承类的构造函数和析构函数的构建顺序
    Delphi 对象间数据的复制
  • 原文地址:https://www.cnblogs.com/zztong/p/6695296.html
Copyright © 2020-2023  润新知