执行时,我的代码给出退出状态-1。如果有任何不同,我可以显示输入。任何人都可以找到为什么会这样吗?
输入:
6
氮 10
E 2
3
W 4
S 5
E 8
我已经查看了二维整数数组和代码中的变量,寻找未初始化的变量,但我没有发现这样的错误。任何人都可以看到为什么我得到退出状态-1?
#include <iostream>
#include <algorithm>
#include <fstream>
using namespace std;
int main() {
ofstream fout("mowing.out");
ifstream fin("mowing.in");
int n; fin >> n;
int ans = 0;
int field[2003][2003];
for (int i = 0; i < 2003; i++) {
for (int j = 0; j < 2003; j++) {
field[i][j] = 0;
}
}
int xloc = 1001, yloc = 1001, time = 0;
for (int i = 0; i < n; i++) {
char dir; int steps;
fin >> dir >> steps;
if (dir == 'N') {
for (int j = 1; j < steps; j++) {
yloc++;
time++;
if (field[xloc][yloc] != 0) ans = max(ans, time-field[xloc][yloc]);
field[xloc][yloc] = time;
}
}
if (dir == 'S') {
for (int j = 1; j < steps; j++) {
yloc--;
time++;
if (field[xloc][yloc] != 0) ans = max(ans, time-field[xloc][yloc]);
field[xloc][yloc] = time;
}
}
if (dir == 'W') {
for (int j = 1; j < steps; j++) {
xloc--;
time++;
if (field[xloc][yloc] != 0) ans = max(ans, time-field[xloc][yloc]);
field[xloc][yloc] = time;
}
}
else {
for (int j = 1; j < steps; j++) {
xloc++;
time++;
if (field[xloc][yloc] != 0) ans = max(ans, time-field[xloc][yloc]);
field[xloc][yloc] = time;
}
}
}
if (ans == 0) fout << -1 << "\n";
else fout << ans << "\n";
return 0;
}