PTA(每日一题)7-41 骑士

在国际象棋中,棋盘的行编号为18,列编号为ah;马以“日”方式行走,根据马在当前棋盘上的位置,请问可以有几种合适的走法。如下图所示,设马(以H表示)在e4位置,则下一步可以走的位置是棋盘中粗体数字标注的8个位置:

在这里插入图片描述

输入格式:
首先输入一个正整数T,表示测试数据的组数,然后是T组测试数据。每组测试数据输入一个字符(ah)和一个整数(18),表示马所在的当前位置。

输出格式:
对于每组测试,输出共有几种走法。

输入样例:

1
e4

输出样例:

8

代码:

n = int(input())
m = 8
for i in range(n):
    column, row = input()
    row = int(row)
    count = 0
    if m - row >= 2 and ord(column) - ord('a') >= 2:
        count += 2
    if m - row >= 2 and ord(column) - ord('a') == 1:
        count += 1
    if m - row >= 2 and ord('h') - ord(column) >= 2:
        count += 2
    if m - row >= 2 and ord('h') - ord(column) == 1:
        count += 1
    if m - row == 1 and ord(column) - ord('a') >= 2:
        count += 1
    if m - row == 1 and ord('h') - ord(column) >= 2:
        count += 1
    if row - 1 >= 2 and ord(column) - ord('a') >= 2:
        count += 2
    if row - 1 >= 2 and ord(column) - ord('a') == 1:
        count += 1
    if row - 1 >= 2 and ord('h') - ord(column) >= 2:
        count += 2
    if row - 1 >= 2 and ord('h') - ord(column) == 1:
        count += 1
    if row - 1 == 1 and ord(column) - ord('a') >= 2:
        count += 1
    if row - 1 == 1 and ord('h') - ord(column) >= 2:
        count += 1
    print(count)

在这里插入图片描述

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

爱写bug的小邓程序员

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值