CF1503B 题解

2021-04-04 12:22:36

## 代码实现

#include<cmath>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<ctime>
#include<iomanip>
#include<iostream>
#include<limits>

namespace StandardLibrary {
using std::cin;
using std::cout;
using std::cerr;
using std::endl;
using std::fixed;
using std::flush;
using std::setprecision;
using std::setw;
using std::numeric_limits;
}
using namespace StandardLibrary;

#include<algorithm>
#include<bitset>
#include<complex>
#include<deque>
#include<map>
#include<queue>
#include<set>
#include<stack>
#include<string>
#include<utility>
#include<vector>

namespace STL {
using std::bitset;
using std::complex;
using std::deque;
using std::greater;
using std::less;
using std::map;
using std::pair;
using std::priority_queue;
using std::queue;
using std::set;
using std::stack;
using std::string;
using std::vector;

using std::make_pair;
using std::sort;
}
using namespace STL;

#define rep(var, start, end) for(int var = (start), var##_end = (end); var <= var##_end; var++)
#define rrep(var, start, end) for(int var = (start), var##_end = (end); var >= var##_end; var--)

namespace IO {
template <typename T>
int c = getchar(); T f = 1; dest = 0;
while (c < 48 || c > 57) f *= (c == 45 ? -1 : 1), c = getchar();
while (c >= 48 && c <= 57) dest = (dest << 3) + (dest << 1) + (c ^ 48), c = getchar();
return dest *= f;
}
template <typename T, typename... args>
template <typename T>
int c = getchar(); T f = 1, dest = 0;
while (c < 48 || c > 57) f *= (c == 45 ? -1 : 1), c = getchar();
while (c >= 48 && c <= 57) dest = (dest << 3) + (dest << 1) + (c ^ 48), c = getchar();
return dest * f;
}

template <typename T>
inline void put(T val) {
T tmp = 0, l = 0; if (val < 0) putchar('-'), val = -val;
while (val) tmp = tmp * 10 + (val % 10), l++, val /= 10;
if (l == 0) putchar('0');
else {
while (l) putchar((tmp % 10) + 48), l--, tmp /= 10;
while (l) putchar('0'), l--;
}
}
template <>
inline void put<char*>(char* val) { rep(i, 0, ((int)strlen(val)) - 1) putchar(val[i]); }
template <>
inline void put<const char*>(const char* val) {
rep(i, 0, ((int)strlen(val)) - 1) putchar(val[i]);
}
template <>
inline void put<string>(string val) { put(val.c_str()); }
template <typename T, typename... args>
inline void put(T val, args&... values) { put(val), putchar(' '), put(values...); }

template <typename T>
inline void write(T val) {
put(val), putchar(' ');
}
template <typename T, typename... args>
inline void write(T val, args&... values) { write(val), write(values...); }

inline void writeln() { puts(""); }
template <typename T>
inline void writeln(T val) { put(val), writeln(); }
template <typename T, typename... args>
inline void writeln(T val, args&... values) { put(val), putchar(' '), writeln(values...); }
}
using namespace IO;

namespace Tools {
template <typename T, typename... args>
inline void log(T val) { cerr<<val<<endl; }
template <typename T, typename... args>
inline void log(T val, args... lists) { cerr<<val<<' '; log(lists...); }
}
using namespace Tools;

//=======正文开始=======

int a[201][201];

int n, k, oi = 1, oj = 1, ei = 2, ej = 1, flag = 1, li = 1, lj = 1;

void _put(int b, int i, int j) { a[i][j] = b, writeln(b,i,j), fflush(stdout); }  //填充函数

int main() {
for (int i = 1; i <= n * n; i++) {
if (k == 0) {          //确定两种格子颜色
if (d == 1) {
_put(2, 1, 1), k = 2, oi++, oj++;
} else _put(1, 1, 1), k = 1, oi++, oj++;
} else if (flag) {         //未达到两种状态
if (d == k) {
_put(3 - k, ei, ej);
ei++, ej++;
if (ei > n) ei -= n, ej -= (n & 1);
if (ej > n) ej -= n, ei -= (n & 1);
if (a[ei][ej]) {
if (ei == n || ei == n - 1) flag = 0;
else ei += 2;
}
} else {
_put(k, oi, oj);
oi++, oj++;
if (oi == oj && oi > n) oi -= n, oj -= n;
if (oi > n) oi -= n, oj += (n & 1);
if (oj > n) oj -= n, oi += (n & 1);
if (a[oi][oj]) {
if (oi == n || oi == n - 1) flag = 0;
else oi += 2;
}
}
} else {        //达到两种状态之一
int f = 0;
for (; li <= n; li++) {
if (lj == n + 1) lj = 1;
for (; lj <= n; lj++) {
if (a[li][lj]) continue;
if ((li >= lj && (li - lj) % 2 == 1) || ((li < lj) && (lj - li) % 2 == 1)) {
if (3 - k == d) _put(3, li, lj);
else _put(3 - k, li, lj);
} else {
if (d == k) _put(3, li, lj);
else _put(k, li, lj);
}
f = 1; break;
}
if (f) break;
}
}
}
}
• star
首页