hdu 2531 #BFS


简单搜索,莫名wa了一小时,望大佬指出wa点,多谢

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

typedef  long long  ll ;
typedef pair<int , int> p2 ;
int s[150][150] ;
vector<p2> pq ;
int  n , m ;
struct node{ int x ,  y ,  step ;
    node(int xx , int yy , int ss): x (xx) , y (yy) , step (ss) {}
};
p2 jian ( p2 a , p2 b){
    return make_pair(a.first-b.first , a.second - b.second);
}
int x[4] = {0,1,0,-1} ; int y[4] = {1,0,-1,0};
bool vis[150][150] ;
int ans = 99999999 ;
queue<node> work ;
bool check(int a , int b ){
    for( int i = 0 ; i < pq.size() ; i ++  ){
        int kx = a - pq[i].first ,ky = b - pq[i].second ;
        if( kx >= 0 && kx < n && ky >= 0 && ky < m && s[kx][ky] != 2 ){}
        else return false ;
    }
    return true ;
}
void BFS(){
    if( work.empty() ) return ;
    node temp = work.front() ;
    work.pop() ;
    for( int i = 0 ; i < 4 ; i ++ ){
            int a = temp.x + x[i] , b = temp.y + y[i] ;
            bool ffflag = 0 ;
            if( vis[a][b] || a < 0 || a >= n || b < 0 || b >= m ) { continue ;}
            vis[a][b] = true ;
            if( s[a][b] == 1 ) ffflag = 1 ;
            for(int j = 0 ; j < pq.size() && ! ffflag ; j ++ ){
                int aa = a - pq[j].first , bb = b - pq[j].second ;
                if( s[aa][bb] == 1) {  ffflag = 1 ;break ;}
            }
            if( !s[a][b] )
                if( check(a , b) )   work.push(node( a , b , temp.step + 1 )) ;
            if( ffflag )
                if( check(a , b)) {  ans = min( temp.step + 1 , ans );   return ; }
    }
    BFS() ;
}

int main()
{

    while( ~ scanf("%d %d" , &n , &m) )
    {
        if( !n && !m ) break ;
        memset(s , 0 , sizeof( s )) ; pq.clear() ;
        ans = 99999999 ;
        char read[550] ;
        memset(s , 0 , sizeof( s )) ;
        p2 start ; bool flag = 0 ;
        for(int i = 0 ; i < n ; i ++ ){
            scanf("%s" , read) ;
            for(int j = 0 ; j < m ; j ++ ){
                if( read[j] == 'Q') s[i][j] = 1 ;
                else if( read[j] == 'O') s[i][j] = 2 ;
                else if( !flag && read[j] == 'D') {
                    flag = true ;
                    start = make_pair(i , j);
                }else if( read[j] == 'D'){
                    pq.push_back(jian(start , make_pair(i,j)));
                    //cout << pq[pq.size()-1].first << " " << pq[pq.size()-1].second << endl ;
                }
            }
        }
        memset(vis , 0 ,sizeof(vis)) ; vis[start.first][start.second] = 1 ;
        while(!work.empty())    work.pop() ;
        work.push(node(start.first , start.second , 0));
        BFS() ;
        if(ans != 99999999 )
            printf("%d\n" , ans );
        else
            printf("Impossible\n") ;
    }
	return 0;
}
/*
4 4
....
DDQ.
....
....
*/


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值