Codeforces Good Bye 2017 Div.2 908A,B

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 
#include 
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

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

#include 
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;
}





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


wechat.jpg

你可能感兴趣的:(Codeforces Good Bye 2017 Div.2 908A,B)