-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAssignment3.cpp
More file actions
172 lines (159 loc) · 6.86 KB
/
Assignment3.cpp
File metadata and controls
172 lines (159 loc) · 6.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
#include <iostream>
#include <string>
#include <iomanip>
#include <vector>
#include <fstream>
#include <ctype.h>
//reads in file of name specified by user and fills 2D vector with its characters
//returns number of rows and cols (indirectly) that represent the rows and columns of the 2D vector
//returns 2D vector filled with characters from file
std::vector<std::vector<char>> readFile(int & rows, int & cols){
std::cout << "Enter filename: ";
std::string filename = "";
std::cin >> filename;
std::cout << std::endl;
std::ifstream fileInput;
fileInput.open(filename);
//gets input of the file with the name specified by user
char character;
int row = 0;
int col = 0;
fileInput >> rows;
fileInput >> cols;
//set the number of rows and cols based on the first two numbers in the file
std::vector<std::vector<char>> grid(rows);
for (int i = 0; i < rows; i++){
grid[i] = std::vector<char>(cols);
}
//creates 2D vector of size rows * cols
while (!fileInput.eof()){//CHECK WHY I NEED THE IF ROW !+ ROWS STATEMENT
fileInput >> character;
//read character from file
if (row != rows){
grid[row][col] = character;
//if in bounds assign the character to the 2D vector at index [row][col]
}
col++;
if (col == cols){
//start a new row in the array once all colums of previous row are filled
col = 0;
row++;
}
}
fileInput.close();
return grid;
}
//recurisvely finds a path through the 2D array that matches with the word given by the user. starts from first matching letter
//find path and marks it in the "blank" array
//does not choose a path with letters it has used before
//grid is the 2D vector of characters
//row is the row to search within
//col is the column to search within
//word is the word or substring which is searched for in the array
//gridBlank is the blank array filled with '-' that is filled in with the path of the word if applicable
//returns true if path is found and falls if no path is found
bool findPath(std::vector<std::vector<char>> & grid, int row, int col, std::string word, std::vector<std::vector<char>> & gridBlank ){
if (word.length() == 1){
gridBlank[row][col] = '*';
//if the last letter has been found, mark it with a '*' in the gridBlank 2D vector and return true
return true;
}
if (row-1 != -1 && grid[row-1][col] == word[1] && findPath(grid, row -1, col, word.substr(1), gridBlank)&& gridBlank[row][col] == '-' ){//UP
gridBlank[row][col] = '^';
//mark gridBlank with the up symbol and return true if the upper field is in bound, matches the searched letter, has not been used before, and the rest of the path works out
return true;
}
if (col+1 != (int)grid[row].size() && grid[row][col+1] == word[1] && findPath(grid, row, col +1, word.substr(1), gridBlank) && gridBlank[row][col] == '-' ){//RIGHT
gridBlank[row][col] = '>';
//mark gridBlank with the right symbol and return true if the right field is in bound, matches the searched letter, has not been used before, and the rest of the path works out
return true;
}
if (row+1 != (int)grid.size() && grid[row+1][col] == word[1] && findPath(grid, row +1, col, word.substr(1), gridBlank) && gridBlank[row][col] == '-'){//DOWN
gridBlank[row][col] = 'V';
//mark gridBlank with the down symbol and return true if the lower field is in bound, matches the searched letter, has not been used before, and the rest of the path works out
return true;
}
if (col-1 != -1 && grid[row][col-1] == word[1] && findPath(grid, row, col -1, word.substr(1), gridBlank)&& gridBlank[row][col] == '-' ){//LEFT
gridBlank[row][col] = '<';
//mark gridBlank with the left symbol and return true if the left field is in bound, matches the searched letter, has not been used before, and the rest of the path works out
return true;
}
return false;
//if no path is found return false
}
//prints a given 2D vector in the right format
//grid is the 2D vector to be printed
//rows is the number of rows of the vector
//cols is the number of columns of the vector
//no return
void printVector(std::vector<std::vector<char>> & grid, int & rows, int & cols){
for (int i = 0; i < rows; i++){
//for each row
for (int j = 0; j < cols; j++){
//for each column
std::cout << grid[i][j] << " ";
//print vector at index i and j
}
std::cout << std::endl;
}
}
//checks every index of the vector for if a path exists
//grid is the 2D vector to check for the starting letter
//rows is the number of rows of the vector
//cols is the number of columns of the vector
//word is the word to search for
//gridBlank is the vector filled with '-'
//returns true if path is found
bool pathExists(std::vector<std::vector<char>> grid, int rows, int cols, std::string word, std::vector<std::vector<char>> gridBlank){
for (int row = 0; row < rows; row++){
for (int col = 0; col < cols; col++){
if (grid[row][col] == word[0]){
if (findPath(grid, row, col, word, gridBlank)){
printVector(gridBlank, rows, cols);
return true;
}
}
}
}
return false;
}
//sets up the searching process by getting the word to search for, making it all uppercase, and making a blank 2D vector to match the grid vector and filling it with '-'
//grid is the 2D vector to search the word within
//rows is the number of rows of the vectors
//cols is the number of columns of the vectors
//returns true if a path has been found
bool searchWord(std::vector<std::vector<char>> grid, int rows, int cols){
std::string word;
std::cout << "Insert word to search: ";
std::cin >> word;
for (int i = 0; i < (int)word.length(); i++){
word[i] = toupper(word[i]);
}
//gets an input from user and turns it all to uppercase
std::cout << std::endl;
std::vector<std::vector<char>> gridBlank(rows);
for (int i = 0; i < rows; i++){
gridBlank[i] = std::vector<char>(cols);
}
for (int i = 0; i < (int)gridBlank.size(); i++){
for (int j = 0; j < (int)gridBlank[0].size(); j++){
gridBlank[i][j] = '-';
}
}
//makes a blank 2D vector to match the grid vector and filling it with '-'
return pathExists(grid, rows, cols, word, gridBlank);
}
//main method
int main(){
int rows, cols;
//rows and cols to be assigned in readFile() function
std::vector<std::vector<char>> grid = readFile(rows, cols);
printVector(grid, rows, cols);
//prints 2D vector
bool isPath = searchWord(grid, rows, cols);
//isPath is true if a path has been found
if (!isPath){
std::cout << "No path found." << std::endl;
}
return 0;
}