QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#77287 | #5509. Kooky Tic-Tac-Toe | nweeks | TL | 2ms | 3476kb | C++17 | 3.2kb | 2023-02-13 22:16:43 | 2023-02-13 22:18:26 |
Judging History
answer
#include <bits/stdc++.h>
#define int long long
using namespace std;
string to_string(string s) { return s; }
template <typename T> string to_string(T v) {
bool first = true;
string res = "[";
for (const auto &x : v) {
if (!first)
res += ", ";
first = false;
res += to_string(x);
}
res += "]";
return res;
}
namespace std {
template <typename T1, typename T2> string to_string(pair<T1, T2> v) {
string res = "{" + to_string(v.first) + ", " + to_string(v.second) + "}";
return res;
}
} // namespace std
void dbg_out() { cout << endl; }
template <typename Head, typename... Tail> void dbg_out(Head H, Tail... T) {
cout << ' ' << to_string(H);
dbg_out(T...);
}
#ifdef DEBUG
#define dbg(...) cout << "(" << #__VA_ARGS__ << "):", dbg_out(__VA_ARGS__)
#else
#define dbg(...)
#endif
const pair<int, int> deltas[] = {{0, 1}, {1, 0}, {1, 1}, {1, -1}};
void solve() {
int dim, nbNec;
cin >> dim >> nbNec;
array<vector<pair<int, int>>, 2> moves;
for (int i = 0; i < dim; ++i)
for (int j = 0; j < dim; ++j) {
char c;
cin >> c;
if (c == 'o')
moves[0].emplace_back(i, j);
else if (c == 'x')
moves[1].emplace_back(i, j);
}
vector<string> grid(dim, string(dim, '.'));
auto isSafe = [&](int lig, int col) {
return lig >= 0 and col >= 0 and col < dim and lig < dim;
};
auto checkWin = [&](int lig, int col) {
for (auto [dx, dy] : deltas) {
int l = 0, r = 0;
while (isSafe(lig + (l - 1) * dy, col + (l - 1) * dx) and
grid[lig + (l - 1) * dy][col + (l - 1) * dx] == grid[lig][col])
--l;
while (isSafe(lig + (r + 1) * dy, col + (r + 1) * dx) and
grid[lig + (r + 1) * dy][col + (r + 1) * dx] == grid[lig][col])
++r;
if (r - l + 1 >= nbNec)
return true;
}
return false;
};
int movesL = moves[0].size();
int movesR = moves[1].size();
array<vector<bool>, 2> used;
used[0].resize(movesL);
used[1].resize(movesR);
vector<pair<int, int>> order;
string play = "xo";
auto Gen = [&](auto gen, int curCoup, int joueur) -> bool {
if (curCoup == movesL + movesR) {
cout << "TAK\n";
for (auto [y, x] : order)
cout << y + 1 << ' ' << x + 1 << '\n';
return true;
}
for (int iCoup = 0; iCoup < (int)moves[joueur].size(); ++iCoup) {
if (used[joueur][iCoup])
continue;
auto [lig, col] = moves[joueur][iCoup];
grid[lig][col] = play[joueur];
order.emplace_back(lig, col);
used[joueur][iCoup] = true;
if ((curCoup + 1 == movesL + movesR and
(checkWin(lig, col) or dim * dim == curCoup + 1)) or
(curCoup + 1 < movesL + movesR and !checkWin(lig, col)))
if (gen(gen, curCoup + 1, !joueur))
return true;
used[joueur][iCoup] = false;
grid[lig][col] = '.';
order.pop_back();
}
return false;
};
if (!Gen(Gen, 0, 0) and !Gen(Gen, 0, 1))
cout << "NIE\n";
}
signed main(void) {
ios_base::sync_with_stdio(false);
cin.tie(0);
int nbTests;
cin >> nbTests;
for (int iTest(0); iTest < nbTests; ++iTest)
solve();
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 2ms
memory: 3476kb
input:
7 3 3 x.o xxx o.o 4 3 xx.x ...o ..o. .o.. 3 3 xoo oxx xoo 3 2 xoo oxx xoo 3 3 xox .o. xox 3 2 xo. ..x xo. 3 3 x.. .x. ..x
output:
TAK 1 1 1 3 2 1 3 1 2 2 3 3 2 3 TAK 1 1 2 4 1 2 3 3 1 4 4 2 TAK 1 2 1 1 1 3 2 2 2 1 2 3 3 2 3 1 3 3 NIE NIE NIE NIE
result:
ok correct (7 test cases)
Test #2:
score: -100
Time Limit Exceeded
input:
10000 3 3 x.o xxx o.o 3 3 xoo oxx xoo 3 2 xoo oxx xoo 3 3 xox .o. xox 3 2 xo. ..x xo. 3 2 oox .xo o.x 5 5 xxx.. xxo.x xoo.. xxxox .oooo 3 3 xxx .o. oo. 3 2 x.o xo. ..o 3 2 ..x xxo .o. 3 3 xxo o.. oxo 3 2 oox ..x ... 3 3 xxo ... .ox 3 3 .xo ... oox 3 3 .x. xo. o.o 3 2 o.. xxo .ox 3 2 x.x xoo x.o 3 2 ...