Tuesday, August 23, 2016

【CodeForces】〖 Educational Codeforces Round 16〗A. King Moves

Problem here

Problem

The only king stands on the standard chess board. You are given his position in format “cd”, where c is the column from ‘a’ to ‘h’ and d is the row from ‘1’ to ‘8’. Find the number of moves permitted for the king.
Check the king’s moves here https://en.wikipedia.org/wiki/King_(chess).
这里写图片描述
King moves from the position e4

Input

The only line contains the king’s position in the format “cd”, where ‘c’ is the column from ‘a’ to ‘h’ and ‘d’ is the row from ‘1’ to ‘8’.

Output

Print the only integer x — the number of moves permitted for the king.

Example

input
e4
output
8

Solution

#include <iostream>
#include <memory.h>
using namespace std;


int main(){
    char cc;
    int c, d, ans = 0;
    cin >> cc >> d;
    cc -= 48;
    c = cc - '0';
    if(c + 1 <= 8 && c + 1 > 0){
        ans++;
        if(d+1 > 0 && d+1 <= 8)
            ans++;
        if(d-1 > 0 && d-1 <= 8)
            ans++;
    }
    if(c - 1 <= 8 && c - 1 > 0){
        ans++;
        if(d+1 > 0 && d+1 <= 8)
            ans++;
        if(d-1 > 0 && d-1 <= 8)
            ans++;
    }
    if(d + 1 > 0 && d + 1 <= 8)
        ans++;
    if(d - 1 > 0 && d - 1 <= 8)
        ans++;

    cout << ans << endl;

    return 0;
}

No comments:

Post a Comment