迷宫问题,还非得用一下栈T_T
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 using namespace std; 5 const int Max=1000; 6 struct point 7 { 8 int x,y; 9 bool operator== (point a) const {if(x==a.x&&y==a.y) return 1;return 0;}; 10 void print() const {cout<<x<<" "<<y;}; 11 }; 12 class stack 13 { 14 private: 15 point a[Max]; 16 int top; 17 public: 18 stack(){top=-1;}; 19 ~stack(){}; 20 bool is_empty(){return top==-1?1:0;}; 21 bool push(point x){a[++top]=x;return 1;}; 22 bool pop(); 23 point gettop(){return a[top];}; 24 }; 25 bool stack::pop() 26 { 27 if(!is_empty()) 28 { 29 top--; 30 return 1; 31 } 32 return 0; 33 } 34 const int map[6][8]={ 35 {1,1,1,1,1,1,1,1}, 36 {1,0,0,1,0,0,0,1}, 37 {1,1,0,0,0,0,0,1}, 38 {1,0,0,1,0,1,0,1}, 39 {1,0,0,0,0,0,0,1}, 40 {1,1,1,1,1,1,1,1} 41 }; 42 int vis[6][8]; 43 stack Minpath,path; 44 int MinLength=Max,Length=0; 45 point start,end,next; 46 int dx[4]={1,-1,0,0},dy[4]={0,0,-1,1}; 47 int DFS(point p) 48 { 49 if(p==end&&Length<MinLength) 50 { 51 Minpath=path; 52 MinLength=Length; 53 return 0; 54 } 55 if(vis[p.x][p.y]||map[p.x][p.y]||p.x<0||p.x>=6||p.y<0||p.y>=8) 56 return 0; 57 for(int i=0;i<4;i++) 58 { 59 next.x=p.x+dx[i]; 60 next.y=p.y+dy[i]; 61 Length++; 62 vis[p.x][p.y]=1; 63 path.push(next); 64 DFS(next); 65 path.pop(); 66 Length--; 67 vis[p.x][p.y]=0; 68 } 69 return 0; 70 } 71 int main() 72 { 73 start.x=1,start.y=1,end.x=4,end.y=6; 74 memset(vis,0,sizeof(vis)); 75 path.push(start); 76 DFS(start); 77 cout<<"最短路径长度:"<<MinLength<<endl; 78 while(!Minpath.is_empty()) 79 { 80 Minpath.gettop().print(); 81 cout<<endl; 82 Minpath.pop(); 83 } 84 return 0; 85 }
入门搜索题
运算表达式:
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <map> 5 using namespace std; 6 const int Max=1000; 7 class stack 8 { 9 private: 10 int a[Max]; 11 int top; 12 public: 13 stack() {top=-1;}; 14 ~stack() {}; 15 bool is_empty() {return top==-1?1:0;}; 16 bool push(int x) {a[++top]=x;}; 17 bool pop(); 18 int gettop() {return a[top];}; 19 }; 20 bool stack::pop() 21 { 22 if(!is_empty()) 23 { 24 top--; 25 return 1; 26 } 27 return 0; 28 } 29 30 class stackch 31 { 32 private: 33 char a[Max]; 34 int top; 35 public: 36 stackch() {top=-1;}; 37 ~stackch() {}; 38 bool is_empty() {return top==-1?1:0;}; 39 bool push(int x) {a[++top]=x;}; 40 bool pop(); 41 char gettop() {return a[top];}; 42 }; 43 bool stackch::pop() 44 { 45 if(!is_empty()) 46 { 47 top--; 48 return 1; 49 } 50 return 0; 51 } 52 53 stack num; 54 stackch ch; 55 char str[Max]; 56 char s,st; 57 int t; 58 int len,pos=0; 59 map<char,int> sign; 60 void init() 61 { 62 sign.insert(pair<char,int>(‘#‘,0)); 63 sign.insert(pair<char,int>(‘)‘,1)); 64 sign.insert(pair<char,int>(‘+‘,2)); 65 sign.insert(pair<char,int>(‘-‘,2)); 66 sign.insert(pair<char,int>(‘*‘,3)); 67 sign.insert(pair<char,int>(‘/‘,3)); 68 sign.insert(pair<char,int>(‘(‘,4)); 69 } 70 void Operator(char p) 71 { 72 int n1=num.gettop(); 73 num.pop(); 74 int n2=num.gettop(); 75 num.pop(); 76 //cout<<n1<<n2<<p<<endl; 77 switch(p) 78 { 79 case ‘*‘:num.push(n1*n2);break; 80 case ‘/‘:num.push(n2/n1);break; 81 case ‘+‘:num.push(n2+n1);break; 82 case ‘-‘:num.push(n2-n1);break; 83 } 84 } 85 int main() 86 { 87 init(); 88 ch.push(‘#‘); 89 num.push(0); 90 cin>>str; 91 len=strlen(str); 92 while(pos<len) 93 { 94 int n=0; 95 bool flag=0; 96 while(str[pos]<=‘9‘&&str[pos]>=‘0‘) 97 { 98 int ch=str[pos]-‘0‘; 99 n=n*10+ch; 100 pos++; 101 flag=1; 102 } 103 if(flag) {num.push(n);continue;} 104 s=str[pos]; 105 st=ch.gettop(); 106 if((st==‘(‘&&s==‘)‘)||(st==‘#‘&&s==‘#‘)) {ch.pop();pos++;continue;} 107 if(st==‘(‘) {ch.push(s);pos++;continue;} 108 if(sign[st]>=sign[s]) {Operator(st);ch.pop();continue;} 109 ch.push(s); 110 pos++; 111 } 112 cout<<num.gettop()<<endl; 113 }
时间: 2024-10-12 14:27:47