1008. Image Encoding
Memory Limit: 64 MB
2 3
2 4
3 3
3 4
4 2
4 3
RT,
RT,
,
B,
,
.
Input
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; }