#include<iostream> #include<cstdlib> #include<cstdio> #include<time.h> #include<windows.h> using namespace std; const int initial_lize=10000; const int adding_size=2*initial_lize; template<class T> struct Queue{ T * base; T * Qhead,*Qbegin,*Qend,*Qfront,*Qtail; int front_pos,tail_pos; int now_size,whole_size; Queue(){ base=(T *)malloc(sizeof(T)*initial_lize); Qhead=base; Qbegin=base;Qend=base+(initial_lize-1); Qfront=Qtail=base+1; front_pos=tail_pos=1; now_size=1; whole_size=initial_lize; } bool Empty(){ if(Qfront==Qtail) return true; else return false; } void push(const T x){ now_size++; if(now_size<whole_size){ *Qtail=x; Qtail++; tail_pos++; } else{ base=(T *)realloc(base,(sizeof(T))*whole_size*2);//别忘了类型转换 whole_size*=2; Qhead=base;Qend=base+(whole_size-1); Qfront=base+front_pos;Qtail=base+tail_pos; *Qtail=x;Qtail++;tail_pos++; } } T Top(){ if(!Empty()) return (*Qfront); else{ cerr<<"Queue is empty! can not return any elements"<<endl; } } void pop(){ if(!Empty()){ Qfront++; front_pos++; } else{ cerr<<"Queue is empty! can not pop any elements"<<endl; } } void delete_Queue(){ free(base); } }; const int m_size=100; int matrix[m_size][m_size]; struct node{ int x,int y; }; const int dir[4][2]={{1,0},{-1,0},{0,1},{0,-1}}; bool check(int x,int y){ if(x<0||x>=m_size||y<0||y>=m_size) return false; else return true; } int bfs(int sx,int sy,int dx,int dy){ Queue<node> que; que.push((node){sx,sy}); while(!que.Empty()){ node k=que.Top(); que.pop(); for(int i=0;i<4;i++){ int x=k.x+dir[i][0]; int y=k.y+dir[i][1]; if(check(x,y)) {if(x==dx&&y==dy){print();break;} que.push((node){x,y});} } } delete_Queue(); } bool general_matrix(){} int main(){ //队列的测试 // Queue<int> que; // int temp=0; // que.Top(); // que.pop(); // for(int i=0;i<10;i++){ // que.push(i); // } // for(int i=0;i<10;i++){ // temp=que.Top(); // que.pop(); // cout<<temp<<endl; // } // que.delet_Queue(); srand(time(NULL)); general_matrix(); bfs(); return 0; }
时间: 2024-12-28 10:47:42