CodeForces-710A. King Moves

来源:互联网 发布:同济大学软件研究生 编辑:程序博客网 时间:2024/04/30 21:51

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

#include<iostream>using namespace std;int main(){    char c, x;    int dir[8][2] = { {-1,0},{-1,1},{0,1},{1,1},{1,0},{1,-1},{0,-1},{-1,-1} };    int d, y, number = 0;    cin >> c >> d;    for (int i = 0; i < 8; i++)    {        x = c + dir[i][1];        y = d + dir[i][0];        if (x >= 'a'&&x <= 'h'&&y >= 1 && y <= 8)            number++;    }    cout << number << endl;    return 0;}
0 0
原创粉丝点击