codeforce solution 3
A
s = gets
p1 = s[0].ord - 97
p2 = s[1].to_i - 1
t = gets
q1 = t[0].ord - 97
q2 = t[1].to_i - 1
puts [(p1 - q1).abs, (p2 - q2).abs].max
while (p1 != q1 || p2 != q2)
d = ''
if p1 < q1
d += 'R'
p1 += 1
elsif p1 > q1
d += 'L'
p1 -= 1
end
if p2 < q2
d += 'U'
p2 += 1
elsif p2 > q2
d += 'D'
p2 -= 1
end
puts d
end
C
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#define FOR(i, n) for (i = 0; i < n; ++i)
#define br break
#define con continue
int i, j, k;
char board[3][3];
char check() {
char c;
char x = 0;
char y = 0;
char z = 0;
FOR(i, 3) {
FOR (j, 3) {
switch (board[i][j]) {
case '0': x++; break;
case 'X': y++; break;
case '.': z++; break;
}
}
}
if (x > y || y - x > 1) return '_';
char win = 0;
char l = 0;
// for each row
FOR(i, 3) {
c = board[i][0];
if (c == '.') con;
if (c == board[i][1] && c == board[i][2]) {
if (l == 0) l = c; else return '_';
if (win == 0) win = c;
}
}
// for each column
l = 0;
FOR (i, 3) {
c = board[0][i];
if (c == '.') con;
if (c == board[1][i] && c == board[2][i]) {
if (l == 0) l = c; else return '_';
if (win == 0) win = c;
}
}
c = board[0][0];
if (c != '.' && c == board[1][1] && c == board[2][2]) {
if (win == 0) win = c;
}
c = board[0][2];
if (c != '.' && c == board[1][1] && c == board[2][0]) {
if (win == 0) win = c;
}
if (x < y && win == '0') return '_';
if (x == y && win == 'X') return '_';
if (win != 0) return win;
if (z == 0) return '.';
if (y > x) return 'o';
return 'x';
}
int main() {
FOR(i, 3) scanf(" %s", &board[i]);
char c = check();
switch (c) {
case '_':
printf("illegal"); break;
case 'x':
printf("first"); break;
case 'o':
printf("second"); break;
case 'X':
printf("the first player won"); break;
case '0':
printf("the second player won"); break;
case '.':
printf("draw"); break;
}
}
D
import java.lang.Integer.max
import java.util.*
fun readInt(): Int {
return readLine()!!.toInt()
}
fun readString(): String {
return readLine()!!.trim()
}
fun readIntList(): List<Int> {
return readLine()!!.split(' ').map { it.toInt() }
}
fun main() {
var str = readLine()!!.trim()
var cr = str.toCharArray()
var c = 0
var t = PriorityQueue<Pair<Int, Int>> {
o1, o2 -> o1.first.compareTo(o2.first)
}
var cost = 0L
for (i in cr.indices) {
when (cr[i]) {
'(' -> c++
')' -> c--
else -> {
val (a, b) = readIntList()
cr[i] = ')'
c--
cost += b
t.add(Pair(a - b, i))
}
}
if (c < 0) {
if (t.isEmpty()) break
val to = t.poll()
c += 2
cost += to.first
cr[to.second] = '('
}
}
if (c != 0) {
println(-1)
} else {
println(cost)
println(cr)
}
return
}