URAL1197解题报告

一道水题啊~~~

1197. Lonesome Knight

Time limit: 1.0 second
Memory limit: 64 MB
The statement of this problem is very simple: you are to determine how many squares of the chessboard can be attacked by a knight standing alone on the board. Recall that a knight moves two squares forward (horizontally or vertically in any direction) and then one square sideways (perpedicularly to the first direction).

Input

The first line contains the number N of test cases, 1 ≤ N ≤ 100. Each of the following N lines contains a test: two characters. The first character is a lowercase English letter from 'a' to 'h' and the second character is an integer from 1 to 8; they specify the rank and file of the square at which the knight is standing.

Output

Output N lines. Each line should contain the number of the squares of the chessboard that are under attack by the knight.

Sample

input output
3
a1
d4
g6
2
8
6

这道题又再一次嘲讽了我的英语功底啊%>_<%;

题意大致是一个8*8的棋盘,一个棋子能否分别在水平(或竖直)方向上走1格(或2格),然后在竖直(或水平)方向上走2(或1)格,且棋子不会走到棋盘外,能则计数一次,最后看一共能走几次;


代码如下:

  1. #include<iostream>
  2. #include<cstring>
  3. using namespace std;
  4. int sufa(char i,char j)
  5. {
  6. int c=0;
  7. if(i+1<='h')
  8. {
  9. if(j+2<='8')c++;
  10. if(j-2>='1')c++;
  11. }
  12. if(i-1>='a')
  13. {
  14. if(j+2<='8')c++;
  15. if(j-2>='1')c++;
  16. }
  17. if(j+1<='8')
  18. {
  19. if(i+2<='h')c++;
  20. if(i-2>='a')c++;
  21. }
  22. if(j-1>='1')
  23. {
  24. if(i+2<='h')c++;
  25. if(i-2>='a')c++;
  26. }
  27. return c;
  28. }
  29. int main()
  30. {
  31. int a,c,i;
  32. char b[5],d,e;
  33. cin>>a;
  34. while(a--)
  35. {
  36. cin>>b;
  37. d=b[0];e=b[1];
  38. c=sufa(d,e);
  39. cout<<c<<endl;
  40. }
  41. return 0;
  42. }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值