Мой код выглядит следующим образом. Это дает ошибку сегментации. Я отлаживал его, но застрял! Я не могу найти проблему. Кто-нибудь может мне помочь?Почему мой код показывает ошибку сегментации?
#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
Выход должен быть: 4 3
Спасибо! Он работает сейчас и дает AC :) –