• ural 1008. Image Encoding


    1008. Image Encoding

    Time Limit: 2.0 second
    Memory Limit: 64 MB
    Problem illustration
    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

    inputoutput
    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;
    }
    

      

  • 相关阅读:
    Ubuntu18.04通过网线共享网络
    [转载]PyTorch上的contiguous
    [转载]集线器、交换机、路由器和全双工、半双工
    如何在交换机上组建局域网
    [转载]PyTorch中permute的用法
    Pytorch报错:cuda runtime error (59) : device-side assert triggered at /pytorch/aten/src/THC/generic/THCTensorMath.cu:26
    org.apache.commons.httpclient工具类(封装的HttpUtil)
    org.apache.commons.httpclient工具类
    kali下ll命令无法使用
    Kali Linux 下载、引导、安装
  • 原文地址:https://www.cnblogs.com/xiongqiangcs/p/3043020.html
Copyright © 2020-2023  润新知