AOJ 0230
問題
http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=0230
解法
幅優先探索(BFS)する
はしごを登ったり、滑ったりする処理をミスってバグりまくった
#include <cstdio> #include <algorithm> #include <queue> using namespace std; typedef pair<int, int> P; typedef pair<P, int> PP; int b[2][100]; bool used[2][100]; int n; int main(){ while(scanf("%d", &n) && n != 0){ for(int i = 0; i < 2; i++){ for(int j = 0; j < n; j++){ scanf("%d", &b[i][j]); used[i][j] = false; } } queue<PP> Q; Q.push(PP(P(0,0), 0)); Q.push(PP(P(1,0), 0)); int ans = -1; while(!Q.empty()){ int s = Q.front().first.first; int t = Q.front().first.second; int j = Q.front().second; Q.pop(); if(b[s][t] == 1){ //はしご for(; t < n && b[s][t] == 1; t++) ; if(!(t == n-1 && b[s][t] == 1)) t--; } if(b[s][t] == 2){ //すべる for(; b[s][t] == 2; t--) ; } if(used[s][t]) continue; used[s][t] = true; if(t == n - 1){ ans = j; break; } if(t + 2 < n) Q.push(PP(P( (s+1)&1, t+2), j + 1)); Q.push(PP(P( (s+1)&1, t+1), j + 1)); } if(ans == -1) puts("NA"); else printf("%d\n", ans); } return 0; }