-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path03-Maze.cpp
82 lines (70 loc) · 1.73 KB
/
03-Maze.cpp
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
/*
Design and implement efficient data structure and algorithm for finding shortest
path from the start cell s to the destination cell t in a maze of dimension m x
n.
date:2020-10-26
*/
#include <iostream>
using namespace std;
class llNode {
private:
char value;
int row_pos;
int column_pos;
llNode *next;
public:
llNode() : next(NULL){};
llNode(char a) : value(a), next(NULL){};
friend class LinkedList;
};
class LinkedList {
private:
llNode *first;
public:
LinkedList() : first(NULL){};
void PrintList();
void addNode(int newrow_pos, int newcolumn_pos, char newvalue);
void printMatrix(int m, int n);
int search(LinkedList A, int row_index, int column_index);
};
int main() {
int m, n, pos;
char value;
LinkedList list1, list2, list3;
cout << "Enter the number of row and column:" << endl;
cin >> m >> n;
cout << "Enter the element of matrix A:" << endl;
for (int i = 0; i < m; i++) {
while (cin >> pos && pos != 0 && pos <= n) {
cin >> value;
pos = pos - 1;
list1.addNode(i, pos, value);
}
if (pos > n) {
cout << "It's not a valid value." << endl;
break;
}
}
}
void LinkedList::addNode(int newrow_pos, int newcolumn_pos, char newvalue) {
llNode *newNode = new llNode(newvalue);
llNode *temp = new llNode();
llNode *tempNew = new llNode();
if (first == NULL) {
temp->value = newvalue;
temp->row_pos = newrow_pos;
temp->column_pos = newcolumn_pos;
temp->next = NULL;
first = temp;
} else {
temp = first;
while (temp->next != NULL) {
temp = temp->next;
}
tempNew->value = newvalue;
tempNew->row_pos = newrow_pos;
tempNew->column_pos = newcolumn_pos;
tempNew->next = NULL;
temp->next = tempNew;
}
}