hdu 1175 BFS

这题忘记每次侧的时候初始化mark  WA了无数次。。。。说多了都是泪

#include <iostream>
#include <queue>
#include <cstdio>
#include <cstdlib>
#include <cstring>
using namespace std;

typedef struct{
    int x, y;
    int derect;
    int num;
}Point;

int moves[][2] = { -1, 0, 0, 1, 1, 0, 0, -1 };
int mark[1100][1100];
int map[1100][1100];
int beginx, beginy, endx, endy, MAX;
int N, M;

void BFS(){
    if( map[beginx][beginy] == 0 || map[endx][endy] == 0 || map[beginx][beginy] != map[endx][endy]  ){
        cout << "NO" << endl;
        return;
    }

    Point begin;
    begin.x = beginx;
    begin.y = beginy;
    begin.derect = -1;
    begin.num = -1;
    queue<Point> q;
    q.push( begin );

    while( !q.empty() ){
        Point p;
        p = q.front();
        q.pop();
        for( int i = 0; i < 4; i++ ){
            Point temp;
            temp.x = p.x + moves[i][0];
            temp.y = p.y + moves[i][1];
            temp.derect = i;
            if( temp.derect != p.derect ){
                temp.num = p.num + 1;
            }else{
                temp.num = p.num;
            }
            if( temp.x == endx && temp.y == endy && temp.num <= MAX ){
                cout << "YES" << endl;
                return;
            }
            if( temp.x < 1 || temp.x > M || temp.y < 1 || temp.y > N || temp.num > MAX || map[temp.x][temp.y] != 0 ){
                continue;
            }

            if( temp.num < mark[temp.x][temp.y] ){//注意这里有个等于号,因为虽然拐弯数相同,但方向可能不同!!!!
                mark[temp.x][temp.y] = temp.num;
                q.push( temp );
            }
        }
    }
    cout << "NO" << endl;
}

int main(){
    int t;
    while( scanf( "%d%d", &M, &N ) && M && N ){

        memset( map, 0, sizeof( map ) );
        for( int i = 1; i <= M; i++ ){
            for( int j = 1; j <= N; j++ ){
                cin >> map[i][j];
                mark[i][j] = 3;
            }
        }
        cin >> t;
        while( t-- ){
            memset( mark, 127, sizeof( mark ) );
            cin >> beginx >> beginy >> endx >> endy;
            MAX = 2;
            BFS();
        }
    }
    return 0;
}


 

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值