牛客国庆集训派对Day3 A Knight

时间:2022-11-13 20:27:52

Knight

思路:

bfs打表找规律

如下图

牛客国庆集训派对Day3 A Knight

代码:

#pragma GCC optimize(2)
#pragma GCC optimize(3)
#pragma GCC optimize(4)
#include<bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define pi acos(-1.0)
#define LL long long
//#define mp make_pair
#define pb push_back
#define ls rt<<1, l, m
#define rs rt<<1|1, m+1, r
#define ULL unsigned LL
#define pll pair<LL, LL>
#define pii pair<int, int>
#define piii pair<pii, int>
#define mem(a, b) memset(a, b, sizeof(a))
#define fio ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define fopen freopen("in.txt", "r", stdin);freopen("out.txt", "w", stout);
//head int n, m;
int dir[][] = {
, , , , , -, -, , -, , , -, -, -, -, -
};
int res[][] = {
{, , , , },
{, , , , },
{, , , , },
{, , , , },
{, , , , }
};
map<pii, bool> vis;
int bfs() {
queue<piii> q;
q.push(piii{{, }, });
vis.clear();
vis[pii{, }] = true;
while(!q.empty()) {
piii now = q.front();
q.pop();
if(now.fi.fi == n && now.fi.se == m) return now.se;
for (int i = ; i < ; i++) {
int x = now.fi.fi + dir[i][];
int y = now.fi.se + dir[i][];
if(vis.find(pii{x, y}) == vis.end()) {
vis[pii{x, y}] = true;
q.push({{x, y}, now.se+});
}
}
}
return ;
}
int main() {
int T;
// for (n = 0; n <= 20; n++) {
// for (m = 0; m <= 20; m++) {
// cout<< setw(4) << bfs();
// }
// cout << endl;
// }
scanf("%d", &T);
while(T--) {
scanf("%d %d", &n, &m);
n = abs(n);
m = abs(m);
if(n <= && m <= ) {
printf("%d\n", res[n][m]);
continue;
}
if(n > m) swap(n, m);
if(m <= *n) {
printf("%d\n", (m+n)/ + (m+n)%);
}
else {
if(n == ) printf("%d\n", (m+)/* + m%);
else printf("%d\n", (m-*n)/* + n + (m-*n)%);
}
}
return ;
}