feat(bfs): new problem formated
This commit is contained in:
52
knight-moves/src/ac.cpp
Normal file
52
knight-moves/src/ac.cpp
Normal file
@@ -0,0 +1,52 @@
|
||||
#include <bits/stdc++.h>
|
||||
|
||||
using namespace std;
|
||||
|
||||
int bfs(const string &start, const string &end)
|
||||
{
|
||||
int rowS = start[1] - '1', colS = start[0] - 'a';
|
||||
int rowE = end[1] - '1', colE = end[0] - 'a';
|
||||
|
||||
queue<pair<pair<int, int>, int>> q;
|
||||
vector<vector<bool>> vis(8, vector<bool>(8, false));
|
||||
|
||||
q.push({{rowS, colS}, 0});
|
||||
vis[rowS][colS] = true;
|
||||
|
||||
while (!q.empty())
|
||||
{
|
||||
auto [pos, cost] = q.front(); q.pop();
|
||||
auto [row, col] = pos;
|
||||
|
||||
if (row == rowE && col == colE)
|
||||
return cost;
|
||||
|
||||
vector<pair<int, int>> moves = {{-2, -1}, {-2, 1}, {-1, 2}, {1, 2}, {2, 1}, {2, -1}, {-1, -2}, {1, -2}};
|
||||
for (const auto [dy, dx] : moves)
|
||||
{
|
||||
int nextRow = row + dy, nextCol = col + dx;
|
||||
if (nextRow < 0 || nextRow > 7 || nextCol < 0 || nextCol > 7) continue;
|
||||
|
||||
if (!vis[nextRow][nextCol])
|
||||
{
|
||||
q.push({{nextRow, nextCol}, cost + 1});
|
||||
vis[nextRow][nextCol] = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
int t;
|
||||
cin >> t;
|
||||
while (t--)
|
||||
{
|
||||
string start, end;
|
||||
cin >> start >> end;
|
||||
cout << bfs(start, end) << endl;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
21
knight-moves/src/checker.cpp
Normal file
21
knight-moves/src/checker.cpp
Normal file
@@ -0,0 +1,21 @@
|
||||
#include "testlib.h"
|
||||
#include <bits/stdc++.h>
|
||||
|
||||
using namespace std;
|
||||
|
||||
int main(int argc, char* argv[]) {
|
||||
setName("compare two signed int%d's line by line", 8 * int(sizeof(int)));
|
||||
registerTestlibCmd(argc, argv);
|
||||
|
||||
int T = inf.readInt();
|
||||
|
||||
for (int t = 0; t < T; t++) {
|
||||
int ja = ans.readInt();
|
||||
int pa = ouf.readInt();
|
||||
|
||||
if (ja != pa)
|
||||
quitf(_wa, "on test %d: expected %d, found %d", t + 1, ja, pa);
|
||||
}
|
||||
|
||||
quitf(_ok, "all answers are correct");
|
||||
}
|
||||
69
knight-moves/src/generator.cpp
Normal file
69
knight-moves/src/generator.cpp
Normal file
@@ -0,0 +1,69 @@
|
||||
#include "testlib.h"
|
||||
#include <bits/stdc++.h>
|
||||
|
||||
using namespace std;
|
||||
|
||||
const int MIN_N = 1;
|
||||
const int MAX_N = 4096;
|
||||
|
||||
const int rnd_test_n = 100;
|
||||
|
||||
template <typename T> void append(vector<T> &dest, const vector<T> &orig) {
|
||||
dest.insert(dest.end(), orig.begin(), orig.end());
|
||||
}
|
||||
|
||||
string output_tc(const vector<string> &positions) {
|
||||
ostringstream oss;
|
||||
oss << (int)positions.size() << endl;
|
||||
for (const string &pos : positions) {
|
||||
oss << pos << endl;
|
||||
}
|
||||
return oss.str();
|
||||
}
|
||||
|
||||
vector<string> generate_sample_tests() {
|
||||
vector<string> tests;
|
||||
tests.push_back(output_tc({"a1 h8", "a1 c2", "h8 c3"}));
|
||||
return tests;
|
||||
}
|
||||
|
||||
vector<string> generate_manual_tests() {
|
||||
vector<string> tests;
|
||||
tests.push_back(output_tc({"a1 a1"}));
|
||||
tests.push_back(output_tc({"b5 g3"}));
|
||||
return tests;
|
||||
}
|
||||
|
||||
string rnd_test(int i){
|
||||
int cases = rnd.next(MIN_N, MAX_N);
|
||||
vector<string> positions;
|
||||
while (cases--) {
|
||||
string start = rnd.next("[a-h][1-8]");
|
||||
string end = rnd.next("[a-h][1-8]");
|
||||
positions.push_back(start + " " + end);
|
||||
}
|
||||
|
||||
return output_tc(positions);
|
||||
}
|
||||
|
||||
vector<string> generate_random_tests() {
|
||||
vector<string> tests;
|
||||
for (int i = 0; i < rnd_test_n; i++){
|
||||
tests.push_back(rnd_test(i));
|
||||
}
|
||||
return tests;
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
registerGen(argc, argv, 1);
|
||||
vector<string> tests;
|
||||
size_t test = 0;
|
||||
append(tests, generate_sample_tests());
|
||||
append(tests, generate_manual_tests());
|
||||
append(tests, generate_random_tests());
|
||||
for (const auto &t : tests) {
|
||||
startTest(++test);
|
||||
cout << t;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
1
knight-moves/src/script.sh
Normal file
1
knight-moves/src/script.sh
Normal file
@@ -0,0 +1 @@
|
||||
generator
|
||||
5963
knight-moves/src/testlib.h
Normal file
5963
knight-moves/src/testlib.h
Normal file
File diff suppressed because it is too large
Load Diff
24
knight-moves/src/validator.cpp
Normal file
24
knight-moves/src/validator.cpp
Normal file
@@ -0,0 +1,24 @@
|
||||
#include "testlib.h"
|
||||
#include <bits/stdc++.h>
|
||||
|
||||
using namespace std;
|
||||
|
||||
|
||||
int main(int argc, char* argv[]) {
|
||||
registerValidation(argc, argv);
|
||||
int T = inf.readInt(1, 4096, "T");
|
||||
inf.readEoln();
|
||||
|
||||
for (int i = 1; i <= T; i++) {
|
||||
setTestCase(i);
|
||||
|
||||
string start = inf.readToken("[a-h][1-8]", "start");
|
||||
inf.readSpace();
|
||||
string end = inf.readToken("[a-h][1-8]", "end");
|
||||
inf.readEoln();
|
||||
}
|
||||
|
||||
inf.readEof();
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user