#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;
}
int push(ElementType value)
{
if(this->full())
return FAILURE;
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;
}
int full()
{
return topOfStack == STACK_SIZE - 1;
}
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:
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);
}
};
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)
return vector<int>({input.begin(), input.begin() + k});
else if (p + 1 < k)
left = p;
else
right = p - 1;
}
return ret;
}
};
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)
return input[p];
else if (p + 1 < k)
left = p;
else
right = 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;
int root_val = postorder[post_idx];
TreeNode *root = new TreeNode(root_val);
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;
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;
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;
}