数据结构
# 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;
}
int getX ( )
{
return this -> x;
}
int getY ( )
{
return this -> y;
}
void add ( int x1, int y1)
{
( this -> nums) ++ ;
struct Queue * 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 )
{
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)
{
int i, j;
for ( i= 0 ; i< column; i++ )
{
for ( j= 0 ; j< line; j++ )
{
Map[ i] [ j] = rand ( ) % 2 ;
}
}
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) ;
}
* /