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:
Eevee
Fox and Minimal path
Modular Multiplicative Inverse
Amr and Pins
Vanya and Computer Game
Rabin-Karp Algorithm for string matching
M-numbers
Finding the largest zero submatrix
Finding the Eulerian path in $O(M)$
Egor and an RPG game
Tablecity
Professor GukiZ and Two Arrays
Gauss method for solving system of linear equations
Magic Stones
Wilbur and Swimming Pool
Pie Rules
Fox And Dinner
Fox And Names
Yuhao and a Parenthesis
Hilbert's Hotel
Farewell Party
Three Blocks Palindrome
Magic Odd Square
Kaavi and Magic Spell
Increase Sequence
Au Pont Rouge
Bus Video System
Một số vấn đề đáng chú ý trong môn Tin học - Phan Công Minh
A + B Strikes Back
Array Shrinking
Sherlock and his girlfriend
Bear and Polynomials