mylib

#include <iostream>
#include <vector>
#include <queue>
#include <unordered_map>
#include <map>
#include <algorithm>
#include <math.h>
#include <stack>
using namespace std;

//字符串分割
void SplitString(const std::string &s, std::vector<std::string> &vs, const std::string &patten)
{
    std::string::size_type pos1, pos2;
    pos2 = s.find(patten);
    pos1 = 0;
    while (std::string::npos != pos2)
    {
        vs.push_back(s.substr(pos1, pos2 - pos1));

        pos1 = pos2 + patten.size();
        pos2 = s.find(patten, pos1);
    }
    if (pos1 != s.length())
        vs.push_back(s.substr(pos1));
}

//单例模式
class Singleton
{
public:
    ~Singleton() { std::cout << "destructor called!" << std::endl; }
    Singleton(const Singleton &) = delete;
    Singleton &operator=(const Singleton &) = delete;
    static Singleton &get_instance()
    {
        static Singleton instance;
        return instance;
    }

    int main()
    {
        Singleton &instance_1 = Singleton::get_instance();
        Singleton &instance_2 = Singleton::get_instance();
        return 0;
    }
private:
    Singleton() { std::cout << "constructor called!" << std::endl; }
};

//使用数组实现栈
class mystack
{
private:
    typedef int ElementType; /*栈元素类型*/
    #define SUCCESS 1
    #define FAILURE -1
    #define STACK_SIZE 64 /*栈大小*/
    #define TOP_OF_STACK -1 /*栈顶位置*/
    int topOfStack ; /*记录栈顶位置*/
    ElementType stack[STACK_SIZE]; /*栈数组,也可以使用动态数组实现*/

public:
    /*构造函数*/
    mystack()
    {
        topOfStack = TOP_OF_STACK;//初始化栈顶
    }

    /*入栈,0表示成功,-1表示出错*/
    int push(ElementType value)
    {
        if(this->full())
            return FAILURE;
        /*先增加topOfStack,再赋值*/
        topOfStack++;
        stack[topOfStack] = value;
        return SUCCESS;
    }

    /*出栈*/
    int pop()
    {
        /*首先判断栈是否为空*/
        if(this->empty())
            return FAILURE;        
        topOfStack--;
        return SUCCESS;
    }

    /*访问栈顶元素*/
    ElementType top()
    {
        /*首先判断栈是否为空*/
        if(this->empty()){
            std::cerr << "stack is empty! can't get top element" << '\n';
            return FAILURE;
        }        
        ElementType value = stack[topOfStack];
        return value;
    }

    /*判断栈是否已满,满返回1,未满返回0*/
    int full()
    {
        return topOfStack == STACK_SIZE - 1;
    }

    /*判断栈是否为空,空返回1,非空返回0*/
    int empty()
    {
        return topOfStack ==  - 1;
    }
};

//使用数组实现队列(实际使用循环队列)
class myqueue
{
private:
    typedef int ElementType;
    #define MAX_SIZE 5    
    #define SUCCESS 1
    #define FAILURE -1

    /*定义队列结构*/
    int front; //队头位置
    int rear;  //队尾位置
    ElementType queueArr[MAX_SIZE]={};//队列数组

public:
    myqueue()
    {
        front = 1;
        rear = 0;
    }

    /*判断队列是否已满*/
    bool full()
    {
        if((rear + 2) % MAX_SIZE == front)
        {
            printf("queue is full\n");
            return true;
        }
        else 
            return false;
    }

    /*判断队列是否为空*/
    bool empty()
    {
        if((rear + 1) % MAX_SIZE == front)
        {
            printf("queue is empty\n");
            return true;
        }
        else 
            return false;
    }

    /*出队*/
    int pop()
    {
        if(this->empty())
            return FAILURE;
        front = (front + 1) % MAX_SIZE;
        return SUCCESS;
    }

    /*入队*/
    int push(ElementType value)
    {
        if(this->full())
            return FAILURE;
        rear = (rear + 1) % MAX_SIZE;
        queueArr[rear] = value;   
        return SUCCESS;
    }

    ElementType getfront(){
        return queueArr[front];
    }

};

//归并排序
class mergeSort
{
public:
    // 构造函数 参数为要排序的vector
    mergeSort(std::vector<int> &arr){
        dfs(arr, 0, arr.size() - 1);
    }

