7-116 3207 数字阶梯
这题很有意思啊,写个解析。
观察数字坐标的移动方式,不难发现所有的移动以 ↗↘↗↖ 为一组不断循环,且每次移动后对应坐标的点变为上个点 \(+1\) 。
那么代码就很好模拟了,我们只需要使用 {{1, 1}, {1, -1}, {1, 1}, {-1, 1}} 来模拟每次的移动即可。
#include <algorithm>
#include <cstdint>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <numeric>
#include <string>
#include <utility>
#include <vector>
using i32 = std::int32_t;
using i64 = std::int64_t;
using u32 = std::uint32_t;
using u64 = std::uint64_t;
using pii = std::pair<i32, i32>;
using pll = std::pair<i64, i64>;
using vi = std::vector<i32>;
using vll = std::vector<i64>;
using vpii = std::vector<pii>;
using vpll = std::vector<pll>;
void solve();
int main()
{
std::cin.tie(nullptr)->sync_with_stdio(false);
solve();
}
using move = pii;
using point = pii;
class MoveManager
{
public:
move get_move()
{
++index;
if (index <= 3)
{
return moves[index];
}
index = 0;
return moves[0];
}
private:
i32 index{-1};
const std::vector<move> moves{{1, 1}, {1, -1}, {1, 1}, {-1, 1}};
};
void solve()
{
std::map<point, i32> map;
point current_point{0, 0};
i32 current_value{0};
MoveManager movemanager;
while (current_point.first < 5000 || current_point.second < 5000)
{
map[current_point] = current_value;
auto get = movemanager.get_move();
current_point.first += get.first;
current_point.second += get.second;
++current_value;
}
i32 t;
std::cin >> t;
bool endl = false;
for (i32 i = 1; i <= t; ++i)
{
if (endl)
{
std::cout << '\n';
}
i32 x, y;
std::cin >> x >> y;
if (x == 0 && y == 0)
{
std::cout << 0;
}
else
{
if (map[{x, y}] > 0)
{
std::cout << map[{x, y}];
}
else
{
std::cout << "No Number";
}
}
endl = true;
}
}

浙公网安备 33010602011771号