-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcodeforces - 1085C - Connect Three.cpp
145 lines (133 loc) · 2.59 KB
/
codeforces - 1085C - Connect Three.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
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
#include<bits/stdc++.h>
using namespace std;
typedef pair<int, int> mypair;
vector<mypair> v;
stack<mypair> s;
set<mypair> st;
mypair dummy;
int dist[4];
int a, b;
bool isEligible()
{
if(s.top()!=dummy)
{
if(binary_search(st.begin(), st.end(), dummy))
{
return true;
}
}
st.insert(dummy);
return false;
}
void increasingX(mypair from, mypair to)
{
for(int i=from.first; i<=to.first; i++)
{
dummy = make_pair(i, from.second);
if(isEligible())
{
s.push(dummy);
}
}
}
void decreasingX(mypair from, mypair to)
{
for(int i=from.first; i>=to.first; i--)
{
dummy = make_pair(i, from.second);
if(isEligible())
{
s.push(dummy);
}
}
}
void increasingY(mypair from, mypair to)
{
for(int i=from.second+1; i<=to.second; i++)
{
dummy = make_pair(from.first, i);
if(isEligible())
{
s.push(dummy);
}
}
}
void decreasingY(mypair from, mypair to)
{
for(int i=from.second-1; i>=to.second; i--)
{
dummy = make_pair(from.first, i);
if(isEligible())
{
s.push(dummy);
}
}
}
void gofrom(mypair from, mypair to)
{
if(from.first < to.first)
{
increasingX(from, to);
}
else
{
decreasingX(from, to);
}
from.first = to.first;
if(from.second < to.second)
{
increasingY(from, to);
}
else
{
decreasingY(from, to);
}
}
int main()
{
//freopen("_in.txt", "r", stdin);
ios_base::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
int x, y;
for(int i=0; i<3; i++)
{
cin >> x >> y;
v.push_back(make_pair(x, y));
}
int i=0;
s.push(make_pair(-1, -1));
st.insert(make_pair(-1, -1));
do
{
a = fabs(v[i].first-v[(i+1)%3].first);
b = fabs(v[i].second-v[(i+1)%3].second);
dist[i] = a+b;
i = (i+1)%3;
} while(i!=0);
dist[3] = dist[0];
int k = numeric_limits<int>::max();
int minindex;
for(int i=0; i<3; i++)
{
if(dist[i]+dist[(i+1)%3] < k)
{
minindex = i;
k = dist[i];
}
}
int startindex = minindex;
int endindex = (minindex+3)%3;
do
{
gofrom(v[startindex], v[(startindex+1)%3]);
startindex = (startindex+1)%3;
} while(startindex!=endindex);
k = s.size()-1;
cout << k << "\n";
while(s.size()>1)
{
cout << s.top().first << " " << s.top().second << "\n";
s.pop();
}
return 0;
}