forked from mohitsh/SPOJ
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlabyr1.cpp
103 lines (103 loc) · 1.91 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
// 2008-06-21
#include <cstdio>
#include <queue>
using namespace std;
#ifdef _MSC_VER
#define GC getchar
#else
#define GC getchar_unlocked
#endif
int main()
{
int T,i,x,y,X,Y,z;
static char a[1000][1000];
static bool visited[1000][1000];
static bool visited2[1000][1000];
scanf("%d",&T);
for (i=0; i<T; i++)
{
scanf("%d %d",&X,&Y);
for (y=0; y<Y; y++)
{
for (x=0; x<X; x++)
{
a[x][y]=GC();
visited[x][y]=false;
visited2[x][y]=false;
}
GC();
}
int n;
for (x=0; x<X; x++)
{
for (y=0; y<Y; y++)
{
if (a[x][y]=='#')
continue;
n=0;
if (x>0&&a[x-1][y]=='.')
n++;
if (x<X-1&&a[x+1][y]=='.')
n++;
if (y>0&&a[x][y-1]=='.')
n++;
if (y<Y-1&&a[x][y+1]=='.')
n++;
if (n==1) //corner
break;
}
if (n==1)
break;
}
if (x==X&&y==Y)
{
printf("Maximum rope length is 0.\n");
continue;
}
queue<int> qx;
queue<int> qy;
qx.push(x);
qy.push(y);
int x2,y2,z2;
while (!qx.empty())
{
x=qx.front(),y=qy.front();
qx.pop(),qy.pop();
if (visited[x][y])
continue;
visited[x][y]=true;
if (x>0&&a[x-1][y]=='.')
qx.push(x-1),qy.push(y);
if (x<X-1&&a[x+1][y]=='.')
qx.push(x+1),qy.push(y);
if (y>0&&a[x][y-1]=='.')
qx.push(x),qy.push(y-1);
if (y<Y-1&&a[x][y+1]=='.')
qx.push(x),qy.push(y+1);
x2=x,y2=y;
}
qx.push(x2);
qy.push(y2);
queue<int> qz;
qz.push(0);
while (!qx.empty())
{
x=qx.front(),y=qy.front(),z=qz.front();
qx.pop(),qy.pop(),qz.pop();
if (visited2[x][y])
continue;
visited2[x][y]=true;
if (x>0&&a[x-1][y]=='.')
qx.push(x-1),qy.push(y),qz.push(z+1);
if (x<X-1&&a[x+1][y]=='.')
qx.push(x+1),qy.push(y),qz.push(z+1);
if (y>0&&a[x][y-1]=='.')
qx.push(x),qy.push(y-1),qz.push(z+1);
if (y<Y-1&&a[x][y+1]=='.')
qx.push(x),qy.push(y+1),qz.push(z+1);
x2=x,y2=y,z2=z;
}
printf("Maximum rope length is %d.\n",z2);
}
return 0;
}