标签:des style http os io for ar cti
4 6 3 *** *B* *B* *H* *H* *** 4 4 **** *BB* *HH* **** 4 4 **** *BH* *HB* **** 5 6 ****** *.BB** *.H*H* *..*.* ******
3 1 2 Sorry , sir , my poor program fails to get an answer.
思路:普通的BFS,就是球移动的时候处理有点麻烦,要分还剩一个球和还剩两个球的情况考虑。
#include <stdio.h>
#include <string.h>
struct S{
int x[2],y[2],hx[2],hy[2],step,num;
}que[1000000],t;
int n,m;
char mp[25][25];
bool vis[25][25][25][25];
int main()
{
int T,i,j,bf,hf;
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&n,&m);
for(i=1;i<=n;i++) scanf("%s",mp[i]+1);
bf=hf=0;
for(i=1;i<=n;i++)
{
for(j=1;j<=m;j++)
{
if(mp[i][j]=='B')
{
que[0].x[bf]=i;
que[0].y[bf]=j;
bf++;
mp[i][j]='.';
}
if(mp[i][j]=='H')
{
que[0].hx[hf]=i;
que[0].hy[hf]=j;
hf++;
mp[i][j]='.';
}
}
}
int top=0;
int bottom=1;
que[0].step=0;
que[0].num=2;
memset(vis,0,sizeof vis);
while(top<bottom)
{
t=que[top];
if(!t.num)
{
printf("%d\n",t.step);
break;
}
if(t.num==2)
{
if(t.x[0]<t.x[1])//x-1
{
if(t.x[0]-1>=1 && mp[t.x[0]-1][t.y[0]]!='*') t.x[0]--;
for(i=0;i<2;i++) if(t.hx[i]==t.x[0] && t.hy[i]==t.y[0])
{
t.hx[i]=t.x[0]=0;
t.num--;
}
if(t.x[1]-1>=1 &&mp[t.x[1]-1][t.y[1]]!='*' && !(t.x[1]-1==t.x[0] && t.y[1]==t.y[0])) t.x[1]--;
for(i=0;i<2;i++) if(t.hx[i]==t.x[1] && t.hy[i]==t.y[1])
{
t.hx[i]=t.x[1]=0;
t.num--;
}
if(!vis[t.x[0]][t.y[0]][t.x[1]][t.y[1]])
{
vis[t.x[0]][t.y[0]][t.x[1]][t.y[1]]=1;
if(t.num==1 && !t.x[0])
{
t.x[0]=t.x[1];
t.y[0]=t.y[1];
t.x[1]=0;
}
t.step++;
que[bottom++]=t;
}
}
else
{
if(t.x[1]-1>=1 && mp[t.x[1]-1][t.y[1]]!='*') t.x[1]--;
for(i=0;i<2;i++) if(t.hx[i]==t.x[1] && t.hy[i]==t.y[1])
{
t.hx[i]=t.x[1]=0;
t.num--;
}
if(t.x[0]-1>=1 &&mp[t.x[0]-1][t.y[0]]!='*' && !(t.x[1]==t.x[0]-1 && t.y[1]==t.y[0])) t.x[0]--;
for(i=0;i<2;i++) if(t.hx[i]==t.x[0] && t.hy[i]==t.y[0])
{
t.hx[i]=t.x[0]=0;
t.num--;
}
if(!vis[t.x[0]][t.y[0]][t.x[1]][t.y[1]])
{
vis[t.x[0]][t.y[0]][t.x[1]][t.y[1]]=1;
if(t.num==1 && !t.x[0])
{
t.x[0]=t.x[1];
t.y[0]=t.y[1];
t.x[1]=0;
}
t.step++;
que[bottom++]=t;
}
}//x-1
t=que[top];
if(t.x[0]>t.x[1])//x+1
{
if(t.x[0]+1<=n && mp[t.x[0]+1][t.y[0]]!='*') t.x[0]++;
for(i=0;i<2;i++) if(t.hx[i]==t.x[0] && t.hy[i]==t.y[0])
{
t.hx[i]=t.x[0]=0;
t.num--;
}
if(t.x[1]+1<=n && mp[t.x[1]+1][t.y[1]]!='*' && !(t.x[1]+1==t.x[0] && t.y[1]==t.y[0])) t.x[1]++;
for(i=0;i<2;i++) if(t.hx[i]==t.x[1] && t.hy[i]==t.y[1])
{
t.hx[i]=t.x[1]=0;
t.num--;
}
if(!vis[t.x[0]][t.y[0]][t.x[1]][t.y[1]])
{
vis[t.x[0]][t.y[0]][t.x[1]][t.y[1]]=1;
if(t.num==1 && !t.x[0])
{
t.x[0]=t.x[1];
t.y[0]=t.y[1];
t.x[1]=0;
}
t.step++;
que[bottom++]=t;
}
}
else
{
if(t.x[1]+1<=n && mp[t.x[1]+1][t.y[1]]!='*') t.x[1]++;
for(i=0;i<2;i++) if(t.hx[i]==t.x[1] && t.hy[i]==t.y[1])
{
t.hx[i]=t.x[1]=0;
t.num--;
}
if(t.x[0]+1<=n &&mp[t.x[0]+1][t.y[0]]!='*' && !(t.x[1]==t.x[0]+1 && t.y[1]==t.y[0])) t.x[0]++;
for(i=0;i<2;i++) if(t.hx[i]==t.x[0] && t.hy[i]==t.y[0])
{
t.hx[i]=t.x[0]=0;
t.num--;
}
if(!vis[t.x[0]][t.y[0]][t.x[1]][t.y[1]])
{
vis[t.x[0]][t.y[0]][t.x[1]][t.y[1]]=1;
if(t.num==1 && !t.x[0])
{
t.x[0]=t.x[1];
t.y[0]=t.y[1];
t.x[1]=0;
}
t.step++;
que[bottom++]=t;
}
}//x+1
t=que[top];
if(t.y[0]>t.y[1])//y+1
{
if(t.y[0]+1<=m && mp[t.x[0]][t.y[0]+1]!='*') t.y[0]++;
for(i=0;i<2;i++) if(t.hx[i]==t.x[0] && t.hy[i]==t.y[0])
{
t.hx[i]=t.x[0]=0;
t.num--;
}
if(t.y[1]+1<=m && mp[t.x[1]][t.y[1]+1]!='*' && !(t.x[1]==t.x[0] && t.y[1]+1==t.y[0])) t.y[1]++;
for(i=0;i<2;i++) if(t.hx[i]==t.x[1] && t.hy[i]==t.y[1])
{
t.hx[i]=t.x[1]=0;
t.num--;
}
if(!vis[t.x[0]][t.y[0]][t.x[1]][t.y[1]])
{
vis[t.x[0]][t.y[0]][t.x[1]][t.y[1]]=1;
if(t.num==1 && !t.x[0])
{
t.x[0]=t.x[1];
t.y[0]=t.y[1];
t.x[1]=0;
}
t.step++;
que[bottom++]=t;
}
}
else
{
if(t.y[1]+1<=m && mp[t.x[1]][t.y[1]+1]!='*') t.y[1]++;
for(i=0;i<2;i++) if(t.hx[i]==t.x[1] && t.hy[i]==t.y[1])
{
t.hx[i]=t.x[1]=0;
t.num--;
}
if(t.y[0]+1<=m &&mp[t.x[0]][t.y[0]+1]!='*' && !(t.x[1]==t.x[0] && t.y[1]==t.y[0]+1)) t.y[0]++;
for(i=0;i<2;i++) if(t.hx[i]==t.x[0] && t.hy[i]==t.y[0])
{
t.hx[i]=t.x[0]=0;
t.num--;
}
if(!vis[t.x[0]][t.y[0]][t.x[1]][t.y[1]])
{
vis[t.x[0]][t.y[0]][t.x[1]][t.y[1]]=1;
if(t.num==1 && !t.x[0])
{
t.x[0]=t.x[1];
t.y[0]=t.y[1];
t.x[1]=0;
}
t.step++;
que[bottom++]=t;
}
}//y+1
t=que[top];
if(t.y[0]<t.y[1])//y-1
{
if(t.y[0]-1>=1 && mp[t.x[0]][t.y[0]-1]!='*') t.y[0]--;
for(i=0;i<2;i++) if(t.hx[i]==t.x[0] && t.hy[i]==t.y[0])
{
t.hx[i]=t.x[0]=0;
t.num--;
}
if(t.y[1]-1>=1 && mp[t.x[1]][t.y[1]-1]!='*' && !(t.x[1]==t.x[0] && t.y[1]-1==t.y[0])) t.y[1]--;
for(i=0;i<2;i++) if(t.hx[i]==t.x[1] && t.hy[i]==t.y[1])
{
t.hx[i]=t.x[1]=0;
t.num--;
}
if(!vis[t.x[0]][t.y[0]][t.x[1]][t.y[1]])
{
vis[t.x[0]][t.y[0]][t.x[1]][t.y[1]]=1;
if(t.num==1 && !t.x[0])
{
t.x[0]=t.x[1];
t.y[0]=t.y[1];
t.x[1]=0;
}
t.step++;
que[bottom++]=t;
}
}
else
{
if(t.y[1]-1>=1 && mp[t.x[1]][t.y[1]-1]!='*') t.y[1]--;
for(i=0;i<2;i++) if(t.hx[i]==t.x[1] && t.hy[i]==t.y[1])
{
t.hx[i]=t.x[1]=0;
t.num--;
}
if(t.y[0]-1>=1 &&mp[t.x[0]][t.y[0]-1]!='*' && !(t.x[1]==t.x[0] && t.y[1]==t.y[0]-1)) t.y[0]--;
for(i=0;i<2;i++) if(t.hx[i]==t.x[0] && t.hy[i]==t.y[0])
{
t.hx[i]=t.x[0]=0;
t.num--;
}
if(!vis[t.x[0]][t.y[0]][t.x[1]][t.y[1]])
{
vis[t.x[0]][t.y[0]][t.x[1]][t.y[1]]=1;
if(t.num==1 && !t.x[0])
{
t.x[0]=t.x[1];
t.y[0]=t.y[1];
t.x[1]=0;
}
t.step++;
que[bottom++]=t;
}
}//y-1
t=que[top];
}
else//num==1
{
if(t.x[0]+1<=n && mp[t.x[0]+1][t.y[0]]!='*') t.x[0]++;
for(i=0;i<2;i++) if(t.hx[i]==t.x[0] && t.hy[i]==t.y[0])
{
t.hx[i]=t.x[0]=0;
t.num--;
}
if(!vis[t.x[0]][t.y[0]][t.x[1]][t.y[1]])
{
vis[t.x[0]][t.y[0]][t.x[1]][t.y[1]]=1;
t.step++;
que[bottom++]=t;
}
t=que[top];
if(t.x[0]-1>=1 && mp[t.x[0]-1][t.y[0]]!='*') t.x[0]--;
for(i=0;i<2;i++) if(t.hx[i]==t.x[0] && t.hy[i]==t.y[0])
{
t.hx[i]=t.x[0]=0;
t.num--;
}
if(!vis[t.x[0]][t.y[0]][t.x[1]][t.y[1]])
{
vis[t.x[0]][t.y[0]][t.x[1]][t.y[1]]=1;
t.step++;
que[bottom++]=t;
}
t=que[top];
if(t.y[0]+1<=m && mp[t.x[0]][t.y[0]+1]!='*') t.y[0]++;
for(i=0;i<2;i++) if(t.hx[i]==t.x[0] && t.hy[i]==t.y[0])
{
t.hx[i]=t.x[0]=0;
t.num--;
}
if(!vis[t.x[0]][t.y[0]][t.x[1]][t.y[1]])
{
vis[t.x[0]][t.y[0]][t.x[1]][t.y[1]]=1;
t.step++;
que[bottom++]=t;
}
t=que[top];
if(t.y[0]-1>=1 && mp[t.x[0]][t.y[0]-1]!='*') t.y[0]--;
for(i=0;i<2;i++) if(t.hx[i]==t.x[0] && t.hy[i]==t.y[0])
{
t.hx[i]=t.x[0]=0;
t.num--;
}
if(!vis[t.x[0]][t.y[0]][t.x[1]][t.y[1]])
{
vis[t.x[0]][t.y[0]][t.x[1]][t.y[1]]=1;
t.step++;
que[bottom++]=t;
}
}
top++;
}
if(top==bottom) printf("Sorry , sir , my poor program fails to get an answer.\n");
}
}
HDU-3309-Roll The Cube(BFS),布布扣,bubuko.com
标签:des style http os io for ar cti
原文地址:http://blog.csdn.net/faithdmc/article/details/38498443