srupのメモ帳

競プロで解いた問題や勉強したことを記録していくメモ帳

Codeforces Educational Codeforces Round 16 A. King Moves

問題

問題概要

チェスのキングのコマの位置が与えられる。次の手で何通りの進み方があるか。

解法

キングは周囲8方向に1マスずつ進めるので、周りがマスに囲まれていれば、8通りの進み方がある。ただし、隅っこの場合などは変わる。8方向に動かして、範囲内の数を数得れば良い。

ミス

これぐらい簡潔に問題文を書いてもらいたい。

コード

#include <iostream>
#include <cstdio>
#include <string>
using namespace std;

int dx[8] = {0, 1, 1, 1, 0, -1, -1, -1};
int dy[8] = {1, 1, 0, -1, -1, -1, 0, 1};

int main(void){
    string s; cin >> s;
    int w = s[0] - 'a';
    int h = s[1] - '1';
    int ans = 0;
    for (int i = 0; i < 8; ++i){
        int nowy = h + dy[i], nowx = w + dx[i];
        if(0 <= nowy&&nowy<= 7 && 0 <= nowx && nowx <= 7){
            ans++;
        }
    }
    printf("%d\n", ans);
    return 0;
}