AOJ - Problem 0223 : Stray Twins
2人が同じ位置に来るのにかかる最小の時間を求める問題です。幅優先探索(BFS)で解くことができます。必要な情報は2人の位置座標と時間です。
気をつけることは
- 2人は逆の動きをする
- 2人の位置座標が同じ場所の探索をしないようにフラグをたてるのを忘れない
- 座標が(1,1)から始まっているので2人の初期位置の座標から1を引いておくのを忘れない
- 2人が出会わないまま時間が100以上になったら探索を打ち切る
こんなところだと思います。
#include <iostream> #include <map> #include <queue> #include <algorithm> using namespace std; typedef pair<int,int> P; int w,h,ans; int a[51][51]; bool f[51][51][51][51]; int dx[4] = {0,0,1,-1}; int dy[4] = {1,-1,0,0}; struct cost{ P tp,kp; int cnt; }; void solve(struct cost c){ queue<struct cost> q; q.push( c ); ans = -1; for(int ty=0 ; ty < h ; ty++ ){ for(int tx=0 ; tx < w ; tx++ ){ for(int ky=0 ; ky < h ; ky++ ){ for(int kx=0 ; kx < w ; kx++ ){ f[ty][tx][ky][kx] = false; } } } } while( !q.empty() ){ struct cost now = q.front(), next; q.pop(); int tx = now.tp.first; int ty = now.tp.second; int kx = now.kp.first; int ky = now.kp.second; next.cnt = now.cnt + 1; if( f[ty][tx][ky][kx] ){ continue; }else{ f[ty][tx][ky][kx] = true; } if( now.cnt == 100 ){ break; } if( tx == kx && ty == ky ){ ans = now.cnt; break; } for(int i=0 ; i < 4 ; i++ ){ int tmx = tx + dx[i]; int tmy = ty + dy[i]; int kmx = kx - dx[i]; int kmy = ky - dy[i]; if( tmx < 0 || tmy < 0 || tmx >= w || tmy >= h || a[tmy][tmx] ){ P tp(tx,ty); next.tp = tp; }else{ P tp(tmx,tmy); next.tp = tp; } if( kmx < 0 || kmy < 0 || kmx >= w || kmy >= h || a[kmy][kmx] ){ P kp(kx,ky); next.kp = kp; }else{ P kp(kmx,kmy); next.kp = kp; } q.push( next ); } } } int main(){ while( cin >> w >> h , w||h ){ int tx, ty, kx, ky; cin >> tx >> ty >> kx >> ky; tx--; ty--; kx--; ky--; for(int y=0 ; y < h ; y++ ){ for(int x=0 ; x < w ; x++ ){ cin >> a[y][x]; } } struct cost c; P tp(tx,ty), kp(kx,ky); c.tp = tp; c.kp = kp; c.cnt = 0; solve( c ); if( ans == -1 ){ cout << "NA" << endl; }else{ cout << ans << endl; } } }