QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#119156 | #5509. Kooky Tic-Tac-Toe | hos_lyric | AC ✓ | 32ms | 3752kb | C++14 | 4.4kb | 2023-07-05 01:55:03 | 2023-07-05 01:55:04 |
Judging History
answer
#include <cassert>
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
using namespace std;
using Int = long long;
template <class T1, class T2> ostream &operator<<(ostream &os, const pair<T1, T2> &a) { return os << "(" << a.first << ", " << a.second << ")"; };
template <class T> ostream &operator<<(ostream &os, const vector<T> &as) { const int sz = as.size(); os << "["; for (int i = 0; i < sz; ++i) { if (i >= 256) { os << ", ..."; break; } if (i > 0) { os << ", "; } os << as[i]; } return os << "]"; }
template <class T> void pv(T a, T b) { for (T i = a; i != b; ++i) cerr << *i << " "; cerr << endl; }
template <class T> bool chmin(T &t, const T &f) { if (t > f) { t = f; return true; } return false; }
template <class T> bool chmax(T &t, const T &f) { if (t < f) { t = f; return true; } return false; }
int N, K;
char A[10][10];
bool solve() {
vector<pair<int, int>> pss[2];
for (int x = 0; x < N; ++x) for (int y = 0; y < N; ++y) {
const int h = string("ox").find(A[x][y]);
if (~h) {
pss[h].emplace_back(x, y);
}
}
int psLens[2];
for (int h = 0; h < 2; ++h) {
psLens[h] = pss[h].size();
}
bool wins[2] = {};
Int ands[2];
for (int h = 0; h < 2; ++h) {
ands[h] = (1LL << (N * N)) - 1;
}
for (int x = 0; x < N; ++x) for (int y = 0; y < N; ++y) {
auto seek = [&](int dx, int dy) -> void {
bool ok = true;
Int mask = 0;
for (int k = 0; k < K; ++k) {
const int xx = x + k * dx;
const int yy = y + k * dy;
if (0 <= xx && xx < N && 0 <= yy && yy < N) {
ok = ok && (A[x][y] == A[xx][yy]);
mask |= 1LL << (xx * N + yy);
} else {
ok = false;
}
}
if (ok) {
const int h = string("ox").find(A[x][y]);
if (~h) {
wins[h] = true;
ands[h] &= mask;
}
}
};
seek(1, 0);
seek(1, 1);
seek(0, 1);
seek(-1, 1);
}
if (wins[0] && wins[1]) {
// cerr<<"both wins"<<endl;
return false;
}
if (!wins[0] && !wins[1] && psLens[0] + psLens[1] < N * N) {
// cerr<<"not finished"<<endl;
return false;
}
vector<pair<int, int>> ans;
auto output = [&]() -> void {
reverse(ans.begin(), ans.end());
puts("TAK");
for (const auto &p : ans) {
printf("%d %d\n", p.first + 1, p.second + 1);
}
};
auto check = [&](int h0) -> bool {
if (wins[h0 ^ 1]) {
return false;
} else if (wins[h0]) {
if (ands[h0]) {
const int z0 = __builtin_ctzll(ands[h0]);
const int x0 = z0 / N;
const int y0 = z0 % N;
// cerr<<"(x0, y0) = "<<make_pair(x0,y0)<<endl;
for (int i = 0; i < psLens[h0]; ++i) {
if (pss[h0][i] == make_pair(x0, y0)) {
swap(pss[h0][i], pss[h0][0]);
break;
}
}
for (int i = 0; i < psLens[0] || i < psLens[1]; ++i) {
for (const int h : {h0, h0 ^ 1}) {
if (i < psLens[h]) {
ans.push_back(pss[h][i]);
}
}
}
output();
return true;
} else {
return false;
}
} else {
for (int i = 0; i < psLens[0] || i < psLens[1]; ++i) {
for (const int h : {h0, h0 ^ 1}) {
if (i < psLens[h]) {
ans.push_back(pss[h][i]);
}
}
}
output();
return true;
}
};
const int len = min(psLens[0], psLens[1]);
for (int h = 0; h < 2; ++h) {
if (psLens[h] > len) {
if (psLens[h] == len + 1) {
return check(h);
} else {
return false;
}
}
}
for (int h = 0; h < 2; ++h) {
if (check(h)) {
return true;
}
}
return false;
}
int main() {
for (int numCases; ~scanf("%d", &numCases); ) { for (int caseId = 1; caseId <= numCases; ++caseId) {
scanf("%d%d", &N, &K);
for (int x = 0; x < N; ++x) {
scanf("%s", A[x]);
}
const bool res = solve();
if (!res) {
puts("NIE");
}
}
#ifndef LOCAL
break;
#endif
}
return 0;
}
详细
Test #1:
score: 100
Accepted
time: 1ms
memory: 3680kb
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 2 3 3 3 2 2 3 1 1 1 1 3 2 1 TAK 1 4 4 2 1 2 3 3 1 1 2 4 TAK 3 3 3 1 3 2 2 3 2 1 2 2 1 3 1 1 1 2 NIE NIE NIE NIE
result:
ok correct (7 test cases)
Test #2:
score: 0
Accepted
time: 11ms
memory: 3732kb
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 ...
output:
TAK 2 3 3 3 2 2 3 1 1 1 1 3 2 1 TAK 3 3 3 1 3 2 2 3 2 1 2 2 1 3 1 1 1 2 NIE NIE NIE NIE NIE TAK 3 2 1 3 3 1 1 2 2 2 1 1 NIE NIE NIE NIE NIE NIE NIE NIE NIE NIE NIE NIE NIE NIE NIE NIE TAK 3 2 4 3 3 1 4 2 2 4 1 1 2 3 2 2 1 3 1 4 1 2 4 1 NIE NIE NIE NIE NIE TAK 4 4 4 3 4 1 4 2 3 4 3 2 3 3 2 2 3 1 1 4 ...
result:
ok correct (10000 test cases)
Test #3:
score: 0
Accepted
time: 32ms
memory: 3752kb
input:
10000 6 4 x.xx.o xo.o.x ooox.o o..xo. ..xxxo o.oxx. 6 5 oooxxx oxoxxo xoooxo xoxxxx xooxox xoxxxx 6 3 o.x.x. oo.o.x xx.oo. .x.xx. ooxo.. .xxo.. 6 6 xoo..o o.xx.x oooooo xx.x.. o..xx. ...xxx 6 5 xooxoo ooxxoo xxooxx oxooxx oxoxxx xxoxoo 6 5 xoxxxo ooooxo ooxoxx oxxoox xxxxox ooooxo 6 5 o....o .ox.oo ...
output:
TAK 6 3 6 5 6 1 6 4 5 6 5 5 4 5 5 4 4 1 5 3 3 6 4 4 3 3 1 1 3 2 2 6 3 1 2 1 2 4 1 4 2 2 1 3 1 6 3 4 NIE TAK 6 3 6 4 6 2 5 4 1 3 5 2 4 5 5 1 4 4 3 5 4 2 3 4 3 2 2 4 3 1 2 2 2 6 2 1 1 5 1 1 5 3 NIE TAK 6 4 6 6 6 2 6 5 6 1 6 3 5 6 5 3 5 5 5 1 5 4 4 4 5 2 4 3 4 6 4 1 4 5 3 4 4 2 3 3 3 6 2 6 3 5 2 5 3 2 ...
result:
ok correct (10000 test cases)