I am working on a program which takes in a 5x5 array of characters and finds the longest list of same characters, connected meaning adjacent either up, down, left, or to the right of (meaning NO diagonals). However the output is off by 1, giving 6 instead of the correct 7, with the input:
a b c c b
a c b c c
c a a b a
b b a a c
a a a b a
Can anybody help me find what my error in my code is? (MY CODE IS BELOW)
DETAILS: the missing character is at index [3][3] (index starting at 0). When I tested my look() function, it worked properly, when I passed it 3 for row and 2 for col,and it added [3][3] to the final vector to be returned, but I think that something did not pan out right with the recursion.
I have already worked on debugging this, to no success, you can see the debug prints in my code.
#include <iostream>
#include <vector>
#include <algorithm>
#include <fstream>
#include <utility>
using namespace std;
char grid[5][5];
bool seen[5][5];
int cnt = 1;
int maxn = 0;
vector<pair<int, int>> look(int row, int col)
{
//
vector<pair<int, int >> node_list;
if (row != 0)
if (grid[row - 1][col] == grid[row][col])
if (!seen[row - 1][col])
node_list.push_back(make_pair(row - 1, col));
if (row != 4)
if (grid[row + 1][col] == grid[row][col])
if (!seen[row+1][col])
node_list.push_back(make_pair(row + 1, col));
if (col != 0)
if (grid[row][col - 1] == grid[row][col])
if (!seen[row][col-1])
node_list.push_back(make_pair(row, col - 1));
if (col != 4)
if (grid[row][col + 1] == grid[row][col])
if (!seen[row][col+1])
node_list.push_back(make_pair(row, col + 1));
if (binary_search(node_list.begin(), node_list.end(), make_pair(2, 2)))
cout << "HAPPENED^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^" << "\n";
return node_list;
}
void search(int row, int col)
{
for (pair<int, int> a : look(row, col))
{
if (!seen[a.first][a.second])
{
seen[a.first][a.second] = true;
cnt++;
cout << "COUNTED and now SEARCHING " << a.first << " " << a.second << "\n";
cout << "search about to be called on " << a.first << " " << a.second << "\n";
search(a.first, a.second);
}
}
if (cnt > maxn)
maxn = cnt;
cout << "CNT: " << cnt << "\n";
cnt = 1;
return;
}
int main()
{
for (int i = 0; i < 5; i++)
{
for (int j = 0; j < 5; j++)
{
cin >> grid[i][j];
seen[i][j] = false;
}
}
for (int i = 0; i < 5; i++)
{
for (int j = 0; j < 5; j++)
{
if (!seen[i][j])
{
cout << "INITIALLY SEARCHING: " << i << " " << j << "\n";
seen[i][j] = true;
cout << "search about to be called on " << i << " " << j << "\n";
search(i, j);
}
else
cout << "NO INITIAL SEARCH, SEEN: " << i << " " << j << "\n";
}
}
cout << maxn << "\n";
return 0;
}
Aucun commentaire:
Enregistrer un commentaire