내 코드는 다음과 같습니다. 그것은 세분화 오류를 제공하고 있습니다. 나는 그것을 디버깅했지만 붙어있다! 문제를 찾을 수 없습니다. 누군가 나를 도울 수 있습니까?내 코드가 세그먼트 오류를 표시하는 이유는 무엇입니까?
#include <iostream>
#include <fstream>
using namespace std;
char art[200][200];
char art2[200][200];
int n;
void solve(char a, int x, int y);
int main() {
// ifstream fin("cowart.in");
// ofstream fout("cowart.out");
cin >> n;
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
cin >> art[i][j];
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
art2[i][j] = art[i][j];
int rh = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (art[i][j] == 'R' || art[i][j] == 'G' || art[i][j] == 'B') {
rh++;
solve(art[i][j], i, j);
}
}
}
int rc = 0;
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
art[i][j] = art2[i][j];
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
if (art[i][j] == 'G')
art[i][j] = 'R';
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (art[i][j] == 'R' || art[i][j] == 'B') {
rc++;
solve(art[i][j], i, j);
}
}
}
cout << rh << " " << rc << endl;
// system("PAUSE");
// fin.close();
// fout.close();
return 0;
}
void solve(char a, int x, int y) {
if (x < 0 || y < 0 || x >= n || y >= n)
return;
if (art[x][y] != a)
return;
art[x][y] == '.';
if (x < n - 1)
solve(a, x + 1, y);
if (x > 0)
solve(a, x - 1, y);
if (y < n - 1)
solve(a, x, y + 1);
if (y > 0)
solve(a, x, y - 1);
return;
}
이 코드되는 용액 : USACO Problem 414
테스트 케이스 것은 내가 보이고 분할 결함은 :
5
RRRBB
GGBBB
BBBRR
BBRRR
RRRRR
출력되어야 다음 solve()
함수에서 4 3
고마워요! 그것은 지금 작동하고 교류를주고 있습니다 :) –