[CSP-S2 2019]格雷码 题解
题目地址:洛谷:P5657 格雷码 – 洛谷 | 计算机科学教育新生态 题目描 …
May all the beauty be blessed.
题目地址:Codeforces:Problem – 3C – Codeforces、洛谷:CF3C Tic-tac-toe – 洛谷 | 计算机科学教育新生态
Certainly, everyone is familiar with tic-tac-toe game. The rules are very simple indeed. Two players take turns marking the cells in a 3 × 3 grid (one player always draws crosses, the other — noughts). The player who succeeds first in placing three of his marks in a horizontal, vertical or diagonal line wins, and the game is finished. The player who draws crosses goes first. If the grid is filled, but neither Xs, nor 0s form the required line, a draw is announced.
You are given a 3 × 3 grid, each grid cell is empty, or occupied by a cross or a nought. You have to find the player (first or second), whose turn is next, or print one of the verdicts below:
illegal
— if the given board layout can’t appear during a valid game;the first player won
— if in the given board layout the first player has just won;the second player won
— if in the given board layout the second player has just won;draw
— if the given board layout has just let to a draw.给一个井字棋(三子棋)的局面,请判断该局面的状态,状态有以下几种:
井字棋玩法请百度。
输入格式:
The input consists of three lines, each of the lines contains characters “.”, “X” or “0” (a period, a capital letter X, or a digit zero).
输出格式:
Print one of the six verdicts: first
, second
, illegal
, the first player won
, the second player won
or draw
.
输入样例#1:
X0X .0. .X.
输出样例#1:
second
大模拟题,细节和分类特别多。分状态讨论符合的情况。
下一步X下棋,仅出现于合法且无人获胜的局面中,特征是局面中X和O的数量相同。
下一步O下棋,仅出现于合法且无人获胜的局面中,特征是局面中X比O的数量多1。
非法状态,有以下几类:X与O的数量的关系既不是相同也不是X比O多1;X获胜之后O仍然下了一步棋(X获胜之后X与O数量相同);O获胜之后X仍然下了一步棋(O获胜之后X比O数量多1)。
X获胜,合法局面中X棋子占据了某一行、列或正副对角线。
O获胜,合法局面中O棋子占据了某一行、列或正副对角线。
平局,合法局面,无人获胜,且棋盘中没有空位。
调试的时候平均一个submission过一个测试点,果然还是我太菜了。
// Code by KSkun, 2019/6
#include <cstdio>
char a[5][5];
int main() {
scanf("%s%s%s", a[1] + 1, a[2] + 1, a[3] + 1);
int cntx = 0, cnto = 0;
bool hasdot = false;
for(int i = 1; i <= 3; i++) {
for(int j = 1; j <= 3; j++) {
if(a[i][j] == 'X') cntx++;
if(a[i][j] == '0') cnto++;
if(a[i][j] == '.') hasdot = true;
}
}
if(cntx != cnto && cntx != cnto + 1) {
puts("illegal"); return 0;
}
bool fw = false, sw = false;
for(int i = 1; i <= 3; i++) {
if(a[i][1] == a[i][2] && a[i][2] == a[i][3]) {
if(a[i][1] == 'X') fw = true;
else if(a[i][1] == '0') sw = true;
}
}
for(int i = 1; i <= 3; i++) {
if(a[1][i] == a[2][i] && a[2][i] == a[3][i]) {
if(a[1][i] == 'X') fw = true;
else if(a[1][i] == '0') sw = true;
}
}
if(a[1][1] == a[2][2] && a[2][2] == a[3][3]) {
if(a[1][1] == 'X') fw = true;
else if(a[1][1] == '0') sw = true;
}
if(a[1][3] == a[2][2] && a[2][2] == a[3][1]) {
if(a[1][3] == 'X') fw = true;
else if(a[1][3] == '0') sw = true;
}
if(fw && sw) puts("illegal");
else if(fw && cntx != cnto + 1) puts("illegal");
else if(sw && cntx != cnto) puts("illegal");
else if(fw) puts("the first player won");
else if(sw) puts("the second player won");
else if(!hasdot) puts("draw");
else if(cntx == cnto) puts("first");
else if(cntx == cnto + 1) puts("second");
return 0;
}
Translation by KSkun
原题:問題「機械の総合病院」 | エンジニアが死滅シタ世界 〜アンドロイドとふたりぼっちで生きろ〜
你在分析PAIZA医院的系统。
为了避免被非法入侵,医院系统的用户密码应该达到一定的强度。
PAIZA医院的系统要求用户密码满足以下的强度要求:
密码中,不区分英文字符的大小写。
如果密码满足以上强度要求,输出Valid
,否则输出Invalid
。
例如,样例1中的密码7Caaad9
满足条件1和2,但是不满足条件3,因为aaa
这里连续出现了3个a
字符。
t
如果密码满足以上强度要求,输出Valid
,否则输出Invalid
。
输入:
7Caaad9
输出:
Invalid
输入:
DjZGrduN8Mj4
输出:
Valid
// Code by KSkun, 2019/1
#include <cstdio>
#include <cctype>
#include <cstring>
#include <algorithm>
typedef long long LL;
inline char fgc() {
static char buf[100000], *p1 = buf, *p2 = buf;
return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 100000, stdin), p1 == p2)
? EOF : *p1++;
}
inline LL readint() {
LL res = 0, neg = 1; char c = fgc();
for(; !isdigit(c); c = fgc()) if(c == '-') neg = -1;
for(; isdigit(c); c = fgc()) res = res * 10 + c - '0';
return res * neg;
}
inline char readsingle() {
char c;
while(!isgraph(c = fgc())) {}
return c;
}
char t[35];
bool hasalpha = false, hasdigit = false;
int main() {
scanf("%s", t + 1);
int n = strlen(t + 1);
if(n < 6) {
puts("Invalid"); return 0;
}
int cnt = 0;
for(int i = 1; i <= n; i++) {
if(isupper(t[i])) t[i] = tolower(t[i]);
if(isalpha(t[i])) hasalpha = true;
if(isdigit(t[i])) hasdigit = true;
if(t[i] != t[i - 1]) cnt = 1;
else cnt++;
if(cnt >= 3) {
puts("Invalid"); return 0;
}
}
if(!hasalpha || !hasdigit) {
puts("Invalid"); return 0;
}
puts("Valid");
return 0;
}
Translation by KSkun
原题:問題「砂漠の公園」 | エンジニアが死滅シタ世界 〜アンドロイドとふたりぼっちで生きろ〜
以前公园经常举行某种比赛的大会。每次比赛的结果记录都能找到,但是哪只队伍最终获得优胜的记录却丢失了。
因此你需要写一个程序来计算最终是哪只队伍获得了优胜。
大会进行循环比赛,按照以下的方式计算每只队伍的得分。一次比赛获胜得2分,平局得1分,输局得0分。
在所有比赛进行完毕后,得分最多的队伍获得优胜。
输入中包含大会的参赛人数以及比赛结果,请设计程序输出比赛的获胜者以及得分和胜利、平局、失败的场数。
样例1解释如下图所示。
N
c_{1,1}c_{1,2}...c_{1,N}
c_{2,1}c_{2,2}...c_{2,N}
...
c_{N,1}c_{N,2}...c_{N,N}
W
D
L
-
你需要输出优胜队伍的编号s、得分t、获胜场数W、平局场数D、失败场数L。
s t W D L
W
、D
、L
或-
四种W
时,c_{j, i}为L
D
时,c_{j, i}为D
-
输入:
3
-DW
D-D
LD-
输出:
1 3 1 1 0
输入:
10
-WLDWWDWWW
L-WDWWWLWW
WL-LWWLWWD
DDW-WWDWWW
LLLL-LLLWW
LLLLW-WLLL
DLWDWL-WLW
LWLLWWL-WW
LLLLLWWL-W
LLDLLWLLL-
输出:
4 15 6 3 0
// Code by KSkun, 2019/5
#include <cstdio>
#include <cctype>
#include <cstring>
#include <algorithm>
typedef long long LL;
inline char fgc() {
static char buf[100000], *p1 = buf, *p2 = buf;
return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 100000, stdin), p1 == p2)
? EOF : *p1++;
}
inline LL readint() {
LL res = 0, neg = 1; char c = fgc();
for(; !isdigit(c); c = fgc()) if(c == '-') neg = -1;
for(; isdigit(c); c = fgc()) res = res * 10 + c - '0';
return res * neg;
}
inline char readsingle() {
char c;
while(!isgraph(c = fgc())) {}
return c;
}
inline bool isop(char c) {
return c == '-' || c == 'D' || c == 'W' || c == 'L';
}
inline char readop() {
char c;
while(!isop(c = fgc())) {}
return c;
}
const int MAXN = 105;
int n, t[MAXN], w[MAXN], d[MAXN], l[MAXN];
int main() {
n = readint();
for(int i = 1; i <= n; i++) {
for(int j = 1; j <= n; j++) {
char op = readop();
if(i == j) continue;
if(op == 'W') {
w[i]++; t[i] += 2;
} else if(op == 'D') {
d[i]++; t[i] += 1;
} else if(op == 'L') {
l[i]++;
}
}
}
int win = 0;
for(int i = 1; i <= n; i++) {
if(t[i] > t[win]) win = i;
}
printf("%d %d %d %d %d", win, t[win], w[win], d[win], l[win]);
return 0;
}