• 堆栈


    1.堆栈原理:先进后出,主要这种思想,对于堆栈而言,主要运用在二叉树的中序遍历,前序遍历;在递归中需要记录子程序的值和运行方式;深度DFS;中断处理

    最重点问题:Mouse 走迷宫问题:堆栈在其中最重要作用记录走过的路径,当走到死胡同时候在依次回溯到上一个分叉点,堆栈中最终要点在于记录在正确路径上的分叉点。

    package Static;
    
    public class CH04_5 {
    
        public static int Exitx=8;
        public static int Exity=10;
        
        public static int [][]MAZE={{1,1,1,1,1,1,1,1,1,1,1,1},
                                    {1,0,0,0,1,1,1,1,1,1,1,1},
                                    {1,1,1,0,1,1,0,0,0,0,1,1},
                                    {1,1,1,0,1,1,0,1,1,0,1,1},
                                    {1,1,1,0,0,0,0,1,1,0,1,1},
                                    {1,1,1,0,1,1,0,1,1,0,1,1},
                                    {1,1,1,0,1,1,0,1,1,0,1,1},
                                    {1,1,1,1,1,1,0,1,1,0,1,1},
                                    {1,1,0,0,0,0,0,0,1,0,0,1},
                                    {1,1,1,1,1,1,1,1,1,1,1,1}};
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            //第一步选择方向,同时将已经走过路径记录为2
            TraceRecord path=new TraceRecord();
            int x=1,y=1;//入口在1,1位置上
            
            //Mouse 不断地进行遍历
            while(x<=Exitx && y<=Exity)
            {
                MAZE[x][y]=2;//走过路程在用2表示
                if(MAZE[x-1][y]==0)
                {
                    x-=1;
                    path.insert(x, y);
                }
                else if(MAZE[x+1][y]==0)
                {
                    x+=1;
                    path.insert(x, y);
                }
                else if(MAZE[x][y-1]==0)
                {
                    y-=1;
                    path.insert(x, y);
                }
                else if(MAZE[x][y+1]==0)
                {
                    y+=1;
                    path.insert(x, y);
                }
                else if(chkExit(x,y,Exitx,Exity)==1)
                {
                    break;//是否到了死胡同
                }
                else{
                    MAZE[x][y]=2;
                    path.delet();
                    x=path.last.x;
                    y=path.last.y;
                }
            }
            for(int i=0;i<10;i++)
            {
                for (int j=0;j<12;j++)
                {
                    System.out.print(MAZE[i][j]);
                }
                System.out.println();
            }
        }
        public static int chkExit(int x,int y,int Ex,int Ey)
        {
            //判断其刚好到了出口位置,不是的话在死胡同直接返回
            if(x==Ex && y==Ey)
            {
                if(MAZE[x-1][y]==1 || MAZE[x+1][y]==1 || MAZE[x][y-1]==1 || MAZE[x][y+1]==2)
                    return 1;
                if(MAZE[x-1][y]==1 || MAZE[x+1][y]==1 || MAZE[x][y-1]==2 || MAZE[x][y+1]==1)
                    return 1;
                if(MAZE[x-1][y]==1 || MAZE[x+1][y]==2 || MAZE[x][y-1]==1 || MAZE[x][y+1]==1)
                    return 1;
                if(MAZE[x-1][y]==2 || MAZE[x+1][y]==1 || MAZE[x][y-1]==1 || MAZE[x][y+1]==1)
                    return 1;
            }
            //d都不符合条件直接返回
            return 0;
            
        }
    
    }

     八皇后问题:

    #include<cstdio>
    #include<iostream>
    using namespace std;
    #define EIGHT 8
    //回溯法模拟八皇后问题,新放置位置与前位置进行比较
    int Queen[EIGHT];int numble=0;
    //Queen[x]放的行的位置
    void print_stack()
    {
        int x=0,y=0;numble+=1;
        cout<<"八皇后的解"<<numble<<endl;
        for (x=0;x<EIGHT;x++)
        {
            for(y=0;y<EIGHT;y++)
                if(x==Queen[y])
                    cout<<"<*>"<<" ";
                else
                    cout<<"<->"<<" ";
            cout<<endl;
    
        }
    
    }
    int attack(int row, int col)
    {
        int alt=0;int i=0;
        int off_row=0,off_col=0;//判断其是否在对角线上
        while(i<col && alt!=1)
        {
            off_row=abs(Queen[i]-row);
            off_col=abs(i-col);
            if(off_col==off_row || row==Queen[i])
                
                {    alt=1;
                        break;
               }
            i++;
    
        }
        return alt;
    }
    void Trace(int value)
    {
        //每一行只能放置一位皇后,新来的皇后与老皇后进行比较
        //只有当找到位置才能够在往新的一行放置新的皇后
        int i=0;
        while(i<EIGHT)
        {
            //判断新放的位置是否会受到攻击
            if(attack(i,value)!=1)
            {
                Queen[value]=i;
                if(value==7)
                {
                    print_stack();
                }
                else
                        {    Trace(value+1);
                }
            }
            i++;
        }
    }
    
    int main()
    {
        Queen[0]=0;
        Trace(0);
        return 0;
    }
  • 相关阅读:
    求连续子数组的最大和
    【LeetCode练习题】Gas Station
    再来看看快速排序
    【LeetCode练习题】First Missing Positive
    【LeetCode练习题】Merge Sorted Array
    Hdu 2089-不要62 数位dp
    Tsinsen A1517. 动态树 树链剖分,线段树,子树操作
    Bzoj 3505: [Cqoi2014]数三角形 数论
    Poj 3695-Rectangles 矩形切割
    Tsinsen A1505. 树(张闻涛) 倍增LCA,可持久化线段树,DFS序
  • 原文地址:https://www.cnblogs.com/woainifanfan/p/6017611.html
Copyright © 2020-2023  润新知