@[LukeSu](/user/593753)
这样调输入没问题(应该吧)
但是程序还有问题(也许吧)
样例调完输入后你的程序输出:
To get from e50 to e52 takes 0 knight moves.
To get from a49 to b50 takes 0 knight moves.
To get from b50 to c51 takes 0 knight moves.
To get from a49 to h56 takes 0 knight moves.
To get from a49 to h55 takes 0 knight moves.
To get from h56 to a49 takes 0 knight moves.
To get from b49 to c51 takes 0 knight moves.
To get from f54 to f54 takes 0 knight moves.
```cpp
#include<bits/stdc++.h>
using namespace std;
const int N = 66;
int g[N][N];
int maxx = 8, maxy = 8; //定义棋盘的边界
char ch1, temp1, ch2, temp2; //获取起点输入,终点输入
int bx, by, ex, ey; //起点位置和终点位置
int dx[8] = {1, 1, -1, -1, 2, 2, -2, -2}; //马的步伐
int dy[8] = {2, -2, 2, -2, -1, 1, 1, -1};
queue< pair<int, int> > q;
int st[N][N] = {};
int dist[N][N]; //记录步数
int xx, yy;
void bfs(){
while(!q.empty()){
int x = q.front().first;
int y = q.front().second;
q.pop();
st[x][y] = 1;
for(int i = 0; i < 8; i++){
xx = dx[i] + x;
yy = dy[i] + y;
if(xx >= 1 and xx <= maxx and yy >= 1 and yy <= maxy and st[xx][yy] == 0){
dist[xx][yy] = dist[x][y] + 1;
if(xx == ex and yy == ey){
return;
}
q.push(make_pair(xx, yy));
}
}
}
}
int main(){
map<char, int> mp;
mp['a'] = 1; mp['b'] = 2; mp['c'] = 3; mp['d'] = 4;
mp['e'] = 5; mp['f'] = 6; mp['g'] = 7; mp['h'] = 8;
while(1){
cin>>ch1>>temp1;
char ch=getchar();
cin>>ch2>>temp2;
if(ch!=' '){
break;
}
bx = mp[ch1];
by = temp1;
ex = mp[ch2];
ey = temp2;
dist[ex][ey] = 0;
q.push(make_pair(bx, by));
bfs();
cout << "To get from " << ch1 << by << " to " << ch2 << ey << " takes " << dist[xx][yy] << " knight moves.\n";
memset(st, 0, sizeof st);
while(!q.empty()) q.pop();
memset(dist, 0, sizeof dist);
}
return 0;
}
```
by zxy123bc @ 2022-03-03 20:55:15
@[jiangtaizhe001](/user/215042)
%%%
大佬我还真没试过
不过样例我在c++本地测也过不了……
by zxy123bc @ 2022-03-03 20:58:59
@[zxy123bc](/user/550357)
我觉得程序应该是没问题的(应该是吧)
原来的代码,输出结果中,绿圈是多余的,除了绿圈就是正确答案了,和样例一模一样。就是不知道怎么让这多余的绿圈部分不输出。。
![](https://seikim.com/i/2022/03/03/1283xdd.png)
by LukeSu @ 2022-03-03 23:13:20
**QWQ**
by LukeSu @ 2022-03-03 23:14:13
@[zxy123bc](/user/550357)
问题我解决了!
使用rewind方法放在while里即可,rewind()作用是清空缓存区,这样就可以避免读入多余换行符:
```cpp
rewind(stdin);
```
by LukeSu @ 2022-03-07 19:45:15
@[LukeSu](/user/593753) Nb
所以你A了吗
by zxy123bc @ 2022-03-07 21:18:01
@[zxy123bc](/user/550357)
TLE了。。
o(╥﹏╥)o
by LukeSu @ 2022-03-07 21:43:24