URAL 1008 Image encoding [BFS]

来源:互联网 发布:阿里手机云空间 编辑:程序博客网 时间:2024/04/29 09:01
题意:两种图像的打印

思路: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 Input62 32 43 33 44 24 3Sample Output2 3RT,RT,,B,,.*/

原创粉丝点击