備忘録

まとめておきたいことのメモ 主にプロコンのこと

AOJ-ICPC 1166

昨日考えてた問題。

Amazing Mazes | Aizu Online Judge

 迷路でゴールまでの手数を考えます。

昨日の時点では幅優先探索だ!と思っていたものの今ふっと深さ優先探索のほうがいいのでは?と思ってしまった…。

と思っていたけど

迷路で眺める探索アルゴリズム | 机上のにゅーろん

これを見た感じ幅優先探索でよさそう。 

昨日書いたプログラムはこんな感じ。

 

#include <cstdio>
#include <iostream>
#include <cmath>
#include <ctype.h>
#include <string>
#include <sstream>
#include <iostream>
#include <algorithm>
#include <cstdlib>
#include <map>
#include <queue>
#include <utility>
#include <vector>
#include <set>
 
using namespace std;
 
// →にx軸、↓にy軸を取る
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
 
int meiro[40][40][40][40];
// meiro[a][b][c][d] ... (a, b)から(c, d)へ移るのに壁があれば1、なければ0
int tesu[40][40];
int issearched[40][40];
int w, h;
 
void tansaku(int a, int b);
 
int main()
{
    while(1){
        cin >> w >> h;
        if(w == 0 && h == 0) break;
        for(int i = 0; i < h; i++){
            for(int j = 0; j < w; j++){
                tesu[i][j] = 0;
                issearched[i][j] = 0;
            }
        }
        for(int i = 0; i < h - 1; i++){
            for(int j = 0; j < w - 1; j++){
                cin >> meiro[j][i][j + 1][i];
            }
            for(int j = 0; j < w; j++){
                cin >> meiro[j][i][j][i + 1];
            }
        }
        for(int i = 0; i < w - 1; i++){
            cin >> meiro[i][h - 1][i + 1][h - 1];
        }
        tansaku(0, 0);
        cout << tesu[w - 1][h - 1] << endl;
    }
    return 0;
}
 
void tansaku(int a, int b)
{
    queue<pair<int, int> > qu;
    qu.push(pair<int, int>(a, b));
    tesu[a][b] = 1;
    issearched[a][b] = 1;
    int x, y, next_x, next_y;
    while(1){
        if(qu.empty()) break;
        x = (qu.front()).first;
        y = (qu.front()).second;
        for(int i = 0; i < 4; i++){
            next_x = x + dx[i];
            next_y = y + dy[i];
            if(next_x < 0 || next_x >= w || next_y < 0 || next_y >= h) continue;
            if(issearched[next_x][next_y] == 0 && meiro[x][y][next_x][next_y] == 0){
                issearched[next_x][next_y] = 1;
                tesu[next_x][next_y] = tesu[x][y] + 1;
                qu.push(pair<int, int>(next_x, next_y));
            }
        }
        qu.pop();
    }
    return;
}

 

(うまくソースコードを貼り付けられなかった 後でなおす)

で、AOJで提出してみたところWAだった。一つ目からまちがっていたみたい。

これからどこがおかしいのか探します。