    // 合并过程
    void merge__(vector<int> &arr, int l, int mid, int r) {
        vector<int> tmp(r - l + 1);
        int i = l, j = mid + 1, k = 0;

        while (i <= mid && j <= r) {
            if (arr[i] >= arr[j]) {
                tmp[k++] = arr[j++];
            }
            else {
                tmp[k++] = arr[i++];
            }
        }

        while (i <= mid) {
            tmp[k++] = arr[i++];
        }
        while (j <= r) {
            tmp[k++] = arr[j++];
        }

        for (k = 0, i = l; i <= r; ++i, ++k) {
            arr[i] = tmp[k];
        }
    }

    // 递归划分过程
    void dfs(vector<int> &arr, int l, int r) {
        // 只有一个数字,则停止划分
        if (l >= r) {
            return;
        }
        int mid = l + ((r - l) >> 1);
        dfs(arr, l, mid);      //划分左
        dfs(arr, mid + 1, r);  //划分右
        // 合并两个有序区间
        merge__(arr, l, mid, r);
    }
};

//快排
class quick_sort
{
public:
    int partition(vector<int> &A, int left, int right)
    {
        int i = left;
        for (int j = left; j <= right - 1; j++)
        {
            if (A[j] <= A[right])
            {
                swap(A[i], A[j]);
                i++;
            }
        }
        swap(A[i], A[right]);
        return i;
    }
    void quicksort(vector<int> &A, int left, int right)
    {
        if (right <= left)
            return;
        int q = partition(A, left, right);
        quicksort(A, left, q - 1);
        quicksort(A, q + 1, right);
    }
    quick_sort(vector<int> &A, int left, int right)
    {
        quicksort(A, left, right);
    }
};

//最小的k个数 使用快排
class Solution
{
public:
    int partition(vector<int> &A, int left, int right)
    {
        int i = left;
        for (int j = left; j <= right - 1; j++)
        {
            if (A[j] <= A[right])
            {
                swap(A[i], A[j]);
                i++;
            }
        }
        swap(A[i], A[right]);
        return i;
    }
    vector<int> GetLeastNumbers_Solution(vector<int> input, int k)
    {
        vector<int> ret;
        if (k == 0 || k > input.size())
            return ret;
        int left = 0, right = input.size() - 1;
        while (left <= right)
        {
            int p = partition(input, left, right);
            if (p + 1 == k) //p为第k小的数
                return vector<int>({input.begin(), input.begin() + k});
            else if (p + 1 < k)
                left = p;
            else
                right = p - 1;
        }
        return ret;
    }
};

//寻找第K大 使用快排思路
class Solution
{
public:
    int partition(vector<int> &A, int left, int right)
    {
        int i = left;
        for (int j = left; j <= right - 1; j++)
        {
            if (A[j] >= A[right])
            { //从大到小排列
                swap(A[i], A[j]);
                i++;
            }
        }
        swap(A[i], A[right]);
        return i;
    }
    int findKth(vector<int> input, int n, int k)
    {
        if (k == 0 || k > input.size())
            return 0;
        int left = 0, right = input.size() - 1;
        while (left <= right)
        {
            int p = partition(input, left, right);
            if (p + 1 == k) //p为第k大的数
                return input[p];
            else if (p + 1 < k) //p在K前面,从p开始继续排序
                left = p;
            else
                right = p - 1; //p在k后面,从之前到p-1排序
        }
        return 0;
    }
};

//重建二叉树(前序和中序)
class Solution
{
private:
    struct TreeNode
    {
        int val;
        TreeNode *left;
        TreeNode *right;
        TreeNode(int x) : val(x), left(NULL), right(NULL) {}
    };

public:
    int p = 0;
    TreeNode *rebuild(vector<int> preorder, vector<int> inorder, int vleft, int vright)
    {
        for (int i = vleft; i < vright; i++)
        {
            if (inorder[i] == preorder[p])
            {
                int root = i;
                p++;
                TreeNode *tree = new TreeNode(inorder[root]);

                tree->left = rebuild(preorder, inorder, vleft, root);
                tree->right = rebuild(preorder, inorder, root, vright);
                return tree;
            }
        }
        return NULL;
    }
    TreeNode *reConstructBinaryTree(vector<int> preorder, vector<int> inorder)
    {
        TreeNode *res = rebuild(preorder, inorder, 0, inorder.size());
        return res;
    }
};

