-
Notifications
You must be signed in to change notification settings - Fork 7
/
LABYR1.cpp
64 lines (64 loc) · 1.18 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
#include "bits/stdc++.h"
using namespace std;
int r, c;
char arr[1005][1005];
char visit[1005][1005];
pair<int, int>last;
int len;
const int dx[] = { 0,0,1,-1 };
const int dy[] = { 1,-1,0,0 };
bool isSafe(int i, int j)
{
return (i >= 0) && (j >= 0) && (i < r) && (j < c);
}
void dfs(int i,int j,int l)
{
visit[i][j] = 1;
if (l > len)
{
last = make_pair(i, j);
len = l;
}
for (int k = 0; k < 4; k++)
{
if (i + dx[k]>=0&& j + dy[k]>=0&& i + dx[k]<r&& j + dy[k] <c && (visit[i + dx[k]][j + dy[k]]==0) && arr[i + dx[k]][j + dy[k]] == '.')
{
dfs(i + dx[k], j + dy[k], l + 1);
}
}
}
int main()
{
//READ;
int t;
scanf("%d", &t);
while (t--)
{
scanf("%d%d", &c, &r);
for (int i = 0; i < r; i++)
{
scanf("%s", arr[i]);
}
for (int i = 0; i < r; i++)
{
for (int j = 0; j < c; j++)
{
if (arr[i][j] == '.')
{
//printf("%d %d\n", i, j);
memset(visit, 0, sizeof(visit));
len = -1;
dfs(i, j, 0);
memset(visit, 0, sizeof(visit));
len = -1;
//printf("%d %d\n", last.first, last.second);
dfs(last.first, last.second, 0);
printf("Maximum rope length is %d.\n", len);
goto bnsl;
}
}
}
bnsl:;
}
return 0;
}