上后谈爱情

导航

 

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;
}

 

posted on 2016-11-02 21:08  上后谈爱情  阅读(154)  评论(0编辑  收藏  举报