ural 1008 Image Encoding bfs

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

input output
6
2 3
2 4
3 3
3 4
4 2
4 3
2 3
RT,
RT,
,
B,
,
.


题目要求给出任意一种像素表示方法 然后输出任意一种 
两个bfs
#include<stdio.h>
#include<string.h>
bool mark[12][12];
bool vis[12][12];
struct node
{
    int x,y;
}queue[110],u,v;
int dx[5]={0,1,0,-1,0};
int dy[5]={0,0,1,0,-1};
int a;
int xx;
char str[110][6];
void bfs1(int x1,int y1)
{
    int fr;
    int ed;
    fr=ed=0;
    queue[0].x=x1;
    queue[0].y=y1;
    ed++;
    int i;
    vis[x1][y1]=1;
    while(fr<ed)
    {
        u=queue[fr++];
        for(i=1;i<=4;i++)
        {
            v.x=u.x+dx[i];
            v.y=u.y+dy[i];
            if(mark[v.x][v.y]==1&&vis[v.x][v.y]==0)
            {
                if(i==1)
                    printf("R");
                else if(i==2)
                    printf("T");
                else if(i==3)
                    printf("L");
                else if(i==4)
                    printf("B");
                vis[v.x][v.y]=1;
                queue[ed++]=v;
            }
        }
        if(fr<a)
        puts(",");
        else puts(".");
    }
}
void bfs2(int x1,int y1)
{
    int fr,ed;
    fr=ed=0;
    queue[0].x=x1;
    queue[0].y=y1;
    ed++;
    int i;
    while(fr<ed)
    {
        int l=strlen(str[fr]);
        u=queue[fr++];
        if(l==1)
            continue;
        for(i=0;i<l-1;i++)
        {
            if(str[fr-1][i]=='R')
            {
                v.x=u.x+1;
                v.y=u.y;
            }
            else if(str[fr-1][i]=='T')
            {
                v.x=u.x;
                v.y=u.y+1;
            }
            else if(str[fr-1][i]=='L')
            {
                v.x=u.x-1;
                v.y=u.y;
            }
            else if(str[fr-1][i]=='B')
            {
                v.x=u.x;
                v.y=u.y-1;
            }
            mark[v.x][v.y]=1;
            queue[ed++]=v;
        }
    }
}
int main()
{
    int b;
    while(scanf("%d%d",&a,&b)!=EOF)
    {
       memset(mark,0,sizeof(mark));
       memset(vis,0,sizeof(vis));
       xx=0;
       scanf("%s",str[xx++]);
       if(str[0][0]>='1'&&str[0][0]<='9')
       {
           int x1=b;
           int y1=str[0][0]-'0';
           int x,y;
           mark[x1][y1]=1;
           for(int i=1;i<a;i++)
           {
               scanf("%d%d",&x,&y);
               mark[x][y]=1;
           }
           printf("%d %d\n",x1,y1);
           bfs1(x1,y1);
       }
       else
       {
           mark[a][b]=1;
           while(scanf("%s",str[xx])!=EOF&&str[xx][0]!='.')
           {
               xx++;
           }
           bfs2(a,b);
           int count=0;
           for(int i=1;i<=10;i++)
           {
               for(int j=1;j<=10;j++)
               {
                   if(mark[i][j]==1)
                    count++;
               }
           }
           printf("%d\n",count);
           for(int i=1;i<=10;i++)
           {
               for(int j=1;j<=10;j++)
               {
                   if(mark[i][j]==1)
                    printf("%d %d\n",i,j);
               }
           }
       }
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值