ural 1008. Image Encoding
2013-04-25 17:31
309 查看
1008. Image Encoding
Time Limit: 2.0 secondMemory Limit: 64 MB
There are several ways to encode an image. In this problem we will consider two representations of an image. We assume that the image consists of black and white pixels. There is at least one black pixel and all black pixels are connected with their sides. Coordinates of black pixels are not less than 1 and not greater than 10. An example of such an image is at the figure.
Both representations describe arrangement of black pixels only.
At the first representation we specify in the first line number of black pixels and coordinates of each black pixel in the following lines. Pixels are listed in order of increasing X. In case of equality of X they are listed in order of increasing Y. Image at the figure is encoded as follows:
6 2 3 2 4 3 3 3 4 4 2 4 3
At the second representation we specify in the first line coordinates of the lowest left black pixel. Each of the following lines contains a description of neighbors for one of the pixels. At first, neighbors of the lowest left pixel are specified, then neighbors of its first neighbor (if it exists) are specified, then neighbors of its second neighbor (if it also exists) follow. When all its neighbors are described the description of the neighbors of its first neighbor follows. The description of the neighbors of its second neighbor follows then and so on.
Each descriptive line contains at most one letter for each neighbor: R for the right, T for the top, L for the left, B for the bottom. If the neighbor was already specified it is not included into the descriptive line and vice-versa. Also there is only one descriptive line for each pixel. Neighbors are listed counter-clockwise starting with the right. Each descriptive line except the last ends with a comma. The last line ends with a full stop. Image at the figure is encoded as follows:
2 3 RT, RT, , B, , .
There are no leading or tailing spaces in any representation. There is exactly one space between X and Y coordinates.
Input
One representation of the image will be given to your program in the input.Output
Your program has to write other representation of the image to the output.Sample
input | output |
---|---|
6 2 3 2 4 3 3 3 4 4 2 4 3 | 2 3 RT, RT, , B, , . |
#include <iostream> #include <vector> #include <queue> #include <string> #include <sstream> #include <cstring> #include <algorithm> #define MAX 200 using namespace std; typedef struct{ int x,y; }Point; bool cmp(Point a,Point b){ if(a.x == b.x ) return a.y<b.y; return a.x<b.x; } int image[MAX][MAX]; const Point direct[]= {{1,0},{0,1},{-1,0},{0,-1}}; const string directStr[] = { "R","T","L","B"}; vector<string> neighbors; vector<string> firstToSecondBFS(Point start){ vector<string> ans; queue<Point> q; bool visit[MAX][MAX] = {false}; visit[start.x][start.y]=1; q.push(start); while(!q.empty()){ start=q.front(); q.pop(); string tmp=""; for(int i = 0 ; i < 4; i ++ ){ int xx = start.x+direct[i].x,yy=start.y+direct[i].y; if(!visit[xx][yy] && image[xx][yy] == 1) { tmp += directStr[i]; visit[xx][yy] = true; Point newPoint; newPoint.x=xx;newPoint.y=yy; q.push(newPoint); } } if(q.empty() && tmp == "")continue; tmp +=","; ans.push_back(tmp); } return ans; } vector<Point> secondToFirstBFS(Point start){ queue<Point> q; vector<Point> ans; image[start.x][start.y]=1; q.push(start); ans.push_back(start); int k =0; while(!q.empty() && k < neighbors.size()){ start = q.front(); q.pop(); for(int i =0; i< neighbors[k].length()-1; i ++ ){ Point tmp=start; if(neighbors[k][i] == 'R'){ tmp.x+=1; q.push(tmp); if(image[tmp.x][tmp.y] == 0){ ans.push_back(tmp); image[tmp.x][tmp.y] =1; } } else if(neighbors[k][i] == 'T'){ tmp.y +=1; q.push(tmp); if(image[tmp.x][tmp.y] == 0){ ans.push_back(tmp); image[tmp.x][tmp.y] =1; } } else if(neighbors[k][i] == 'L'){ tmp.x -=1; q.push(tmp); if(image[tmp.x][tmp.y] == 0){ ans.push_back(tmp); image[tmp.x][tmp.y] =1; } } else if(neighbors[k][i] == 'B'){ tmp.y -=1; q.push(tmp); if(image[tmp.x][tmp.y] == 0){ ans.push_back(tmp); image[tmp.x][tmp.y] =1; } } } k++; } return ans; } int main(){ string tmp; getline(cin,tmp); if( tmp.length() > 2){ Point start; stringstream sst(tmp); sst >> start.x >> start.y; while(cin >> tmp && tmp !=".") neighbors.push_back(tmp); vector<Point> ans = secondToFirstBFS(start); sort(ans.begin(),ans.end(),cmp); cout<<ans.size()<<endl; for(int i = 0; i < ans.size(); i++){ cout<<ans[i].x<<" "<<ans[i].y<<endl; } } else{ int n; stringstream sst(tmp); sst >> n; Point tmpPoint,start; memset(image,0,sizeof(image)); for(int i = 0; i < n; i ++ ){ cin >> tmpPoint.x>>tmpPoint.y; if( i == 0) start=tmpPoint; image[tmpPoint.x][tmpPoint.y] = 1; } vector<string> res = firstToSecondBFS(start); cout<<start.x<<" "<<start.y<<endl; for(int i = 0; i < res.size(); i ++ ){ cout<<res[i]<<endl; } cout<<"."<<endl; } return 0; }
相关文章推荐
- URAL1961:Cantonese Dialect
- URAL - 1823 Ideal Gas(审题)
- URAL1147 Shaping Regions
- URAL1002——DP——K-based Numbers. Version 2(未AC)
- URAL1081——DP—— Binary Lexicographic Sequence
- URAL1031——DP——Railway Tickets
- Ural 2018 The Debut Album
- URAL 1025. Democracy in Danger (贪心)
- URAL 1502. Domino Dots (找规律)
- URAL 1796. Amusement Park (math)
- 树形DP URAL 1039 Anniversary Party
- URAL 1021 Sacrament of the Sum
- URAL 1942 Attack at the Orbit
- ural 1058 chocolate
- URAL 1030 TITANNIC
- URAL 2046 A - The First Day at School 模拟题
- Ural 1780 Gray Code 乱搞暴力
- URAL1671 Anansi's Cobweb(离线做 + 并查集)
- ural 1272. Non-Yekaterinburg Subway
- URAL 1225 Flags dp练习