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 <bits/stdc++.h> using namespace std; int main() { char s[42]; scanf("%s", s); int x, y; if (s[0] == 'a' || s[0] == 'h') { x = 2; } else { x = 3; } if (s[1] == '1' || s[1] == '8') { y = 2; } else { y = 3; } printf("%d\n", x * y - 1); return 0; }
Related posts:
Divisor Tree
Paint the String
Diverse Matrix
Little Artem and Random Variable
Guess Your Way Out!
Minimum spanning tree - Prim's algorithm
Parity Game
Cycles
Haar Features
Divide by three, multiply by two
Preorder Test
Disjoint Set Union
Train Hard, Win Easy
K Paths
Playing with Permutations
New Year and Finding Roots
Aztec Catacombs
Balanced Ternary
New Year and Ancient Prophecy
Xenia and Colorful Gems
Rectangles and Square
Euler's totient function
Height All the Same
Three Blocks Palindrome
Let Them Slide
K-th order statistic in O(N)
Huffman Coding on Segment
Cowslip Collections
Useful Decomposition
Binary Exponentiation
Bear and Poker
Maximum Waterfall