NYOJ-58 最小步数 AC 分类: NYOJ 2014-01-22 22:01 217人阅读 评论(0) 收藏

NYOJ-58 最小步数  AC                                                    分类:            NYOJ             2014-01-22 22:01    217人阅读    评论(0)    收藏

#include<stdio.h>

void dfs(int step,int x,int y);

int d[4][2] = {{1,0},{-1,0},{0,1},{0,-1}};

int mg[9][9]={1,1,1,1,1,1,1,1,1,
1,0,0,1,0,0,1,0,1,
1,0,0,1,1,0,0,0,1,
1,0,1,0,1,1,0,1,1,
1,0,0,0,0,1,0,0,1,
1,1,0,1,0,1,0,0,1,
1,1,0,1,0,1,0,0,1,
1,1,0,1,0,0,0,0,1,
1,1,1,1,1,1,1,1,1,}; int sx,sy,ex,ey,min=100000; int main(){ int n; scanf("%d",&n); while(n--){ scanf("%d %d %d %d",&sx,&sy,&ex,&ey); if((sx == ex)&&(sy == ey)){
printf("0\n");
continue;
} mg[sx][sy]=1;
mg[ex][ey]=3; dfs(0,sx,sy); printf("%d\n",min); min=100000;
mg[ex][ey]=0;
mg[sx][sy]=0; }
return 0;
} void dfs(int step,int x,int y){
int i; for(i = 0 ; i < 4; i++)
{
int k =x+d[i][0] , v = y+d[i][1] ; if(!mg[k][v]){ mg[k-d[i][0]][v-d[i][1]] = 1; dfs(step+1 , k , v) ; mg[k-d[i][0]][v-d[i][1]] = 0;
} if(mg[k][v] == 3)
{
if(step+1 < min)
min = step + 1;
} }
}

终于,终于,终于,把这题目搞定了,居然只是0步的情况没有考虑到,哎

版权声明:本文为博主原创文章,未经博主允许不得转载。

上一篇:面试总结——Java篇


下一篇:Jackson ObjectMapper类