Educational Codeforces Round 16 A. King Moves

来源:互联网 发布:js 让网页不停的点击 编辑:程序博客网 时间:2024/05/17 21:39
A. King Moves
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

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

<h1>//这种类型的水题应该追求速度和准确度,一定要一次就过千万不要被各种bug缠上</h1>//  main.cpp//  Educational Codeforces Round 16 pa////  Created by 张嘉韬 on 16/8/22.//  Copyright © 2016年 张嘉韬. All rights reserved.//#include <iostream>#include <cstring>#include <cstdio>#include <cmath>using namespace std;int dx[8]={-1,0,1,1,1,0,-1,-1};int dy[8]={-1,-1,-1,0,+1,+1,+1,0};int x,y;int legal(int i){    int tempx=x+dx[i];    int tempy=y+dy[i];    if(tempx<=8&&tempx>=1&&tempy<=8&&tempy>=1) return 1;    else return 0;}int main(int argc, const char * argv[]) {    char c;    int d;    scanf("%c%d",&c,&d);    x=d;    y=c-96;//from 1 t0 8    int counter=0;    for(int i=0;i<8;i++)    {        if(legal(i)) counter++;    }    printf("%d\n",counter);    return 0;}


0 0