hdu1728 逃离迷宫(bfs)

这是本人写过的最纠结的一次bfs,一看到题目就觉得比较简单,是非常基础的广搜,但是用stl写出来交上去居然WA,修改细节后一直wa,T_T,写代码能力亟须加强,有算法无代码实在是太悲剧了。。。。而且有很多细节需要注意,参考网上程序得出AC代码如下:

#include<iostream>

#include<queue>

using namespace std;

struct Node

{

       int x, y;  

       int turn; 

       int dir;    

};

char map[110][110];

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

int n, m;

Node start, end;

int visited[110][110];

int check(int a,int b)    

{    

    if(a>=1&&a<=m&&b>=1&&b<=n)    

        return 1;    

    return 0;    

int check1, limit; 

queue<Node> Que;  

void Bfs()

{

    Node pre, next;

    visited[start.x][start.y]=0;

    Que.push(start);

    while (!Que.empty()){

          pre = Que.front();

          if (pre.x == end.x && pre.y == end.y){

             if (pre.turn <= limit){

                check1 = 1;return ;          

             }          

 

          }   

          Que.pop();  

          for (int i = 0; i < 4; i ++){ 

              next.x = pre.x + Go[i][0];

              next.y = pre.y + Go[i][1];

 

              if (check(next.x, next.y) && map[next.x][next.y] != '*'){ 

                 next.turn = pre.turn;

                 if (pre.dir != i){

                    next.turn ++;

                    if (next.turn > limit){

                       goto l;//不能用continue,暂时我还没想通

                    }

                 }

                    next.dir = i;

                 if (next.turn <= visited[next.x][next.y]){

                    visited[next.x][next.y] = next.turn;

                    Que.push(next);

                 }

              }  l:;  

          }       

    }

    return ;

}

int main()

{

    int t;

    scanf("%d", &t);

    //cin >> t;

    while (t--){

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

          //cin >> m >>n;

          while (!Que.empty()){Que.pop();}

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

              getchar(); 

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

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

                  visited[i][j]=11;   

                  //cin >> map[i][j];

              }

 

          } 

          scanf("%d %d %d %d %d", &limit, &start.y, &start.x, &end.y, &end.x);

          //cin >> limit >> start.y >> start.x >> end.y >> end.x ;

          check1 = 0;

          start.turn = start.dir = -1;

          Bfs();

          if (check1)printf("yes/n");

          else printf("no/n");     

    }

    return 0;

}

另外附上本人脑残的WA代码:

#include<iostream>

#include<queue>

using namespace std;

struct Node

{

       int x, y;  

       int turn; 

       int dir;    

};

char map[110][110];

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

int n, m;

int sx, sy, ex, ey;

int visited[110][110];

int check(int a,int b)    

{    

    if(a>=1&&a<=m&&b>=1&&b<=n&&!visited[a][b])    

        return 1;    

    return 0;    

int check1, limit;   

void Bfs()

{

    queue<Node> Que;

    memset(visited, 0, sizeof(visited));

    Node temp, pre, next;

    temp.x = sx;

    temp.y = sy;

    temp.turn = -1;

    temp.dir = -1;

    Que.push(temp);

    while (!Que.empty()){

          pre = Que.front();

          for (int i = 0; i < 4; i ++){ 

              //pre.dir = i;

              next.x = pre.x + Go[i][0];

              next.y = pre.y + Go[i][1];

              next.turn = pre.turn;

              if (check(next.x, next.y) && map[next.x][next.y] != '*'){ 

                 if (next.dir != i){next.turn ++;}

                 if (next.turn > limit)goto l;

                 if (next.x == ex && next.y == ey){

                    if (next.turn <= limit){

                       //cout << next.turn<<endl;

                       check1 = 1;          

                    }          

                    return ;            

                 }   

                 next.dir=i;

                 visited[next.x][next.y] = 1;

                 Que.push(next);

              }

              l:;  

          }

          Que.pop();           

    }

}

int main()

{

    int t;

    //scanf("%d", &t);

    cin >> t;

    while (t--){

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

          cin >> m >>n;

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

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

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

                  cin >> map[i][j];

              }

              //getchar();    

          } 

          //scanf("%d %d %d %d %d", &limit, &sy, &sx, &ey, &ex);

          cin >> limit >> sy >> sx >> ey >> ex ;

          check1 = 0;

          Bfs();

          if (check1)printf("yes/n");

          else printf("no/n");     

    }

    return 0;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值