COMP2113
COMP2113_ENGG1340 Programming technologies and Computer programming II [Section 2BC] [2023]
Loading...
Searching...
No Matches
2.cpp
Go to the documentation of this file.
1#include <iostream>
2#include <fstream>
3#include <sstream>
4#include <string>
5#include <vector>
6using namespace std;
7
8bool passable(vector<vector<int>> planks, int start_row, int start_column, int row_size, int column_size) {
9 if (0 <= start_row && start_row <= row_size-1 &&
10 0 <= start_column && start_column <= column_size-1) {
11 if (planks[start_row][start_column]) {
12 if (start_column == column_size-1) {
13 return true;
14 }
15 else {
16 /* To prevent infinite recursion, we need to prevent the code from trying to
17 make a path of (1,1)->(2,1)->(1,1)->... (within same column looping)
18 and also (1,1)->(1,2)->(1,1)->... (within same row looping)
19 Solution: mark walked */
20
21 planks[start_row][start_column] = 0;
22
23 bool result = (
24 passable(planks, start_row, start_column + 1, row_size, column_size) ||
25 passable(planks, start_row + 1, start_column, row_size, column_size) ||
26 passable(planks, start_row - 1, start_column, row_size, column_size) ||
27 passable(planks, start_row, start_column - 1, row_size, column_size));
28
29 planks[start_row][start_column] = 1;
30
31 return result;
32 }
33 }
34 }
35 // else
36 return false;
37}
38
39int main() {
40 ifstream file_input;
41 string file_path;
42 cin >> file_path;
43 file_input.open(file_path.c_str());
44 if (file_input.fail()) {
45 exit(1);
46 }
47 string row_column;
48 getline(file_input, row_column);
49 istringstream line_row_column(row_column);
50 int num_row, num_column;
51 line_row_column >> num_row >> num_column;
52 // vector of vector
53 vector<vector<int>> row(num_row, vector<int>(num_column));
54 for (int i = 0; i < num_row; i++) {
55 string line;
56 getline(file_input, line);
57 istringstream line_in(line);
58 for (int j = 0; j < num_column; j++) {
59 line_in >> row[i][j];
60 }
61 }
62 for (int first_row = 0; first_row < num_row; first_row++) {
63 if (passable(row, first_row, 0, num_row, num_column)) {
64 cout << "1" << endl;
65 return 0;
66 }
67 }
68 cout << "0" << endl;
69 return 0;
70}
bool passable(vector< vector< int > > planks, int start_row, int start_column, int row_size, int column_size)
Definition 2.cpp:8
int main()
Definition 2.cpp:39