编程介的小学生 2017-03-27 14:04 采纳率: 20.5%
浏览 778
已采纳

Fax Regions

A fax image is a rectangular array of dark and white pixels. Three small examples are shown above much magnified so individual pixels are clearly visible. Your task is to write a program that will count the connected dark components in fax images. We assume that two dark pixels that are directly adjacent vertically or horizontally are in the same component. Pixels along a diagonal, touching only at a corner, are not directly connected. The two components in Fax 1 and three components in Fax 2 are shown below in different shadings. In Fax 3, all 32 dark pixels are in separate components, because the dark pixels only touch at corners.

Fax images are encoded to save transmission bandwidth. If you imagine a blank row above the actual fax, then each pixel in the fax can be labeled as being the same (S) as the pixel above it or different (D) from the pixel above it, as illustrated below.

If the S and D labels are read in row major order (from left to right across rows and then down to further rows), then the labels for the pixels in the three faxes are

Fax 1: SDDSDDSSSDDDSDD
Fax 2: DDDDDDDSSDDDDSSSDSDSSSDSSDSSSSSSDSSSSSSSSSSSSSDSSSDSDDDS
Fax 3: DSDSDSDSDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD

If we count the repetitions, always starting with S��s (even if there are 0 S��s at the beginning), then we get

Fax 1: 1S 2D 1S 2D 3S 3D 1S 2D
Fax 2: 0S 7D 2S 4D 3S 1D 1S 1D 3S 1D 2S 1D 6S 1D 13S 1D 3S 1D 1S 3D 1S
Fax 3: 0S 1D 1S 1D 1S 1D 1S 1D 1S 56D

Since the runs of S and D always alternate, we can omit the S and D labels, and get the final encodings.

Fax 1: 1 2 1 2 3 3 1 2
Fax 2: 0 7 2 4 3 1 1 1 3 1 2 1 6 1 13 1 3 1 1 3 1
Fax 3: 0 1 1 1 1 1 1 1 1 56

Starting from fax widths and encodings, your task is to calculate the number of components in the faxes. To make things more interesting, the faxes may be very large.

Input

There are from one to 24 data sets, followed by a final line containing only -1. A data set starts with a line containing three integers w, r, and g: the width of the fax in pixels, the total number of runs, and the number of run lengths grouped on one line, respectively. All three numbers are positive: w <= 1,000,000,000, r <= 1000, and g <= 40. The rest of the dataset consists of r run lengths, with a new line starting after each group of g run lengths. The last line (possibly the only line) of run lengths may contain fewer than g run lengths. The numbers on each line are blank separated. The first run length may be 0. All others run lengths are positive. No run length may be greater than 1,000,000,000. The total number of pixels in each fax will be a multiple of w, so the pixels form a rectangle. Though commas are shown in the long numbers above for human readability, the integers in the input and output files include no commas.

Output

For each dataset the output contains a line with one integer: the number of components in the fax. No fax encoded in the input will have more than 1,000,000,000 components. Caution: a solution that examines each pixel individually will not finish within the one-minute time limit.

Sample Input

5 8 4
1 2 1 2
3 3 1 2
7 21 8
0 7 2 4 3 1 1 1
3 1 2 1 6 1 13 1
3 1 1 3 1
8 10 10
0 1 1 1 1 1 1 1 1 56
-1

Sample Output

2
3
32

  • 写回答

2条回答 默认 最新

  • threenewbee 2017-04-06 15:50
    关注
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?