找了半天,锁定bug是find()返回-1,传到tmin的参数f里面突然就变成了0???我好懵啊……大佬们解释一下(可以去IDE看看他都干了啥,可以采用输出调试法锁定错误) 《真·爆零》
#include <bits/stdc++.h>
using namespace std;
int t;
string str;
struct loc
{
int x;
int y;
int dis(loc that)
{
return abs(this -> x - that.x) + abs(this -> y - that.y);
}
loc & move(char mov)
{
if (mov == 'E')
{
this -> y--;
return * this;
}
if (mov == 'S')
{
this -> x--;
return * this;
}
if (mov == 'W')
{
this -> y++;
return * this;
}
if (mov == 'N')
{
this -> x++;
return * this;
}
}
}a, b;
int tmin(int f, int s)
{
if (f == -1) return s;
if (s == -1) return f;
return min(f, s);
}
int find(loc x, loc y, int tim)
{
if (x.dis(y) > tim) return -1;
if (x.dis(y) == 0) return 0;
return tmin(find(x, y, tim - 1), find(x.move(str.at(t - tim)), y, tim - 1) + 1);
}
int main()
{
cin >> a.x >> a.y >> b.x >> b.y >> t;
for (int i = 1; i <= t; i++)
{
char ch;
cin >> ch;
str += ch;
}
cout << find(a, b, t) << endl;
return 0;
}