-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLABYR1.cpp
111 lines (105 loc) · 2.33 KB
/
LABYR1.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
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
#include<iostream>
#include<vector>
#include<stack>
#include<limits>
using namespace std;
int main()
{
int T;
cin>>T;
while(T--)
{
int C=0, R=0;
cin>>C>>R;
struct point
{
int x;
int y;
};
cout<<"The value of C and R is "<<C<<" "<<R<<"\n";
vector<vector<char > > arr(C, vector<char >(R,0));
vector<vector<int > > visited(C, vector<int >(R,0));
vector<point > startPoints;
for (int i = 0; i < R; ++i)
{
for (int j = 0; j < C; ++j)
{
cin>>arr[i][j];
struct point startPoint;
if(arr[i][j] == '.')
{
startPoint.x = i;
startPoint.y = j;
startPoints.push_back(startPoint);
}
}
}
stack<struct point > S;
for (int i = 0; i < startPoints.size(); ++i)
{
S.push(startPoints[i]);
while(!S.empty())
{
struct point ab, pushPoint;
ab = S.top();
int flag = 0;
if(ab.x + 1 < R && (visited[ab.x + 1][ab.y] == 0) && arr[ab.x+1][ab.y] == '.')
{
pushPoint.x = ab.x + 1;
pushPoint.y = ab.y;
S.push(pushPoint);
visited[pushPoint.x][pushPoint.y] = visited[ab.x][ab.y] + 1;
flag = 1;
}
if( ab.x - 1 >= 0 && (visited[ab.x - 1][ab.y] == 0) && arr[ab.x-1][ab.y] == '.')
{
pushPoint.x = ab.x - 1;
pushPoint.y = ab.y;
S.push(pushPoint);
visited[pushPoint.x][pushPoint.y] = visited[ab.x][ab.y] + 1;
flag = 1;
}
if(ab.y + 1 < C && (visited[ab.x][ab.y + 1] == 0) && arr[ab.x][ab.y + 1] == '.')
{
pushPoint.x = ab.x;
pushPoint.y = ab.y + 1;
S.push(pushPoint);
visited[pushPoint.x][pushPoint.y] = visited[ab.x][ab.y] + 1;
flag = 1;
}
if(ab.y - 1 >= 0 && (visited[ab.x][ab.y - 1] == 0) && arr[ab.x][ab.y + 1] == '.')
{
pushPoint.x = ab.x;
pushPoint.y = ab.y - 1;
S.push(pushPoint);
visited[pushPoint.x][pushPoint.y] = visited[ab.x][ab.y] + 1;
flag = 1;
}
if(flag == 0)
{
S.pop();
}
}
}
int len = numeric_limits<int>::min();
cout<<"==========================Visited Array==================================="<<"\n";
for (int i = 0; i < R; ++i)
{
for (int j = 0; i < C; ++i)
{
cout<<visited[i][j]<<" ";
}
cout<<"\n";
}
for (int i = 0; i < R; ++i)
{
for (int j = 0; i < C; ++i)
{
if(len <= visited[i][j])
len = visited[i][j];
}
}
cout<<"Maximum rope length is "<<len<<"\n";
}
return 0;
}