1008. Image Encoding(BFS)

题意:图像的两种表示的相互转化。

好久没有做题了,相当的郁闷啊。。。wa了好久。。

#include <cstdio>
#include <cstring>
#include <cmath>
#include <iostream>
#include <algorithm>
#include <stack>
#include <queue>
#include <vector>
#include <map>
#include <string>

using namespace std;
const int N = 19;
int v[N][N];
int n;
struct NOD{
    int x,y;
};
queue<NOD> que;
int dx[] = {1,0,-1,0};
int dy[] = {0,1,0,-1};
bool oor(int x,int y)
{
    if(x<1||x>10) return false;
    if(y<1||y>10) return false;
    return true;
}
char dd[]="RTLB";
void solve1()
{
    int x,y=-1;
    for(int i=1;i<=10;i++)
    {
        if(y!=-1) break;
        for(int j=1;j<=10;j++)
        if(v[i][j])
        {
            x = i,y = j;
            break;
        }
    }
    while(!que.empty()) que.pop();
    NOD e,t;
    e.x = x,e.y = y;
    que.push(e);
    printf("%d %d\n",x,y);
    v[x][y] = 0;
    while(!que.empty())
    {
        e = que.front();que.pop();
        for(int i=0;i<4;i++)
        {
            int tx = e.x + dx[i],ty = e.y+dy[i];
            if(v[tx][ty])
            {
                printf("%c",dd[i]);
                v[tx][ty] = 0 ;
                t.x = tx,t.y = ty;
                que.push(t);
            }
        }
        if(que.empty()) printf(".\n");
        else  printf(",\n");
    }
}
void solve2(int x,int y)
{
    NOD e,t;
    e.x = x,e.y = y;
    v[x][y] =  1;
    while(!que.empty()) que.pop();
    que.push(e);
    char ch[10];
    int ans = 1;
    while(!que.empty())
    {
        e = que.front(); que.pop();
        scanf("%s",ch);
        for(int i=0;i<strlen(ch);i++)
        if(ch[i]=='R')
        {
            t.x = e.x+1,t.y = e.y;
            v[t.x][t.y] = 1;
            que.push(t);ans++;
        }else if(ch[i]=='T')
        {
            t.x = e.x,t.y = e.y+1;
            v[t.x][t.y] = 1;
            que.push(t);ans++;
        }else if(ch[i]=='L')
        {
            t.x = e.x-1,t.y = e.y;
            v[t.x][t.y] = 1;
            que.push(t);ans++;
        }else if(ch[i]=='B')
        {
            t.x = e.x,t.y = e.y-1;
            v[t.x][t.y] = 1;
            que.push(t);ans++;
        }
    }
    printf("%d\n",ans);
    for(int i=1;i<11;i++)
    for(int j=1;j<11;j++)
    if(v[i][j])
    {
        printf("%d %d\n",i,j);
    }
}
int main()
{
    //freopen("in.txt","r",stdin);
    char ch[30];
    while(gets(ch))
    {
        if(strlen(ch)>0) break;
    }
    int t = 0;
    for(int i=0;i<strlen(ch);i++)
    if(ch[i]==' ')
    t = 1;
    int a,b;
    if(t==0)
    {
        sscanf(ch,"%d",&n);
        for(int i=0;i<n;i++)
        {
            scanf("%d%d",&a,&b);
            //cout<<a<<" "<<b<<endl;
            v[a][b] = 1;
        }
        solve1();
    }else
    {
        sscanf(ch,"%d%d",&a,&b);
        solve2(a,b);
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值