Java-POJ1009-Edge Detection(未完成,有C++代码)

RLE编码,还不会,先搬运一下大佬的代码,理解之后再用Java自己实现

 1 #include <map>
 2 #include <vector>
 3 #include <cstdlib>
 4 #include <iostream>
 5 using namespace std;
 6 static int around[8][2]= {{-1,-1},{-1,0},{-1,1},{0,-1},{0,1},{1,-1},{1,0},{1,1}};
 7 map<int,int>::iterator a_It,b_It;
 8 map<int,int> a,b;
 9 vector<int> s;
10 int getValue(int index) {
11     int value=0;
12     for(a_It = a.begin(); a_It != a.end(); a_It++) {
13         if(index<a_It->first) break;
14         value=a_It->second;
15     }
16     return value;
17 }
18 int getMax(int index,int width,int count) {
19     int center=getValue(index);
20     int h=index/width;
21     int max=0;
22     for(int i=0; i<8; i++) {
23         int sub=index+around[i][0]*width+around[i][1];
24         if(h+around[i][0]==sub/width && sub>=0 && sub<count) {
25             int a=getValue(sub);
26             max=abs(center-a)>max?abs(center-a):max;
27         }
28     }
29     return max;
30 }
31 void process(int width,int count) {
32     for(int i=0; i < s.size(); i++) {
33         int index=s.at(i);
34         b.insert(pair<int,int>(index,getMax(index,width,count)));
35     }
36 }
37 int main() {
38     int width;
39     while(cin>>width && cout<<width<<endl && width) {
40         a.clear(),b.clear(),s.clear();
41         int v,l,count=0;
42         while(cin>>v>>l && l) { //v有可能为0
43             a.insert(pair<int,int>(count,v));
44             count+=l;
45         }
46         for(a_It = a.begin(); a_It != a.end(); a_It++) {
47             for(int i=-1; i<2; i++) {
48                 for(int j=-1; j<2; j++) {
49                     int index=a_It->first+i*width+j;
50                     if(index>=0 && index <count)
51                         s.push_back(index);
52                 }
53             }
54         }
55         s.push_back(width);
56         s.push_back(count-width);
57         process(width,count);
58 
59         b_It = b.begin();
60         int s=b_It->first;
61         int val=b_It->second;
62         b_It++;
63         for(; b_It != b.end(); b_It++) {
64             if(b_It->second != val) {
65                 cout<<val<<" "<<b_It->first-s<<endl;
66                 s=b_It->first;
67                 val=b_It->second;
68             }
69         }
70         cout<<val<<" "<<count-s<<endl;
71         cout<<"0 0"<<endl;
72     }
73     return 0;
74 }

猜你喜欢

转载自www.cnblogs.com/JasonCow/p/12238006.html