Edge Detection
| Time Limit: 1000MS | | Memory Limit: 10000K |
Description
IONU Satellite Imaging, Inc. records and stores very large images using run length encoding. You are to write a program that reads a compressed image, finds the edges in the image, as described below, and outputs another compressed image of the detected edges.
A simple edge detection algorithm sets an output pixel's value to be the maximum absolute value of the differences between it and all its surrounding pixels in the input image. Consider the input image below:
image
The upper left pixel in the output image is the maximum of the values |15-15|,|15-100|, and |15-100|, which is 85. The pixel in the 4th row, 2nd column is computed as the maximum of |175-100|, |175-100|, |175-100|, |175-175|, |175-25|, |175-175|,|175-175|, and |175-25|, which is 150.
Images contain 2 to 1,000,000,000 (10 9) pixels. All images are encoded using run length encoding (RLE). This is a sequence of pairs, containing pixel value (0-255) and run length (1-10 9). Input images have at most 1,000 of these pairs. Successive pairs have different pixel values. All lines in an image contain the same number of pixels.Input
Input consists of information for one or more images. Each image starts with the width, in pixels, of each image line. This is followed by the RLE pairs, one pair per line. A line with 0 0 indicates the end of the data for that image. An image width of 0 indicates there are no more images to process. The first image in the example input encodes the 5x7 input image above.
Output
Output is a series of edge-detected images, in the same format as the input images, except that there may be more than 1,000 RLE pairs.
Sample Input
7
15 4
100 15
25 2
175 2
25 5
175 2
25 5
0 0
10
35 500000000
200 500000000
0 0
3
255 1
10 1
255 2
10 1
255 2
10 1
255 1
0 0
0Sample Output
7
85 5
0 2
85 5
75 10
150 2
75 3
0 2
150 2
0 4
0 0
10
0 499999990
165 20
0 499999990
0 0
3
245 9
0 0
0Hint
A brute force solution that attempts to compute an output value for every individual pixel will likely fail due to space or time constraints.
这道题解了三天,主要需要注意的就是求解的方法。
暴力求解肯定是超时,看了很多前辈的解题思路,提到了一种跳跃式编程的方法。
只计算每第一个发生变化的点和其周围八个点的值。
还有几个特殊点:
第一个点、最后一个点、最后一行的第一个点 和
如果变化的点是最后一个点,下一行第一个点也要求解。
把问题转化为这个思路就容易多了。
#include <iostream>
#include <algorithm>
#include <map>
#include <vector>
using namespace std;
map<int, int> pixelMap;
map<vector<int>, int> calcMap;
map<int, int> valueMap;
int maxCnt;
int n;
int maxCol;
int getNum(int index) {
map<int, int>::iterator it_getNum;
it_getNum = pixelMap.lower_bound(index);
if (it_getNum != pixelMap.end()) {
return (*it_getNum).second;
}
}
//计算要计算的点位,并且计算周边8个点
void calcValueFunc(vector<int> vec, bool calcMore) {
int maxNum = 0;
int curKey = vec[0] * n + vec[1];
if (valueMap.count(curKey) != 0) {
return;
}
if ((!calcMore) && curKey > maxCnt) {
return;
}
int curNum = getNum(curKey);
for (int i = vec[0] - 1; i <= vec[0] + 1; i++) {
for (int j = vec[1] - 1; j <= vec[1] + 1; j++) {
if (i == vec[0] && j == vec[1]) {
continue;
}
if (i >= 0 && i <= maxCol && j >= 0) {
vector<int> pos;
pos.push_back(i);
pos.push_back(j);
if (j < n) {
int calcValue = abs((getNum(i * n + j) - curNum));
if (calcValue > maxNum) {
maxNum = calcValue;
}
}
else
{
pos.clear();
pos.push_back(i + 1);
pos.push_back(0);
}
if (calcMore) {
calcValueFunc(pos, false);
}
}
}
}
if (curKey <= maxCnt) {
valueMap[curKey] = maxNum;
}
}
int main()
{
while(cin >> n) {
if (n == 0) {
cout << "0" << endl;
break;
}
cout << n << endl;
pixelMap.clear();
calcMap.clear();
valueMap.clear();
maxCnt = 0;
int num, cnt;
int index = 0;
bool isEnd = false;
while (cin >> num >> cnt) {
if (num == 0 && cnt == 0) {
isEnd = true;
cnt = 1;
//多计算一个特殊点
vector<int> pos;
pos.push_back((index - 1) / n);
pos.push_back((index - 1) % n);
pos.push_back(index - 1);
calcMap[pos] = num;
}
//计算当前要算的值的位置
vector<int> pos;
pos.push_back(index / n);
pos.push_back(index % n);
pos.push_back(index);
calcMap[pos] = num;
if (isEnd) {
break;
}
index += cnt;
pixelMap[index - 1] = num;
}
maxCnt += index - 1;
maxCol = (maxCnt - 1) / n;
//多计算一个特殊点
vector<int> pos;
pos.push_back(maxCol);
pos.push_back(0);
pos.push_back(maxCol*n);
calcMap[pos] = num;
map<vector<int>, int>::iterator iter;
for (iter = calcMap.begin(); iter != calcMap.end(); ++iter) {
//计算当前的value,和周围8个点的value
if (valueMap.count((*iter).first[2]) == 0) {
calcValueFunc((*iter).first, true);
}
}
map<int, int>::iterator it;
int curUseNum = -1;
int curUseCnt = 0;
int lastCnt = -1;
for (it = valueMap.begin(); it != valueMap.end(); it++) {
if ((*it).second != curUseNum) {
curUseCnt += (*it).first - lastCnt - 1;
if (curUseNum != -1) {
cout << curUseNum << " " << curUseCnt << endl;
}
curUseNum = (*it).second;
curUseCnt = 1;
lastCnt = (*it).first;
}
else
{
curUseCnt += (*it).first - lastCnt;
lastCnt = (*it).first;
}
}
if (lastCnt < maxCnt) {
curUseCnt += maxCnt - lastCnt;
}
cout << curUseNum << " " << curUseCnt << endl;
cout << "0 0" << endl;
}
return 0;
}