//重建二叉树(中序和后序)
class Solution
{
    int post_idx;
    unordered_map<int, int> idx_map;
    struct TreeNode
    {
        int val;
        TreeNode *left;
        TreeNode *right;
        TreeNode(int x) : val(x), left(NULL), right(NULL) {}
    };

public:
    TreeNode *rebuild( vector<int> inorder, vector<int> postorder,int in_left, int in_right)
    {
        // 如果这里没有节点构造二叉树了,就结束
        if (in_left > in_right)
            return nullptr;        

        // 选择 post_idx 位置的元素作为当前子树根节点
        int root_val = postorder[post_idx];
        TreeNode *root = new TreeNode(root_val);

        // 根据 root 所在位置分成左右两棵子树
        int index = idx_map[root_val];

        // 下标减一
        post_idx--;
        // 构造右子树
        root->right = rebuild( inorder, postorder,index + 1, in_right);
        // 构造左子树
        root->left = rebuild(inorder, postorder,in_left, index - 1);
        return root;
    }
    TreeNode *buildTree(vector<int> inorder, vector<int> postorder)
    {
        // 从后序遍历的最后一个元素开始
        post_idx = (int)postorder.size() - 1;

        // 建立(元素,下标)键值对的哈希表
        int idx = 0;
        for (auto &val : inorder)
        {
            idx_map[val] = idx++;
        }
        return rebuild( inorder, postorder,0, (int)inorder.size() - 1);
    }
};

//素数筛法
vector<bool> eratosthenes(int num)
{
    vector<bool> flag(num + 1, true);
    flag[0] = flag[1] = false; //exclude 0 and 1
    for (int i = 2; i * i <= num; ++i)
    {
        if (flag[i])
        {
            for (int j = i * i; j <= num; j += i)
                flag[j] = false;
        }
    }
    return flag;
}

#include <iostream> 
using namespace std;
 
// int main() 
// { 
//     int a,b; 
//     freopen("in.txt","r",stdin); //输入重定向,输入数据将从in.txt文件中读取
//     freopen("out.txt","w",stdout); //输出重定向,输出数据将保存在out.txt文件中
//     while(cin>> a >> b) 
//         cout<< a+b <<endl; // 注意使用endl
//     fclose(stdin);//关闭文件
//     fclose(stdout);//关闭文件
//     return 0; 
// }

struct node
{
    int x;
    int y;
    int step;
    node():x(0),y(0),step(0){}
    node(int xx,int yy,int st){
        x=xx;
        y=yy;
        step=st;
    }
};

class myqueue
{
private:
    typedef node ElementType;
    #define MAX_SIZE 10000    
    #define SUCCESS 1
    #define FAILURE -1

    /*定义队列结构*/
    int front; //队头位置
    int rear;  //队尾位置
    ElementType queueArr[MAX_SIZE]={};//队列数组

public:
    myqueue()
    {
        front = 1;
        rear = 0;        
    }

    /*判断队列是否已满*/
    bool full()
    {
        if((rear + 2) % MAX_SIZE == front)
        {
            printf("queue is full\n");
            return true;
        }
        else 
            return false;
    }

    /*判断队列是否为空*/
    bool empty()
    {
        if((rear + 1) % MAX_SIZE == front)
        {
            printf("queue is empty\n");
            return true;
        }
        else 
            return false;
    }

    /*出队*/
    int pop()
    {
        if(this->empty())
            return FAILURE;
        front = (front + 1) % MAX_SIZE;
        return SUCCESS;
    }

    /*入队*/
    int push(ElementType value)
    {
        if(this->full())
            return FAILURE;
        rear = (rear + 1) % MAX_SIZE;
        queueArr[rear] = value;   
        return SUCCESS;
    }

    ElementType getfront(){
        return queueArr[front];
    }

};

int main(void)
{
    /*队列初始化*/
    myqueue queue;
    int dirx[4]={0,0,1,-1};
    int diry[4]={1,-1,0,0};
    int used[8][8]={0};
    int arr[8][8]={
        {0,0,0,0,0,0,0,0},
        {0,0,0,1,1,1,1,0},
        {0,0,0,1,0,0,0,0},
        {1,1,0,1,0,1,1,0},
        {0,0,0,1,0,0,1,1},
        {0,0,1,1,0,0,0,0},
        {0,0,0,0,0,1,1,0},
        {0,0,0,0,0,1,0,0}
    };
    
    used[0][0]=1;
    queue.push(node(0,0,1));
    while(!queue.empty()){
        node now = queue.getfront();
        queue.pop();
        if(now.x==7 && now.y==7){
            cout<<now.step;
            break;
        }
        for(int i=0;i<4;i++){
            int dx = now.x + dirx[i];
            int dy = now.y + diry[i];
            if(dx<0||dy<0||dx>=8||dy>=8||used[dx][dy]==1||arr[dx][dy]==1)
                continue;
            used[dx][dy] = 1;
            int v = now.step;
            if(arr[dx][dy]==0)v++;
            queue.push(node(dx,dy,v));

        }
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值