2016-09-27 2 views
1

Ich versuche, das folgende Problem zu lösen: http://www.spoj.com/problems/BITMAP/ Ich habe eine Multisource-BFS-Ansatz verwendet, um das Problem zu lösen. Aber ich bekomme immer noch WA. Ich habe es wieder von Grund auf neu programmiert, aber das Problem nicht gefunden. Hier ist mein Code `Getting WA für Spoj BITMAP

#include <iostream> 
#include<queue> 
using namespace std; 
struct node{ 
    int i,j; 
}; 
int main() { 
    int t; 
    cin>>t; 
    while(t--){ 
     queue<node> q; 
     int m,n,i,j; 
     cin>>m>>n; 
     string a[m+1];int b[m+1][n+1]; 
     for(i=0;i<m;i++) 
     cin>>a[i]; 
     for(i=0;i<m;i++) 
     for(j=0;j<n;j++) 
     if(a[i][j]=='1') 
     { 
      node temp; 
      temp.i=i; 
      temp.j=j; 
      q.push(temp); 
      b[i][j]=0; 
     } 
     while(!q.empty()){ 
      node temp=q.front(); 
      i=temp.i;j=temp.j; 
      q.pop(); 
      if(i<m-1&&a[i+1][j]=='0'){ 
       node a1; 
      a[i+1][j]=(b[i][j]+1)+48; 
      b[i+1][j]=b[i][j]+1; 
       a1.i=i+1;a1.j=j; 
       q.push(a1); 
      } 
      if(i>0&&a[i-1][j]=='0'){ 
       node b1; 
       a[i-1][j]=(b[i][j]+1)+48; 
      b[i-1][j]=b[i][j]+1; 
       b1.i=i-1;b1.j=j; 
       q.push(b1); 
      } 
      if(j>0&&a[i][j-1]=='0'){ 
       node c; 
       a[i][j-1]=(b[i][j]+1)+48; 
      b[i][j-1]=b[i][j]+1; 
       c.i=i;c.j=j-1; 
       q.push(c); 
      } 
      if(j<n-1&&a[i][j+1]=='0'){ 
       node d; 
       a[i][j+1]=(b[i][j]+1)+48; 
      b[i][j+1]=b[i][j]+1; 
       d.i=i;d.j=j+1; 
       q.push(d); 
      } 
     } 
     for(i=0;i<m;i++){ 
     for(j=0;j<n;j++){ 
     cout<<b[i][j]; 
      if(j!=n-1) 
      cout<<" "; 
     } 
      cout<<endl; 
     } 



    } 
return 0; 
} 

Antwort

0

Sie WA für a[i+1][j]=(b[i][j]+1)+48;

es möglich (b[i][j]+1)+48=304, da Zeichenbegrenzung immer (0 bis 255) so 304 ähnlich wie 48 ('0') für Typ char Variable. Somit kann b[i][j] später in bfs aktualisiert werden.