hzu3-Lonesome Knight

1197. Lonesome Knight

Time limit: 1.0 second

Memory limit: 64 MB

The statement ofthis problem is very simple: you are to determine how many squares of thechessboard can be attacked by a knight standing alone on the board. Recall thata knight moves two squares forward (horizontally or vertically in anydirection) and then one square sideways (perpedicularly to the firstdirection).

Input

The first linecontains the numberNof test cases, 1 ≤N≤100. Each of the followingNlines contains a test: twocharacters. 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 andfile of the square at which the knight is standing.

Output

OutputNlines.Each line should contain the number of the squares of the chessboard that areunder attack by the knight.

Sample

input

output

3

a1

d4

g62

8

6

题目大意,在一个8x8(两个轴分别是1~8和a~h)的棋盘上,放一个(国际象棋中的)马,马的走法是日字,如图,从一个圈到另一个圈。求马有几个位置可以到达。

输入要求首先输入要测试的数据组数N,然后依次N组数据,每组数据包括一个横坐标和一个纵坐标。

输出N组数据,每一组代表马在指定位置能走的位置的个数。

分析:

【思路一】

一个马最多就8个位置可以去,简单的说就是遍历一个位置的马的8个走位能不能走,也就是那8个位置是否在棋盘中。

可以先将8个位置的偏移定义下来,如下

然后输入一个点,将其转换为坐标,进行8个方向遍历,输出遍历结果。

【思路二】

其实分析一下数据的规模不大就8*8个,可以考虑进行打表,先将数据存放在表中,然后输入后查询表中的数据将数据进行输出。

表如下

【思路一demo】

//#include

//using

namespace std;

#include

intoffset[8][2] = {

{ 2, 1 },//右下

{ 2, -1 },//有上

{ 1, 2 },//下右

{ -1, 2 },//下左

{ -2, 1 },//左上

{ -2, -1 },//左下

{ -1, -2 },//上左

{ 1, -2 }//上右

};

intmain(){

charstr[3];

intN;

//cin >> N;

scanf("%d", &N);

while(N){

inta,

b,

result = 0;

//cin >> str;

scanf("%s", str);

a = str[0] -'a';

b = str[1] -'0'- 1;

for(inti = 0; i < 8;i++) {

intta = a -offset[i][0];

inttb = b -offset[i][1];

if(ta >= 0&& ta < 8 && tb >= 0 && tb < 8) {

result++;

}

}

//cout <<

result << endl;

printf("%d\n", result);

N--;

}

return0;

}

【思路二demo】

#include

inttable[8][8] = {

2, 3, 4, 4, 4, 4, 3, 2,

3, 4, 6, 6, 6, 6, 4, 3,

4, 6, 8, 8, 8, 8, 6, 4,

4, 6, 8, 8, 8, 8, 6, 4,

4, 6, 8, 8, 8, 8, 6, 4,

4, 6, 8, 8, 8, 8, 6, 4,

3, 4, 6, 6, 6, 6, 4, 3,

2, 3, 4, 4, 4, 4, 3, 2

};

intmain(){

charstr[3];

intN;

scanf("%d", &N);

while(N) {

inta,

b;

scanf("%s", str);

a = str[0] -'a';

b = str[1] -'0'- 1;

printf("%d\n", table[a][b]);

N--;

}

return0;

}

WV��p؀8�

你可能感兴趣的:(hzu3-Lonesome Knight)