简单搜索,莫名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.
....
....
*/