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



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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值