Codeforces Round 190 div.2 322C 321A Ciel and Robot

时间:2023-03-09 09:13:05
Codeforces Round 190 div.2 322C 321A Ciel and Robot

唔。。。这题是数学题。

比赛时做出来,但题意理解错了,以为只要判断那点是不是在线上就行了,发现过不了样例就没提交。

思路:记录每一步的偏移,假设那点是在路径上的某步,然后回推出那一个周期的第一步,判断是不是在线上就行了。

本来用斜率做,没考虑斜率不存在的情况。

重新写了一遍,过了前十个样例。但是在追加的-1 -1 UR卡住了。

三鲜大神说: kx + b = y,判断整除就可以了。(orz)

于是想了一下,开始考虑整除,写了个判断的函数来判断就行了。(蒻菜只能写出又长又臭的判断)

代码:

#include <cstdio>
#include <cstring>
int x = 0, y = 0, pos[101][2] = {0}; bool judge(int px, int py) {
if (x == 0 && y != 0)
if (px == 0 && py % y == 0 && py / y >= 0)
return true;
else
return false;
if (y == 0 && x != 0)
if (py == 0 && px % x == 0 && px / x >= 0)
return true;
else
return false;
if (x == 0 && y == 0)
if (px == 0 && py == 0)
return true;
else
return false;
if (x != 0 && y != 0)
if (py == px / x * y && px % x == 0 && px / x >= 0)
return true;
else
return false;
}//bool
int main() {
int a, b;
char op[101];
scanf("%d%d%s", &a, &b, op);
if (a == 0 && b == 0) {
printf("Yes\n");
return 0;
}
for (int i = 0; i < strlen(op); i++) {
switch (op[i]) {
case 'U': y++; break;
case 'D': y--; break;
case 'L': x--; break;
case 'R': x++; break;
}
pos[i][0] = x;
pos[i][1] = y;
}//for
for (int i = 0; i < strlen(op); i++)
if (judge(a - pos[i][0], b - pos[i][1])) {
printf("Yes\n");
return 0;
}//if
printf("No\n");
return 0;
}

以后要争取在比赛时做出C及后面的题目。。。