-
Notifications
You must be signed in to change notification settings - Fork 0
/
OJ3.cc
158 lines (148 loc) · 3.79 KB
/
OJ3.cc
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#include <cstdio>
using namespace std;
struct Rem
{
unsigned size;
short x;
short y;
};
struct pt
{
unsigned short x;
unsigned short y;
};
bool Mat[1000][1000];
bool AddedMat[1000][1000] = {false};
unsigned N;
unsigned maxSize = 0;
Rem arr[100000];
pt PtList[100000];
unsigned PtInd = 0;
unsigned currentInd = 0;
unsigned GetSize(short x, short y)
{
if (x < 0 || y < 0 || x >= N || y >= N || Mat[x][y] == false || AddedMat[x][y] == true)
return 0;
else
{
AddedMat[x][y] = true;
unsigned l1, l2, l3, l4, l5, l6, l7, l8;
l1 = GetSize(x - 1, y - 1);
l2 = GetSize(x - 1, y);
l3 = GetSize(x - 1, y + 1);
l4 = GetSize(x, y - 1);
l5 = GetSize(x, y + 1);
l6 = GetSize(x + 1, y - 1);
l7 = GetSize(x + 1, y);
l8 = GetSize(x + 1, y + 1);
return l1 + l2 + l3 + l4 + l5 + l6 + l7 + l8 + 1;
}
}
inline void GetMax()
{
for (short i = 0; i < N; i++)
for (short j = 0; j < N; j++)
if (Mat[i][j] == true && AddedMat[i][j] == false)
{
unsigned sizee = GetSize(i, j);
if (currentInd > 0)
{
if (sizee > arr[currentInd - 1].size)
{
arr[0].x = i;
arr[0].y = j;
arr[0].size = sizee;
currentInd = 1;
}
else if (sizee == arr[currentInd - 1].size)
{
arr[currentInd].x = i;
arr[currentInd].y = j;
arr[currentInd].size = sizee;
currentInd++;
}
else
continue;
}
else
{
arr[0].x = i;
arr[0].y = j;
arr[0].size = sizee;
currentInd++;
}
}
}
void GetPtList(unsigned x, unsigned y)
{
if (x < 0 || y < 0 || x >= N || y >= N || Mat[x][y] == false || AddedMat[x][y] == true)
return;
else
{
AddedMat[x][y] = true;
PtList[PtInd].x = x;
PtList[PtInd].y = y;
PtInd++;
GetPtList(x - 1, y - 1);
GetPtList(x - 1, y);
GetPtList(x - 1, y + 1);
GetPtList(x, y - 1);
GetPtList(x, y + 1);
GetPtList(x + 1, y - 1);
GetPtList(x + 1, y);
GetPtList(x + 1, y + 1);
}
}
void PtListSort()
{
for (unsigned i = 0; i < PtInd; i++)
{
unsigned minInd = i;
for (unsigned j = i; j < PtInd; j++)
{
if (PtList[j].x < PtList[minInd].x || (PtList[j].x == PtList[minInd].x && PtList[j].y < PtList[minInd].y))
minInd = j;
}
if (minInd != i)
{
pt temp = PtList[minInd];
PtList[minInd] = PtList[i];
PtList[i] = temp;
}
}
}
inline void Output()
{
printf("%u", arr[0].size);
for (int i = 0; i < currentInd; i++)
{
for (unsigned short j = 0; j < N; j++)
for (unsigned short k = 0; k < N; k++)
AddedMat[j][k] = false;
PtInd = 0;
GetPtList(arr[i].x, arr[i].y);
PtListSort();
for (unsigned j = 0; j < PtInd; j++)
printf("(%hu,%hu)", PtList[j].x, PtList[j].y);
printf("\n");
}
}
int main()
{
scanf("%u", &N);
for (unsigned i = 0; i < N; i++)
for (unsigned j = 0; j < N; j++)
{
int c = 0;
scanf("%d", &c);
if (c == 1)
Mat[i][j] = true;
else
Mat[i][j] = false;
}
GetMax();
if (currentInd == 0)
printf("-1");
else
Output();
}