SRM,呵呵又是从看逖靖寒小哥博客开始了解的新东西。以前也倒也看到过n多次,都没有关注过,ACM的题倒做过一些,但限于算法功底太差坚持了没多久。近段时间正好在看算法导论,感觉收获不小,就想检验下我的学习成果。费了好半天功夫装上了TopCorder Arena,发现居然支持C#,迫不及待开始做题。到现在花了快两天的上班和下班时间终于做完144 DIV 1了,写上来记录一下。特别是1100分的题自己思路换了几次才找到一个还算可以接受的方法
第一题:300分
题意:
一个二进制数 011100011 被加密成 123210122 ,规则是加密后的每一位是之前的每一位和其前后相邻位的和,即如果P被加密为Q,则有Q[i] = P[i-1]+P[i]+P[i+1]。现要通过Q找出P,方法是猜测第一位为0,找出一个答案,再猜测为1,找出第二个答案。无解则返回NONE。
思路:
题目很简单,循环遍历一下Q就行了,主要是边界条件及有无解的条件需要判断清楚。第一个题做起来思路还有些紧,这些边界就错了好几次。最后改对了,代码也写得有点多
代码:
Code
1
using System;
2
using System.Collections.Generic;
3
using System.Text;
4![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
5
class BinaryCode
6![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![ContractedBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
7
public string[] decode(string message)
8![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
9
int[] Q = new int[message.Length];
10![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
11
for (int i = 0; i < message.Length; i++)
12![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
13
Q[i] = Int32.Parse(message[i].ToString());
14
}
15![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
16
string[] result = new string[2];
17
result[0] = decodeT(0, Q);
18
result[1] = decodeT(1, Q);
19
return result;
20
}
21![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
22
private string decodeT(int p0, int[] Q)
23![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
24
if (Q.Length == 1)
25![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
26
if (Q[0] != p0)
27![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
28
return "NONE";
29
}
30
else
31![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
32
return Q[0].ToString();
33
}
34
}
35
int[] P = new int[Q.Length];
36
P[0] = p0;
37
int value = p0, i = 0;
38
for (i = 1; i < Q.Length; i++)
39![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
40
P[i] = Q[i - 1] - value;
41
if (P[i] < 0 || P[i] > 1)
42
break;
43![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
44
value = P[i] + P[i - 1];
45
}
46![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
47
if (i < Q.Length || P[i - 1] + P[i - 2] != Q[i - 1])
48![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
49
return "NONE";
50
}
51
else
52![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
53
StringBuilder sb = new StringBuilder(Q.Length);
54
foreach (int p in P)
55![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
56
sb.Append(p);
57
}
58
return sb.ToString();
59
}
60
}
61
}
第二题:550分
题意:
彩票,输入的一个字符串为一种彩票,包含四个参数,分别是每一个数的最大值、集合长度、数是否需要排序、数是否需要唯一。如 "INDIGO: 93 8 T F" 表示一种从1到93中选8个数的彩票,8位数要从小到达排序,但每位数可以重复。要求是输入一个彩票字符串数组,要你根据你买每个彩票的中奖概率从大到小对彩票名进行排序后输出。
思路:
主要是在需要排序时计算概率比较麻烦。我这里是递归去算n位数的概率,并用了字典来作动态规划提高效率。估计有简单得多的数学方法可以实现这个效果。先写上,之后再思考思考
(汗,果然有简单得多的方法,整个问题用排列组合一算就OK了。特别是要排序可重复时也能转化为组合来计算,根本不用什么递归和字典了)
代码:
Code
1
using System;
2
using System.Collections.Generic;
3
using System.Text;
4![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
5
class Lottery
6![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![ContractedBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
7
private long[,] sortTemp = null;
8![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
9
public string[] sortByOdds(string[] rules)
10![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
11
Dictionary<string, long> dic = new Dictionary<string, long>(rules.Length);
12
foreach (string rule in rules)
13![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
14
string[] s = rule.Split(':');
15
dic.Add(s[0], GetCount(s[1]));
16
}
17![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
18
return sortResult(dic);
19
}
20![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
21
private long GetCount(string s)
22![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
23
string[] items = s.Trim().Split(' ');
24
int max = Convert.ToInt32(items[0]);
25
int count = Convert.ToInt32(items[1]);
26
bool isSort = (items[2] == "T");
27
bool isRepeatable = (items[3] == "T");
28
return GetCount(max, count, isSort, isRepeatable);
29
}
30![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
31
private long GetCount(int max, int count, bool isSort, bool isNoRepeate)
32![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
33
sortTemp = new long[max + 1, count + 1];
34
for (int i = 0; i <= max; i++)
35
for (int j = 0; j <= count; j++)
36
sortTemp[i, j] = -1;
37![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
38
if (isSort)
39![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
40
return sort(max, count, isNoRepeate);
41
}
42
else
43![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
44
return power(max, count, isNoRepeate);
45
}
46
}
47![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
48
private long sort(int max, int count, bool isNoRepeate)
49![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
50
if (sortTemp[max, count] != -1)
51![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
52
return sortTemp[max, count];
53
}
54
else
55![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
56
long result = 0;
57![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
58
if (count == 1)
59![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
60
result = max;
61
}
62
else
63![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
64
for (int i = 1; i <= max; i++)
65![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
66
result += sort(
67
isNoRepeate ? max - i : max - i + 1, count - 1, isNoRepeate);
68
}
69
}
70![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
71
sortTemp[max, count] = result;
72
return result;
73
}
74
}
75![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
76
private long power(int max, int count, bool isNoRepeate)
77![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
78
long result = 1;
79
for (int i = 0; i < count; i++)
80![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
81
result *= max;
82
if (isNoRepeate)
83
max--;
84
}
85
return result;
86
}
87![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
88
private string[] sortResult(Dictionary<string, long> dic)
89![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
90
string[] result = new string[dic.Count];
91
int t = 0;
92
while (dic.Count > 0)
93![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
94
string resultKey = null;
95
foreach (string key in dic.Keys)
96![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
97
if (resultKey == null)
98![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
99
resultKey = key;
100
}
101
else
102![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
103
if (dic[resultKey] > dic[key])
104![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
105
resultKey = key;
106
}
107
else if (dic[resultKey] == dic[key])
108![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
109
int i = 0, length = Math.Min(resultKey.Length, key.Length);
110
for (i = 0; i < length; i++)
111![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
112
if (resultKey[i] != key[i])
113
break;
114
}
115![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
116
if (i == length)
117![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
118
if (resultKey.Length > key.Length)
119
resultKey = key;
120
}
121
else if (resultKey[i] > key[i])
122
resultKey = key;
123
}
124
}
125
}
126
result[t++] = resultKey;
127
dic.Remove(resultKey);
128
}
129
return result;
130
}
131
}
第三题:1100分
题意:
输入是平面上一些平行于X或Y轴的线段的集合,以及一个数n,判断要用笔画线经过全部这些线段n次,至少需要从图上抬起几次笔尖(覆盖在一起的线不重复计算)?
思路:
我先判断出来在一个线段连接成的块中,如果n为偶数或块中奇点数为0则需要的划线次数为1,否则需要的划线次数为奇点数/2,其中奇点是指与其相连的线段有奇数条,其实这里就只可能有1条和3条。然后剩下的就是具体的分块及判断奇点等问题了,还是很复杂的,我最后的代码很长,也没作什么重构,可读性不高。做完后发现网上有人说这是欧拉回路,要求先分成几个连通子图等。可惜我对图论一窍不通,回去得翻翻
代码:
Code
1
using System;
2
using System.Collections.Generic;
3
using System.Text;
4![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
5
class PenLift
6![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![ContractedBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
7
public int numTimes(string[] segments, int n)
8![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
9
int result = -1;
10
List<Block> blockList = MadeBlock(segments);
11![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
12
if (n % 2 == 0)
13![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
14
result += blockList.Count;
15
}
16
else
17![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
18
foreach (Block block in blockList)
19![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
20
result += CalcBlock(block);
21
}
22
}
23
return result;
24
}
25![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
26
private List<Block> MadeBlock(string[] segments)
27![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
28
List<Block> blockList = new List<Block>();
29
foreach (string s in segments)
30![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
31
// load line.
32
string[] v = s.Split(' ');
33
Line line = new Line(
34
new Point(Convert.ToInt32(v[0]), Convert.ToInt32(v[1])),
35
new Point(Convert.ToInt32(v[2]), Convert.ToInt32(v[3])));
36![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
37
Block blockWithLine = null;
38
List<Block> tempBlockList = new List<Block>();
39
foreach (Block block in blockList)
40![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
41
bool isThisBlock = false;
42
if (line.IsHorizental)
43![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
44
foreach (int key in block.Horizontal.Keys)
45![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
46
if (key == line.Position)
47![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
48
foreach (Line oldLine in block.Horizontal[key])
49![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
50
if (line.Start <= oldLine.End && line.End >= oldLine.Start)
51![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
52
isThisBlock = true;
53
break;
54
}
55
else if (line.End < oldLine.Start)
56![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
57
break;
58
}
59
}
60![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
61
if (isThisBlock) break;
62
}
63
}
64![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
65
if (!isThisBlock)
66![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
67
foreach (int key in block.Vertical.Keys)
68![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
69
if (key >= line.Start && key <= line.End)
70![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
71
foreach (Line oldLine in block.Vertical[key])
72![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
73
if (line.Position >= oldLine.Start && line.Position <= oldLine.End)
74![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
75
isThisBlock = true;
76
break;
77
}
78
else if (oldLine.Start > line.Position)
79![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
80
break;
81
}
82
}
83![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
84
if (isThisBlock) break;
85
}
86
}
87
}
88![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
89
if (isThisBlock)
90![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
91
blockWithLine = (blockWithLine != null) ? CombineBlocks(blockWithLine, block) : block;
92![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
93
if (!blockWithLine.Horizontal.ContainsKey(line.Position))
94
blockWithLine.Horizontal.Add(line.Position, new List<Line>());
95
blockWithLine.Horizontal[line.Position] = InsertIntoList(blockWithLine.Horizontal[line.Position], line);
96
}
97
else
98![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
99
tempBlockList.Add(block);
100
}
101
}
102
else
103![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
104
foreach (int key in block.Vertical.Keys)
105![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
106
if (key == line.Position)
107![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
108
foreach (Line oldLine in block.Vertical[key])
109![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
110
if (line.Start <= oldLine.End && line.End >= oldLine.Start)
111![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
112
isThisBlock = true;
113
break;
114
}
115
else if (line.End < oldLine.Start)
116![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
117
break;
118
}
119
}
120![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
121
if (isThisBlock) break;
122
}
123
}
124![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
125
if (!isThisBlock)
126![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
127
foreach (int key in block.Horizontal.Keys)
128![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
129
if (key >= line.Start && key <= line.End)
130![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
131
foreach (Line oldLine in block.Horizontal[key])
132![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
133
if (line.Position >= oldLine.Start && line.Position <= oldLine.End)
134![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
135
isThisBlock = true;
136
break;
137
}
138
else if (oldLine.Start > line.Position)
139![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
140
break;
141
}
142
}
143![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
144
if (isThisBlock) break;
145
}
146
}
147
}
148![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
149
if (isThisBlock)
150![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
151
blockWithLine = (blockWithLine != null) ? CombineBlocks(blockWithLine, block) : block;
152![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
153
if (!blockWithLine.Vertical.ContainsKey(line.Position))
154
blockWithLine.Vertical.Add(line.Position, new List<Line>());
155
blockWithLine.Vertical[line.Position] = InsertIntoList(blockWithLine.Vertical[line.Position], line);
156
}
157
else
158![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
159
tempBlockList.Add(block);
160
}
161
}
162
}
163
if (blockWithLine == null)
164![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
165
blockWithLine = new Block();
166
if (line.IsHorizental)
167![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
168
blockWithLine.Horizontal.Add(line.Position, new List<Line>());
169
blockWithLine.Horizontal[line.Position] = InsertIntoList(blockWithLine.Horizontal[line.Position], line);
170
}
171
else
172![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
173
blockWithLine.Vertical.Add(line.Position, new List<Line>());
174
blockWithLine.Vertical[line.Position] = InsertIntoList(blockWithLine.Vertical[line.Position], line);
175
}
176
}
177
tempBlockList.Add(blockWithLine);
178
blockList = tempBlockList;
179
}
180
return blockList;
181
}
182![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
183
private Block CombineBlocks(Block block1, Block block2)
184![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
185
foreach (int key in block2.Horizontal.Keys)
186![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
187
if (block1.Horizontal.ContainsKey(key))
188![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
189
foreach (Line line in block2.Horizontal[key])
190![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
191
block1.Horizontal[key] = InsertIntoList(block1.Horizontal[key], line);
192
}
193
}
194
else
195![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
196
block1.Horizontal.Add(key, block2.Horizontal[key]);
197
}
198
}
199![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
200
foreach (int key in block2.Vertical.Keys)
201![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
202
if (block1.Vertical.ContainsKey(key))
203![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
204
foreach (Line line in block2.Vertical[key])
205![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
206
block1.Vertical[key] = InsertIntoList(block1.Vertical[key], line);
207
}
208
}
209
else
210![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
211
block1.Vertical.Add(key, block2.Vertical[key]);
212
}
213
}
214![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
215
return block1;
216
}
217![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
218
private List<Line> InsertIntoList(List<Line> list, Line line)
219![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
220
int place = -1, removeLength = 0;
221
for (int i = 0; i < list.Count; i++)
222![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
223
Line oldLine = list[i];
224![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
225
if (oldLine.Start > line.End)
226![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
227
// 直接加,跳出循环
228
if (place == -1) place = i;
229
break;
230
}
231
else if (oldLine.End >= line.Start && line.End >= oldLine.Start)
232![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
233
// 合并
234
if (place == -1) place = i;
235
line.Start = Math.Min(line.Start, oldLine.Start);
236
line.End = Math.Max(line.End, oldLine.End);
237
removeLength++;
238
}
239
}
240![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
241
if (place != -1)
242![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
243
list.RemoveRange(place, removeLength);
244
list.Insert(place, line);
245
}
246
else
247![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
248
list.Add(line);
249
}
250![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
251
return list;
252
}
253![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
254
private int CalcBlock(Block block)
255![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
256
int result = 0;
257
foreach (List<Line> vList in block.Vertical.Values)
258![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
259
result += vList.Count;
260![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
261
foreach (Line v in vList)
262![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
263
foreach (List<Line> hList in block.Horizontal.Values)
264![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
265
foreach (Line h in hList)
266![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
267
if (v.Position == h.Start || v.Position == h.End)
268![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
269
if (v.Start == h.Position || v.End == h.Position)
270
result--;
271
break;
272
}
273
else if (v.Position < h.Start)
274![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
275
break;
276
}
277
}
278
}
279
}
280
}
281![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
282
foreach (List<Line> hList in block.Horizontal.Values)
283![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
284
result += hList.Count;
285
}
286![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
287
return (result == 0) ? 1 : result;
288
}
289
}
290
public class Block
291![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![ContractedBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
292
public Dictionary<int, List<Line>> Horizontal = new Dictionary<int, List<Line>>();
293
public Dictionary<int, List<Line>> Vertical = new Dictionary<int, List<Line>>();
294
}
295![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
296
public class Line
297![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![ContractedBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
298
public Point StartPoint;
299
public Point EndPoint;
300
public Line(Point s, Point e)
301![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
302
if (s.X == e.X)
303![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
304
if (s.Y < e.Y)
305![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
306
StartPoint = s;
307
EndPoint = e;
308
}
309
else
310![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
311
StartPoint = e;
312
EndPoint = s;
313
}
314
}
315
else
316![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
317
if (s.X < e.X)
318![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
319
StartPoint = s;
320
EndPoint = e;
321
}
322
else
323![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
324
StartPoint = e;
325
EndPoint = s;
326
}
327
}
328
}
329![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
330
public bool IsHorizental
331![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
332
get
333![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
334
return StartPoint.Y == EndPoint.Y;
335
}
336
}
337![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
338
public int Start
339![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
340
get
341![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
342
return IsHorizental ? StartPoint.X : StartPoint.Y;
343
}
344
set
345![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
346
if (IsHorizental)
347
StartPoint.X = value;
348
else
349
StartPoint.Y = value;
350
}
351
}
352![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
353
public int End
354![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
355
get
356![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
357
return IsHorizental ? EndPoint.X : EndPoint.Y;
358
}
359
set
360![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
361
if (IsHorizental)
362
EndPoint.X = value;
363
else
364
EndPoint.Y = value;
365
}
366
}
367![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
368
public int Position
369![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
370
get
371![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
372
return IsHorizental ? StartPoint.Y : StartPoint.X;
373
}
374
set
375![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
376
if (IsHorizental)
377![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
378
StartPoint.Y = value;
379
EndPoint.Y = value;
380
}
381
else
382![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
383
StartPoint.X = value;
384
EndPoint.X = value;
385
}
386
}
387
}
388
}
389![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
390
public class Point
391![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![ContractedBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
392
public int X;
393
public int Y;
394
public Point(int x, int y)
395![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
396
X = x;
397
Y = y;
398
}
399![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
400
public override string ToString()
401![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
402
return string.Format("{0},{1}", X, Y);
403
}
404
}