2017-01-18 5 views
0

SPOJのビットマップ(http://www.spoj.com/problems/BITMAP/)に私の解決策が間違っています。SPOJでビットマップの投稿に間違った回答を取得しました

問題の説明:ONEとZEROで構成された行列。各ゼロに対して、行列内で最も近いONEまでの距離を求める必要があります。マトリックスの2つのポイント間の距離を測定する際に、1つずつUP、DOWN、LEFT、またはRIGHTを行えます。

以下の解決策は私のテストケースに合格します。

#include <iostream> 
#include <cstring> 
#include <queue> 
using namespace std; 

#define INF 9999999999 

int main() { 
    int T, M, N; 
    cin >> T; 

    vector<pair<int, int>> shift; 
    shift.push_back(make_pair(0, -1)); // Left 
    shift.push_back(make_pair(0, 1)); // Right 
    shift.push_back(make_pair(1, 0)); // Down 
    shift.push_back(make_pair(-1, 0)); // Up 

    while (T--) { 
     cin >> M >> N; 
     int arr[M][N]; 
     int result[M][N]; 
     memset(arr, 0, sizeof(arr)); 

     queue<pair<int, int>> q; 
     for(int i=0; i<M; i++) { 
      string input; 
      cin >> input; 

      for(int j=0; j<N; j++) { 
       result[i][j] = INF; 
       arr[i][j] = input[j] - '0'; 
       if (arr[i][j]) { 
        q.push(make_pair(i, j)); 
        result[i][j] = 0; 
       } 
      } 
     } 

     q.push(make_pair(-1, -1)); 
     while (!q.empty()) { 
      pair<int, int> p = q.front(); 
      q.pop(); 
      if (p.first == -1 && !q.empty()) { 
       q.push(make_pair(-1, -1)); 
      } else { 
       for(int i=0; i<shift.size(); i++) { 
        if (p.first + shift[i].first >= 0 && p.first + shift[i].first < N && 
         p.second + shift[i].second >= 0 && p.second + shift[i].second < N) { 
         if (!arr[p.first + shift[i].first][p.second + shift[i].second]) { 
          if (result[p.first + shift[i].first][p.second + shift[i].second] > 1 + result[p.first][p.second]) { 
           result[p.first + shift[i].first][p.second + shift[i].second] = 1 + result[p.first][p.second]; 
           q.push(make_pair(p.first + shift[i].first, p.second + shift[i].second)); 
          } 
         } 
        } 
       } 
      } 
     } 

     for(int i=0; i<M; i++) { 
      for(int j=0; j<N; j++) { 
       cout << result[i][j]; 
       if (j<N-1) cout << " "; 
      } 
      cout << endl; 
     } 
    } 

    return 0; 
} 
+0

解決 - Nの代わりにMの入力ミス。 –

答えて

0

「M」が間違って「N」と入力した間違いがありました:私は裁判官に提出していたときに、それが失敗した理由を、ここにコードがあるか分かりません。問題を解決する。

関連する問題