您的位置:首页 > 编程语言 > Go语言

Codeforces Good Bye 2017 Div.2 908A,B

2018-02-20 17:25 295 查看

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

#include <iostream>
#include <string>
using namespace std;

#define MAX 50

int main()
{
int cnt = 0;
string s;
cin >> s;

for(int i = 0; i < s.length(); i++)
{
if('a' <= s.at(i) <= 'z')
{
if('a' == s.at(i)|| 'e' == s.at(i) || 'i' == s.at(i) || 'o' == s.at(i) || 'u' == s.at(i))
{
cnt++;
}
}

if('0' <= s.at(i) <= '9')
{
if('1' == s.at(i) || '3' == s.at(i) || '5' == s.at(i) || '7' == s.at(i) || '9' == s.at(i))
{
cnt++;
}
}
}

cout << cnt;
}


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

DirectionDigit
DOWN, UP, RIGHT, LEFT0, 1, 2, 3
DOWN, UP, RIGHT, LEFT0, 1, 3, 2
DOWN, UP, RIGHT, LEFT0, 2, 1, 3
DOWN, UP, RIGHT, LEFT0, 2, 3, 1
DOWN, UP, RIGHT, LEFT0, 3, 1, 2
DOWN, UP, RIGHT, LEFT0, 3, 2, 1
DOWN, UP, RIGHT, LEFT1, 0, 2, 3
DOWN, UP, RIGHT, LEFT1, 0, 3, 2
DOWN, UP, RIGHT, LEFT1, 2, 0, 3
DOWN, UP, RIGHT, LEFT1, 2, 3, 0
DOWN, UP, RIGHT, LEFT1, 3, 0, 2
DOWN, UP, RIGHT, LEFT1, 3, 2, 0
DOWN, UP, RIGHT, LEFT2, 0, 1, 3
DOWN, UP, RIGHT, LEFT2, 0, 3, 1
DOWN, UP, RIGHT, LEFT2, 1, 0, 3
DOWN, UP, RIGHT, LEFT2, 1, 3, 0
DOWN, UP, RIGHT, LEFT2, 3, 0, 1
DOWN, UP, RIGHT, LEFT2, 3, 1, 0
DOWN, UP, RIGHT, LEFT3, 0, 1, 2
DOWN, UP, RIGHT, LEFT3, 0, 2, 1
DOWN, UP, RIGHT, LEFT3, 1, 0, 2
DOWN, UP, RIGHT, LEFT3, 1, 2, 0
DOWN, UP, RIGHT, LEFT3, 2, 0, 1
DOWN, UP, RIGHT, LEFT3, 2, 1, 0
(2) For C++, you can use next_permutation() of STL to enumerate all 24 permutations.

Code

#include <bits/stdc++.h>
using namespace std;

enum Dir{DOWN, UP, RIGHT, LEFT};
const int maxn = 50;
char grid[maxn][maxn];
int n, m;                       // n for rows, m for columns
int digit[4] = {0, 1, 2, 3};
int startX, startY, exitX, exitY;
string instructions;

int move()
{
int row = startX, col = startY;

for (int i = 0; i < instructions.size(); ++i)
{
int d = instructions[i] - '0';
for (int j = 0; j < 4; ++j)
{
if (d == digit[j])
{
if (j == DOWN)
{
row++;
}

if (j == UP)
{
row--;
}

if (j == RIGHT)
{
col++;
}

if (j == LEFT)
{
col--;
}
}

if (row > n || row < 1 || col > m || col < 1)
{
return 0;
}
else if (grid[row][col] == 'E')
{
return 1;
}
else if (grid[row][col] == '#')
{
return 0;
}
}
}

return 0;
}

int main()
{
cin >> n >> m;
for (int i = 1; i <= n; ++i)
{
for (int j = 1; j <= m; ++j)
{
cin >> grid[i][j];
if (grid[i][j] == 'S')
{
startX = i;
startY = j;
}
else if (grid[i][j] == 'E')
{
exitX = i;
exitY = j;
}
}
}

cin >> instructions;
int res = 0;
do
{
res += move();
} while (next_permutation(digit, digit + 4));

cout << res << endl;

return 0;
}


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

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: