#include <iostream>
#include <vector>
using namespace std;
struct Point
{
int x;
int y;
bool is_equal(const Point& other) const
{
return (x == other.x) && (y == other.y);
}
bool operator==(const Point& other) const
{
return is_equal(other);
}
//调试函数
void tostring() const
{
std::cout << "(" << x << ", " << y << ")";
}
};
const int INF = 11;
struct M
{
int a;
int b;
int x = a + 1;
int y = b + 1;
vector<vector<int>> m = vector<vector<int>>(x,vector<int>(y,INF)); // map
// vector<vector<int>> m(x,vector<int>(y,INF));
void tostring() const
{
for (int i = 0; i < x; i++)
{
for (int j = 0; j < y; j++)
{
std::cout << m[i][j] << ' ';
}
std::cout << '\n';
}
}
};
auto get_banned_points(Point horse)
{
/** To get the control points of horse
* @param Point horse
* @return vector<Point> out
*/
// Point offs[] =
// {
// {-2, 1}, {-1, 2}, {1, 2}, {2, 1},
// {-2, -1}, {-1, -2}, {1, -2}, {2,-1},
// };
// vector<Point> offsets(offs,offs+8);
//偏移量
vector<Point> offsets =
{
{-2, 1}, {-1, 2}, {1, 2}, {2, 1},
{-2, -1}, {-1, -2}, {1, -2}, {2,-1},
{0, 0},
};
vector<Point> out;
for (const auto &p : offsets)
{
out.push_back
({
horse.x+p.x, horse.y+p.y
});
}
return out;
}
auto set_banned_points(const M &_m, const vector<Point> &_banned_points)
{
/**
* to set banned points to the map
* @param M map, vector<Point> &_banned_points
* @return M new_map
*/
auto out = _m;
//out.tostring();
cout << '\n';
for (auto &&p : _banned_points)
{
if (
( p.x >= 0 ) && ( p.y >= 0)
&& (p.x <= _m.x) && (p.y <= _m.y)
)
{
out.m[p.x][p.y] = -1;
}
}
return out;
}
static int cnt; //路线计数
void dfs( M &_m, int current_x, int current_y)
{
/**
* 深度优先算法
* @param M map, Point current_point
* @return count
*/
//判断是否越界
if (
(current_x > _m.x) || (current_y > _m.y)
|| (current_x < 0) || (current_y < 0)
)
{
return;
}
//达到终点的情况
if (
(current_x == _m.x)
&& (current_y == _m.y)
)
{
cnt ++;
return;
}
//碰到控制点的情况
if (_m.m[current_x][current_y] == -1)
return;
// int cnt = 0;
// cnt += dfs(_m,current_x+1,current_y);
// cnt += dfs(_m,current_x,current_y+1);
dfs(_m,current_x+1,current_y);
dfs(_m,current_x,current_y+1);
// return cnt;
}
int main()
{
int bx, by, hx, hy; //B点坐标 马坐标
//int cnt;
vector<Point> banned_points;
//cin >> bx >> by >> hx >> hy;
// input test data
bx = 6;
by = 6;
hx = 3;
hy = 3;
Point B = {bx, by}; //B点
Point H = {hx, hy}; //马
M m = {bx, by}; //地图
banned_points = get_banned_points(H);
m = set_banned_points(m, banned_points);
m.tostring();
//cnt = dfs(m,0,0);
dfs(m,0,0);
cout << cnt;
// TEST CODE
// M map = {10, 10};
// map.tostring();
/** 这是象的代码
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
banned_points.push_back({
( (hx-2) + (i*2) ),
( (hy-2) + (j*2) ),
});
}
}*/
//输出点
// for (const auto& p : banned_points)
// {
// p.tostring();
// cout << ' ';
// }
// for (int i = 0; i <= bx; i++)
// {
// for (int j = 0; j <= by; j++)
// {
// Point current_point = {i, j}
// auto it = find(
// banned_points.begin(),
// banned_points.end(),
// current_point);
// if (it != banned_points.end())
// {
// break;
// }
// }
// }
return 0;
}
#include <iostream>
#include <vector>
using namespace std;
struct Point
{
int x;
int y;
bool is_equal(const Point& other) const
{
return (x == other.x) && (y == other.y);
}
bool operator==(const Point& other) const
{
return is_equal(other);
}
//调试函数
void tostring() const
{
std::cout << "(" << x << ", " << y << ")";
}
};
const int INF = 11;
struct M
{
int a;
int b;
int x = a + 1;
int y = b + 1;
vector<vector<int>> m = vector<vector<int>>(x,vector<int>(y,INF)); // map
// vector<vector<int>> m(x,vector<int>(y,INF));
void tostring() const
{
for (int i = 0; i < x; i++)
{
for (int j = 0; j < y; j++)
{
std::cout << m[i][j] << ' ';
}
std::cout << '\n';
}
}
};
auto get_banned_points(Point horse)
{
/** To get the control points of horse
* @param Point horse
* @return vector<Point> out
*/
// Point offs[] =
// {
// {-2, 1}, {-1, 2}, {1, 2}, {2, 1},
// {-2, -1}, {-1, -2}, {1, -2}, {2,-1},
// };
// vector<Point> offsets(offs,offs+8);
//偏移量
vector<Point> offsets =
{
{-2, 1}, {-1, 2}, {1, 2}, {2, 1},
{-2, -1}, {-1, -2}, {1, -2}, {2,-1},
{0, 0},
};
vector<Point> out;
for (const auto &p : offsets)
{
out.push_back
({
horse.x+p.x, horse.y+p.y
});
}
return out;
}
auto set_banned_points(const M &_m, const vector<Point> &_banned_points)
{
/**
* to set banned points to the map
* @param M map, vector<Point> &_banned_points
* @return M new_map
*/
auto out = _m;
//out.tostring();
cout << '\n';
for (auto &&p : _banned_points)
{
if (
( p.x >= 0 ) && ( p.y >= 0)
&& (p.x <= _m.x) && (p.y <= _m.y)
)
{
out.m[p.x][p.y] = -1;
}
}
return out;
}
static int cnt; //路线计数
void dfs( M &_m, int current_x, int current_y)
{
/**
* 深度优先算法
* @param M map, Point current_point
* @return count
*/
//判断是否越界
if (
(current_x > _m.x) || (current_y > _m.y)
|| (current_x < 0) || (current_y < 0)
)
{
return;
}
//达到终点的情况
if (
(current_x == _m.x)
&& (current_y == _m.y)
)
{
cnt ++;
return;
}
//碰到控制点的情况
if (_m.m[current_x][current_y] == -1)
return;
// int cnt = 0;
// cnt += dfs(_m,current_x+1,current_y);
// cnt += dfs(_m,current_x,current_y+1);
dfs(_m,current_x+1,current_y);
dfs(_m,current_x,current_y+1);
// return cnt;
}
int main()
{
int bx, by, hx, hy; //B点坐标 马坐标
//int cnt;
vector<Point> banned_points;
cin >> bx >> by >> hx >> hy;
// input test data
//bx = 6;
//by = 6;
//hx = 3;
//hy = 3;
Point B = {bx, by}; //B点
Point H = {hx, hy}; //马
M m = {bx, by}; //地图
banned_points = get_banned_points(H);
m = set_banned_points(m, banned_points);
m.tostring();
//cnt = dfs(m,0,0);
dfs(m,0,0);
cout << cnt;
return 0;
}
输入 6 6 3 3
输出40
应该输出6