Codeforces Good Bye 2017 Div.2 908A,B

我会带着你远行 2022-05-31 10:09 321阅读 0赞

A. New Year and Counting Cards

Problem Statement

http://codeforces.com/contest/908/problem/A

Analysis

For letter, only vowel need to know the digit in the other side.

For digit, only odd number need to know the letter in the other side.

Code

  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4. #define MAX 50
  5. int main()
  6. {
  7. int cnt = 0;
  8. string s;
  9. cin >> s;
  10. for(int i = 0; i < s.length(); i++)
  11. {
  12. if('a' <= s.at(i) <= 'z')
  13. {
  14. if('a' == s.at(i)|| 'e' == s.at(i) || 'i' == s.at(i) || 'o' == s.at(i) || 'u' == s.at(i))
  15. {
  16. cnt++;
  17. }
  18. }
  19. if('0' <= s.at(i) <= '9')
  20. {
  21. if('1' == s.at(i) || '3' == s.at(i) || '5' == s.at(i) || '7' == s.at(i) || '9' == s.at(i))
  22. {
  23. cnt++;
  24. }
  25. }
  26. }
  27. cout << cnt;
  28. }

B. New Year and Buggy Bot

Problem Statement

http://codeforces.com/contest/908/problem/B

Analysis

(1) Bob forgot to actually assign the directions to digits, so there are 24 mapping relations between directions and digits










































































































Direction Digit
DOWN, UP, RIGHT, LEFT 0, 1, 2, 3
DOWN, UP, RIGHT, LEFT 0, 1, 3, 2
DOWN, UP, RIGHT, LEFT 0, 2, 1, 3
DOWN, UP, RIGHT, LEFT 0, 2, 3, 1
DOWN, UP, RIGHT, LEFT 0, 3, 1, 2
DOWN, UP, RIGHT, LEFT 0, 3, 2, 1
DOWN, UP, RIGHT, LEFT 1, 0, 2, 3
DOWN, UP, RIGHT, LEFT 1, 0, 3, 2
DOWN, UP, RIGHT, LEFT 1, 2, 0, 3
DOWN, UP, RIGHT, LEFT 1, 2, 3, 0
DOWN, UP, RIGHT, LEFT 1, 3, 0, 2
DOWN, UP, RIGHT, LEFT 1, 3, 2, 0
DOWN, UP, RIGHT, LEFT 2, 0, 1, 3
DOWN, UP, RIGHT, LEFT 2, 0, 3, 1
DOWN, UP, RIGHT, LEFT 2, 1, 0, 3
DOWN, UP, RIGHT, LEFT 2, 1, 3, 0
DOWN, UP, RIGHT, LEFT 2, 3, 0, 1
DOWN, UP, RIGHT, LEFT 2, 3, 1, 0
DOWN, UP, RIGHT, LEFT 3, 0, 1, 2
DOWN, UP, RIGHT, LEFT 3, 0, 2, 1
DOWN, UP, RIGHT, LEFT 3, 1, 0, 2
DOWN, UP, RIGHT, LEFT 3, 1, 2, 0
DOWN, UP, RIGHT, LEFT 3, 2, 0, 1
DOWN, UP, RIGHT, LEFT 3, 2, 1, 0

(2) For C++, you can use next_permutation() of STL to enumerate all 24 permutations.

Code

  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. enum Dir{DOWN, UP, RIGHT, LEFT};
  4. const int maxn = 50;
  5. char grid[maxn][maxn];
  6. int n, m; // n for rows, m for columns
  7. int digit[4] = {
  8. 0, 1, 2, 3};
  9. int startX, startY, exitX, exitY;
  10. string instructions;
  11. int move()
  12. {
  13. int row = startX, col = startY;
  14. for (int i = 0; i < instructions.size(); ++i)
  15. {
  16. int d = instructions[i] - '0';
  17. for (int j = 0; j < 4; ++j)
  18. {
  19. if (d == digit[j])
  20. {
  21. if (j == DOWN)
  22. {
  23. row++;
  24. }
  25. if (j == UP)
  26. {
  27. row--;
  28. }
  29. if (j == RIGHT)
  30. {
  31. col++;
  32. }
  33. if (j == LEFT)
  34. {
  35. col--;
  36. }
  37. }
  38. if (row > n || row < 1 || col > m || col < 1)
  39. {
  40. return 0;
  41. }
  42. else if (grid[row][col] == 'E')
  43. {
  44. return 1;
  45. }
  46. else if (grid[row][col] == '#')
  47. {
  48. return 0;
  49. }
  50. }
  51. }
  52. return 0;
  53. }
  54. int main()
  55. {
  56. cin >> n >> m;
  57. for (int i = 1; i <= n; ++i)
  58. {
  59. for (int j = 1; j <= m; ++j)
  60. {
  61. cin >> grid[i][j];
  62. if (grid[i][j] == 'S')
  63. {
  64. startX = i;
  65. startY = j;
  66. }
  67. else if (grid[i][j] == 'E')
  68. {
  69. exitX = i;
  70. exitY = j;
  71. }
  72. }
  73. }
  74. cin >> instructions;
  75. int res = 0;
  76. do
  77. {
  78. res += move();
  79. } while (next_permutation(digit, digit + 4));
  80. cout << res << endl;
  81. return 0;
  82. }

更多内容请关注微信公众号
wechat.jpg

发表评论

表情:
评论列表 (有 0 条评论,321人围观)

还没有评论,来说两句吧...

相关阅读

    相关 GOOD BYE OI

    大米饼正式退役了,OI给我带来很多东西 我会的数学知识基本都在下面了 博客园的评论区问题如果我看到了应该是会尽力回答的... 这也是我作为一个OIer最后一次