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:
King of Thieves
Cow and Haybales
Hiking
Compartments
Gotta Catch Em' All!
Red Blue Tree
Make It One
Color the Carpet
Tidying Up
Cow and Exercise
Felicity's Big Secret Revealed
Wise Men (Hard Version)
Finding a negative cycle in the graph
Command Line Arguments
Breadth-first search
Can Bash Save the Day?
Golden System
Hongcow Masters the Cyclic Shift
Minimum-cost flow - Successive shortest path algorithm
Length of the union of segments
Jordan Smiley
Farewell Party
Vanya and Computer Game
Substitutes in Number
Random Ranking
Bear and Colors
A Lot of Games
Maximum flow - Ford-Fulkerson and Edmonds-Karp
Parity Game
Nikita and stack
Special Task
Exercising Walk