简单的BFS 注意搜索的顺序 两种情况分别处理
#include<stdio.h>
#include<string.h>
long map[12][12];
long dui[1000][3];
long x,y,head,last,i,j,a,b,n,l,p;
char c;
char s[100];
void ind(long x,long y,char c)
{
if (x==0 || y==0 || x>10 || y>10 || map[x][y]==0)
return;
last++;
dui[last][1]=x;dui[last][2]=y;
map[x][y]=0;
printf("%c",c);
}
void indd(long x,long y)
{
map[x][y]=1;
last++;
dui[last][1]=x;
dui[last][2]=y;
}
int main()
{
scanf("%d",&x);
scanf("%c",&c);
if (c==' ')
{
scanf("%d",&b);a=x;
head=0;last=1;dui[1][1]=a;dui[1][2]=b;map[a][b]=1;
while (head<last)
{
head++;
x=dui[head][1];
y=dui[head][2];
scanf("%s",s);
l=strlen(s)-2;
for (j=0;j<=l;j++)
{
if (s[j]=='B')
indd(x,y-1);
else if (s[j]=='L')
indd(x-1,y);
else if (s[j]=='T')
indd(x,y+1);
else if (s[j]=='R')
indd(x+1,y);
}
}
printf("%d\n",last);
p=0;
for (i=10;i>=1;i--)
{
for (j=10;j>=1;j--)
if (map[i][j]==1)
{
a=i;b=j;p=1;break;
}
if(p==1)
break;
}
for (i=1;i<=a;i++)
for (j=1;j<=10;j++)
if (map[i][j]==1)
{
printf("%d %d",i,j);
if (!(i==a &&j==b))
printf("\n");
}
}
else
{
n=x;
for (i=1;i<=n;i++)
{
scanf("%d %d",&a,&b);
map[a][b]=1;
if (i==1)
{
printf("%d %d\n",a,b);
dui[1][1]=a;dui[1][2]=b;
map[a][b]=0;
}
}
head=0;last=1;
while (head<last)
{
head++;
x=dui[head][1];
y=dui[head][2];
ind(x+1,y,'R');
ind(x,y+1,'T');
ind(x-1,y,'L');
ind(x,y-1,'B');
if (head!=last)
printf(",\n");
}
printf(".");
}
}
posted on 2011-06-27 17:23
梦转千寻 阅读(44)
评论(0) 编辑 收藏 引用