骚李


私信TA

用户名:zhouli

访问量:2491

签 名:

等  级
排  名 35214
经  验 393
参赛次数 0
文章发表 3
年  龄 0
在职情况 学生
学  校 宿州学院
专  业

  自我简介:

TA的其他文章

解题思路:
采用了广度优先搜索




注意事项:





参考代码:

#include<stdio.h>

struct note {

int x;

int y;

int step;

};

int main() {

// freopen("C:\\cs.txt","r",stdin);

struct note que[100001];

int t;

scanf("%d",&t);

while(t--) {

int a[110][110] = {0},book[110][110] = {0};

int next[4][2] = {{0,1},

{1,0},

{0,-1},

{-1,0} };

int n,m,startx,starty,endx,endy,tx,ty;


scanf("%d%d",&n,&m);

getchar();

for(int i = 1; i <= n; i++) {

for(int j = 1; j <= m; j++) {

scanf("%c",&a[i][j]);

if(a[i][j] == 'S') {

startx = i;

starty = j;

}

if(a[i][j] == 'E') {

endx = i;

endy = j;

}

}

getchar();

}

int head,tail;

head = tail = 1;

que[tail].x = startx;

que[tail].y = starty;

que[tail].step = 0;

tail++;

book[startx][starty] = 1;

int flag =0;

while(head < tail) {

for(int k = 0; k < 4; k++) {                          //枚举四个方向

tx = que[head].x + next[k][0];

ty = que[head].y + next[k][1];

if(tx < 1 || tx > n ||                          //判断是否越界

ty < 1 || ty > m)

continue;

if(a[tx][ty] != '#' && book[tx][ty] == 0) {

book[tx][ty] = 1;

que[tail].x = tx;

que[tail].y = ty;

que[tail].step = que[head].step + 1;

tail++;

}

if(tx == endx && ty == endy) {

flag = 1;

break;

}

}

if(flag == 1)

break;

head++;                 //这个很重要哦,

}

if(head < tail)

printf("%d\n",que[tail-1].step);

else

printf("%d\n",-1);

}

return 0;

}


 

0.0分

0 人评分

看不懂代码?想转换其他语言的代码? 或者想问其他问题? 试试问问AI编程助手,随时响应你的问题:

编程语言转换

万能编程问答  

代码解释器

代码纠错

SQL生成与解释

  评论区

为啥运行没有结果
2018-06-12 15:18:05
  • «
  • 1
  • »