一道水题啊~~~
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
|
这道题又再一次嘲讽了我的英语功底啊%>_<%;
题意大致是一个8*8的棋盘,一个棋子能否分别在水平(或竖直)方向上走1格(或2格),然后在竖直(或水平)方向上走2(或1)格,且棋子不会走到棋盘外,能则计数一次,最后看一共能走几次;
代码如下:
- #include<iostream>
- #include<cstring>
- using namespace std;
- int sufa(char i,char j)
- {
- int c=0;
- if(i+1<='h')
- {
- if(j+2<='8')c++;
- if(j-2>='1')c++;
- }
- if(i-1>='a')
- {
- if(j+2<='8')c++;
- if(j-2>='1')c++;
- }
- if(j+1<='8')
- {
- if(i+2<='h')c++;
- if(i-2>='a')c++;
- }
- if(j-1>='1')
- {
- if(i+2<='h')c++;
- if(i-2>='a')c++;
- }
- return c;
- }
- int main()
- {
- int a,c,i;
- char b[5],d,e;
- cin>>a;
- while(a--)
- {
- cin>>b;
- d=b[0];e=b[1];
- c=sufa(d,e);
- cout<<c<<endl;
- }
- return 0;
- }