feat(bfs, backtracking): new problem formated

This commit is contained in:
2025-11-05 20:44:07 -03:00
parent abefdb4352
commit 3db23b4f54
228 changed files with 6899 additions and 0 deletions

59
sliding-puzzle/src/ac.cpp Normal file
View File

@@ -0,0 +1,59 @@
#include <bits/stdc++.h>
using namespace std;
int N = 6;
unordered_set<string> games;
queue<pair<string, int>> q;
void addGame(int i, int j, string &s)
{
swap(s[i], s[j]);
if (!games.count(s))
q.emplace(s, j);
games.insert(s);
swap(s[i], s[j]);
}
int bfs(string target)
{
q.push({"123450", 5});
int depth = 0;
while (!q.empty())
{
int n = (int)q.size();
while (n--)
{
auto [s, zeroPos] = q.front();
q.pop();
if (s == target)
return depth;
int up = zeroPos - 3, down = zeroPos + 3, left = zeroPos - 1, right = zeroPos + 1;
if (up >= 0)
addGame(zeroPos, up, s);
if (down < N)
addGame(zeroPos, down, s);
if (left >= 0 && zeroPos != 3)
addGame(zeroPos, left, s); // 3 is the down left side
if (right < N && zeroPos != 2)
addGame(zeroPos, right, s); // 2 is the top right side
}
depth++;
}
return -1;
}
int main()
{
vector<string> board(2);
for (int i = 0; i < 2; i++)
{
cin >> board[i];
}
cout << bfs((board[0] + board[1])) << endl;
return 0;
}

View File

@@ -0,0 +1,19 @@
#include "testlib.h"
#include <bits/stdc++.h>
using namespace std;
int main(int argc, char* argv[]) {
setName("compare two signed int%d's", 8 * int(sizeof(int)));
registerTestlibCmd(argc, argv);
int ja = ans.readInt();
int pa = ouf.readInt();
if (ja != pa)
quitf(_wa, "expected %d, found %d", ja, pa);
quitf(_ok, "answer is %d", ja);
}

View File

@@ -0,0 +1,73 @@
#include "testlib.h"
#include <bits/stdc++.h>
using namespace std;
const int MIN_N = 0;
const int MAX_N = 100;
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(string top, string bottom) {
ostringstream oss;
oss << top << endl << bottom << endl;
return oss.str();
}
vector<string> generate_sample_tests() {
vector<string> tests;
tests.push_back(output_tc("412", "503"));
tests.push_back(output_tc("123", "540"));
tests.push_back(output_tc("123", "405"));
return tests;
}
vector<string> generate_manual_tests() {
vector<string> tests;
tests.push_back(output_tc("413", "025"));
tests.push_back(output_tc("152", "403"));
return tests;
}
string rnd_test(int i){
string start = "012345";
while (i-- && next_permutation(start.begin(), start.end())) {}
return output_tc(start.substr(0, 3), start.substr(3));
}
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;
}
string extreme_test_1(){
return(output_tc("123", "450"));
}
vector<string> generate_extreme_tests(){
vector<string> tests;
tests.push_back(extreme_test_1());
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());
append(tests, generate_extreme_tests());
for (const auto &t : tests) {
startTest(++test);
cout << t;
}
return 0;
}

View File

@@ -0,0 +1 @@
generator

5963
sliding-puzzle/src/testlib.h Normal file

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,24 @@
#include "testlib.h"
#include <bits/stdc++.h>
using namespace std;
int main(int argc, char *argv[])
{
registerValidation(argc, argv);
string game = inf.readToken("[0-5]{3}", "top");
inf.readEoln();
game += inf.readToken("[0-5]{3}", "bottom");
inf.readEoln();
inf.readEof();
vector<bool> nums(6, false);
for (char c : game)
{
ensuref(!nums[c - '0'], "Repeated occurence of number %c", c);
nums[c - '0'] = true;
}
return 0;
}