URAL 1008 Image encoding [BFS]

题意:两种图像的打印

思路:BFS两种互换

#include <queue>
#include <stdio.h>
#include <iostream>
#include <string.h>
using namespace std;
typedef struct Point{
    int x,y;
}point;
int xx,yy;
bool map[12][12]={0};
bool out[12][12]={0};
bool did[12][12]={0};
void input(int n){
    xx=12,yy=12;
    int x,y;
    for(int i=0;i<n;i++){
        cin>>x>>y;
        map[x][y]=1;
        if(x<xx) xx=x,yy=y;
        else if(x==xx&&y<yy) yy=y;
    }
}
void bfs(int n){
    queue<point> q;
    point t;
    t.x=xx;
    t.y=yy;
    cout<<xx<<' '<<yy<<endl;
    q.push(t);
    int x,y;
    int count=0;
    out[xx][yy]=1;
    while(q.size()){
        t=q.front();
        q.pop();
        x=t.x;
        y=t.y;
        if(did[x][y]) continue;
        did[x][y]=1;
        if(map[x+1][y]&&!did[x+1][y]){
            if(!out[x+1][y]){
                cout<<'R';
                out[x+1][y]=1;
                t.x=x+1;
                t.y=y;
                q.push(t);
            }
        }
        if(map[x][y+1]&&!did[x][y+1]){
            if(!out[x][y+1]){
                cout<<'T';
                out[x][y+1]=1;
                t.x=x;
                t.y=y+1;
                q.push(t);
            }
        }
        if(map[x-1][y]&&!did[x-1][y]){
            if(!out[x-1][y]){
                cout<<'L';
                out[x-1][y]=1;
                t.x=x-1;
                t.y=y;
                q.push(t);
            }
        }
        if(map[x][y-1]&&!did[x][y-1]){
            if(!out[x][y-1]){
                cout<<'B';
                out[x][y-1]=1;
                t.x=x;
                t.y=y-1;
                q.push(t);
            }
        }
        count++;
        if(count!=n) cout<<','<<endl;
        else cout <<'.'<<endl;
    }
}
void bfs(int n,int m){
    queue<point> q;
    point t;
    t.x=n;
    t.y=m;
    map[n][m]=1;
    q.push(t);
    char s[5];
    int x,y;
    while(q.size()){
        t=q.front();
        q.pop();
        x=t.x;
        y=t.y;
        if(did[x][y]) continue;
        did[x][y]=1;
        cin>>s;
        for(int i=0;i<strlen(s)-1;i++){
            if(s[i]=='R'){
                map[x+1][y]=1;
                if(!did[x+1][y]){
                    t.x=x+1;
                    t.y=y;
                    q.push(t);
                }
            }
            else if(s[i]=='T'){
                map[x][y+1]=1;
                if(!did[x][y+1]){
                    t.x=x;
                    t.y=y+1;
                    q.push(t);
                }
            }
            else if(s[i]=='L'){
                map[x-1][y]=1;
                if(!did[x-1][y]){
                    t.x=x-1;
                    t.y=y;
                    q.push(t);
                }
            }
            else if(s[i]=='B'){
                map[x][y-1]=1;
                if(!did[x][y-1]){
                    t.x=x;
                    t.y=y-1;
                    q.push(t);
                }
            }
        }
        if(s[strlen(s)-1]=='.') break;
    }
    int count=0;
    for(int i=1;i<=10;i++)
        for(int j=1;j<=10;j++)
            if(map[i][j]) count++;
    cout<<count<<endl;
    for(int i=1;i<=10;i++)
        for(int j=1;j<=10;j++)
            if(map[i][j]) cout<<i<<' '<<j<<endl;
}
int main(){
    int n;
    cin>>n;
    if(getchar()=='\n'){
        input(n);
        bfs(n);
    }
    else{
        int m;
        cin>>m;
        bfs(n,m);
    }
    return 0;
}
/*测试:
Sample Input
6
2 3
2 4
3 3
3 4
4 2
4 3
Sample Output
2 3
RT,
RT,
,
B,
,
.
*/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值