大概题意就是弹弹球,一旦弹出球,只能在有冰块突起的地方被停住,求球球能不能在10步以下被弹到目的地。
ps日本人就是爱玩儿。。。
第一眼看到“The purpose of the game is to lead the stone from the start to the goal with the minimum number of moves."的时候,惯性思维想到用广搜。之后发现数据规模比较小,可以直接深搜遍历。有空再翻回来写个广搜吧~
DFS代码:
#include<stdio.h>
int map[21][21];
int w,h,min;
int sx,sy,ex,ey;
int dir[][2]={{1,0},{-1,0},{0,1},{0,-1}};
void dfs(int dep,int x,int y)
{
if(dep>=10)
return;
for(int i=0; i<4; i++)
{
int nextx=x+dir[i][0];
int nexty=y+dir[i][1];
if(map[nextx][nexty]==1)
continue;
while(!map[nextx][nexty])
{
nextx+=dir[i][0];
nexty+=dir[i][1];
}
if(nextx>0 && nextx<=h && nexty>0 && nexty<=w)
{
if(map[nextx][nexty]==1)
{
map[nextx][nexty]=0;
dfs(dep+1,nextx-dir[i][0],nexty-dir[i][1]);
map[nextx][nexty]=1;
}
if(map[nextx][nexty]==3)
{
if(dep+1<min)
min=dep+1;
}
}
}
}
int main()
{
int i,j;
while(scanf("%d%d",&w,&h)!=EOF)
{
if(w==0&&h==0)
break;
for(i=1; i<=h; i++)
{
for(j=1; j<=w; j++)
{
scanf("%d",&map[i][j]);
if(map[i][j]==2)
{
map[i][j]=0; //因为样例中有 1 0 2 1 1 3 反弹回来的情况
sx=i;
sy=j;
}
if(map[i][j]==3)
{
ex=i;
ey=j;
}
}
}
min=32767; //每一次赋初值
dfs(0,sx,sy);
if(min<32767)
printf("%d\n",min);
else
printf("-1\n");
}
return 0;
}
蓝桥杯还有十几天。。。我不要当炮灰啊T T内牛满面!!最近身体老是感觉不舒服!好久没去实验室了!会不会被骂!!T T comeon!