1008. Image Encoding(bfs)

时间:2023-03-10 01:20:37
1008. Image Encoding(bfs)

1008

没营养的破题

 #include <iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<stdlib.h>
#include<queue>
using namespace std;
typedef struct node
{
int x,y;
}st;
int vis[][],w[][];
int n,o[],dis[][] = {{,},{,},{-,},{,-}};
int s[][],gg;
char ss[][];
st p[];
int judge(int x,int y)
{
if(x<||x>||y<||y>)
return ;
if(!w[x][y])
return ;
return ;
}
char swit(int x)
{
if(x==)
return 'R';
else if(x==)
return 'T';
else if(x==)
return 'L';
else
return 'B';
}
void bfs()
{
int i,g=,j;
queue<st>q;
st te,tt;
q.push(p[]);
vis[p[].x][p[].y] = ;
while(!q.empty())
{
te = q.front();
q.pop();
g++;
for(i = ; i < ; i++)
{
int tx = te.x+dis[i][];
int ty = te.y+dis[i][];
if(judge(tx,ty))
{
if(!vis[tx][ty])
{
o[g]++;
s[g][o[g]] = i;
vis[tx][ty] = ;
tt.x = tx;
tt.y = ty;
q.push(tt);
}
}
}
}
printf("%d %d\n",p[].x,p[].y);
for(i =; i <= n ; i++)
{
for(j = ; j <= o[i] ; j++)
{
printf("%c",swit(s[i][j]));
}
if(i!=n)
printf(",\n");
else
printf(".\n");
}
}
bool cmp(node a,node b)
{
if(a.x==b.x)
return a.y<b.y;
return a.x<b.x;
}
void bfs1(int x,int y)
{
int i,g=;
queue<st>q;
st te,tt;
te.x = x;
te.y = y;
q.push(te);
vis[x][y] = ;
while(!q.empty())
{
te = q.front();
q.pop();
g++;
p[g].x = te.x;
p[g].y = te.y;
int k = strlen(ss[g]);
for(i = ; i < k ; i++)
{
if(ss[g][i]=='.'||ss[g][i]==',')
break;
int tx,ty;
if(ss[g][i]=='R')
{
tx = te.x+;
ty = te.y;
}
else if(ss[g][i]=='T')
{
tx = te.x;
ty = te.y+;
}
else if(ss[g][i]=='L')
{
tx = te.x-;
ty = te.y;
}
else
{
tx = te.x;
ty = te.y-;
}
if(!vis[tx][ty])
{
vis[tx][ty] = ;
tt.x = tx;
tt.y = ty;
q.push(tt);
}
}
}
sort(p+,p+g+,cmp);
printf("%d\n",g);
for(i = ; i <= g ; i++)
printf("%d %d\n",p[i].x,p[i].y);
}
int main()
{
int i,j;
char c[],c1[],c2[];
gets(c);
int k = strlen(c);
for(i = ; i < k ; i++)
{
c1[i] = c[i];
if(c[i]==' ')
break;
}
c1[i] = '\0';
if(i==k)
{
n = atoi(c);
for(i =; i <= n ;i++)
{
scanf("%d%d",&p[i].x,&p[i].y);
w[p[i].x][p[i].y] = ;
}
sort(p+,p+n+,cmp);
bfs();
}
else
{
int oo=;
for(j = i+ ; j < k ; j++)
c2[oo++] = c[j];
c2[oo] ='\0';
int x = atoi(c1);
int y = atoi(c2);
gg=;
while(cin>>ss[gg])
{
if(ss[gg][]=='.')
break;
gg++;
}
bfs1(x,y);
}
return ;
}