Job Save 1

Source: Internet
Author: User

#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);//Don't forget the type conversion
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);
}
};
Template<class t>
struct stack{
T * BASE;
T * head,* Tail;int Tail_pos;
int now_size,whole_size;
Stack () {
Base= (t*) malloc (sizeof (T) *initial_lize);
Head=tail=base+1;tail_pos=1;
Now_size=0;whole_size=initial_lize;
}
BOOL Empty () {
if (Tail==head) return true;
else return false;
}
void push (const T x) {
now_size++;
if (now_size<whole_size) {
tail++;tail_pos++;
*tail=x;
}
else{
Base= (t*) realloc (base,sizeof (T) * (whole_size*2));
whole_size*=2;
Head=base+1;tail=base+tail_pos;
Tail++;tail_pos++;*tail=x;
}
}
void Pop () {
if (! Empty ()) {tail--;}
Else {cerr<< "The Stack is Empty!can not pops any elements" &LT;&LT;ENDL;}
}
T Top () {
if (! Empty ()) {return *tail;}
Else {cerr<< "The Stack is Empty!can not pops any elements" &LT;&LT;ENDL;}
}
void Delete_stack () {
Free (base);
}
};
const int m_size=100;
int matrix[m_size][m_size],sx,sy,dx,dy,n;
BOOL Vis[m_size][m_size];
struct node{
int x,y,step;
};
const int DIR[4][2]={{1,0},{-1,0},{0,1},{0,-1}};
BOOL Check (int x,int y) {
if (x<0| | x>=n| | y<0| | Y>=n) return false;
else return true;
}
void print () {
Sleep (1000);
System ("CLS");
for (int i=0;i<n;i++) {
for (int j=0;j<n;j++) {
if (i==sx&&j==sy) printf ("S");
else if (Vis[i][j]) printf ("N");
else printf ("%d", matrix[i][j]);
}
printf ("\ n");
}
}
int ans;
int BFS (int sx,int sy,int dx,int dy,int draw) {
Queue<node> que;ans=0;
memset (vis,false,sizeof (VIS));
Que.push (node) {sx,sy,0});
Vis[sx][sy]=true;bool _find=false;
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];
int step=k.step+1;
if (check (x, y) &&!vis[x][y]&&matrix[x][y]!=0) {vis[x][y]=true;
if (X==dx&&y==dy) {if (draw) print (); _find=true;printf ("find"); Ans=max (ans,step); break;}
Que.push (node) {x,y,step});
if (draw) print (); Ans=max (Ans,step);
}
}
}
Que.delete_queue ();
return _find;
}
BOOL Havefind (int x,int y) {
if (X==dx&&y==dy) return true;
else return false;
}
BOOL Dvis[m_size][m_size][4];
int dfs (int sx,int sy,int dx,int dy,int draw) {
memset (dvis,false,sizeof (Dvis));
Stack<node> S;memset (vis,false,sizeof (VIS));
S.push (node) {sx,sy,0}); vis[sx][sy]=true;
BOOL _find=false;
while (!s.empty ()) {
Node P=s.top ();
Node k=p;
Sleep (1000);
if (K.x==dx&&k.y==dy) S.pop ();
P=s.top ();
K=p;
printf ("k:%d%d\n", k.x,k.y);
while (!havefind (K.X,K.Y) &&check (k.x,k.y)) {
Sleep (1000);
printf ("k:%d%d\n", k.x,k.y);
BOOL Havedir=false;
for (int i=0;i<4;i++) {
int tx,ty;
if (!dvis[k.x][k.y][i]) {
Havedir=true;
TX=K.X+DIR[I][0];
TY=K.Y+DIR[I][1];
Dvis[k.x][k.y][i]=true;
if (check (tx,ty) &&!vis[tx][ty]&&matrix[tx][ty]!=0) {
Vis[tx][ty]=true;
S.push (node) {tx,ty,k.step+1});
Print ();
k= (node) {tx,ty,k.step+1};break;
}
}
}
if (!havedir) break;
}
Node f=s.top (); bool Havedir=false;
for (int i=0;i<4;i++) {
if (!dvis[f.x][f.y][i]) {havedir=true;break;}
}
if (!havedir) {
S.pop ();
Vis[f.x][f.y]=false;
}
}
}
BOOL General_matrix (int sx,int sy,int dx,int dy,int N) {
for (int i=0;i<n;i++) {
for (int j=0;j<n;++j) {
Matrix[i][j]=rand ()%2;
}
}
while (!BFS (sx,sy,dx,dy,0)) {
for (int i=0;i<n;i++) {
for (int j=0;j<n;++j) {
Matrix[i][j]=rand ()%2;
}
}
}
printf ("successfully generated maze \ n");
}
int main () {
Test of the queue
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 ();
printf ("Enter the order of the maze and start point:");
cin>>n>>sx>>sy>>dx>>dy;
printf ("Input complete \ n");
Srand (Time (NULL));
General_matrix (Sx,sy,dx,dy,n);
DFS (sx,sy,dx,dy,1);
BFS (sx,sy,dx,dy,1);p rintf ("Minimum step is%d", ans);

return 0;
}

Job Save 1

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.