I'm getting WA for this problem,can anyone help?I think here state is 3D,x,y component of grid,and the directions to which it is possible to go from a greed..so I used a 3D bool array of this dimension [505][505][18] to keep record of visited nodes..I though,there can b 16 combinations of directions as each direction it is either possible,or impossible to go..so after getting the combination,I considered it as a binary number,and converted to its decimal equivalent,and made it visited,like if x and y components are 2,3,and directions are 0 0 1 1(north east south west,in this order,this combination means north and east side is blocked,and other sides open),its decimal 3,so made check[2][3][3] visited.Is there any problem in my understanding?Can anyone give any testcase that my code fails?Someone pleas,help

Here's my code
Code: Select all
#include<cstdio>
#include<queue>
#include<cstring>
/*0 for north,1 for east,2 for soth,3 for west*/
using namespace std;
int D[4][2]={0,-1,1,0,0,1,-1,0};
bool check[505][505][18];
int comb(bool sign[4])//returns of decimal equivalent dir array of each node,i.e if it's input
{ //NES,then direction combination is 1110,3 directions open,one close
int i,sum=0,pw=1; //this function returns its decimal equivalent
for(i=3; i>=0; i--)
{
if(sign[i]==true)
sum+=pw;
pw*=2;
}
return sum;
}
typedef struct{
bool dir[4];
}E;
typedef struct{
bool dir[4];//stores directions
int x,y,gen;
}P;
E inp[505][505];
int row,col;
queue<P>q;
E change(E so,int deg)//for every degree,rotates the direction 90 degree clockwise
{
int i;
E r;
if(deg==0) return so;//no rotation needed
for(i=1; i<=deg; i++)
{
r.dir[1]=so.dir[0];
r.dir[2]=so.dir[1];
r.dir[3]=so.dir[2];
r.dir[0]=so.dir[3];
so=r;
}
return r;
}
int BFS(void)
{
P source,ss,pp;
E c;
int g,xx,yy,rotate,i,j;
source.x=source.y=source.gen=0;
for(i=0; i<4; i++) source.dir[i]=inp[0][0].dir[i];
q.push(source);
int k=comb(source.dir);
check[0][0][k]=true;
while(!q.empty())
{
ss=q.front();
q.pop();
for(i=0; i<4; i++)
{
if(ss.dir[i])
{
xx=ss.x+D[i][0];yy=ss.y+D[i][1];
if(xx>=0 && yy>=0 && xx<col && yy<row)
{
pp.x=xx;pp.y=yy;pp.gen=ss.gen+1;
if(pp.x==col-1 && pp.y==row-1) return pp.gen;
c=change(inp[yy][xx],(ss.gen+1)%4);
int k1=comb(c.dir);
if(!check[yy][xx][k1])
{
for(j=0; j<4; j++) pp.dir[j]=c.dir[j];
q.push(pp);
check[yy][xx][k1]=true;
}
}
}
}
}
return -1;
}
int main()
{
int i,j,k,move;
char s[6];
while(scanf("%d%d",&row,&col)!=EOF)
{
for(i=0; i<row; i++)
{
for(j=0; j<col; j++)
{
for(k=0; k<4; k++) inp[i][j].dir[k]=false;//initialisation
scanf("%s",s);
for(k=0; s[k]; k++)
{
if(s[k]=='N') inp[i][j].dir[0]=true;
else if(s[k]=='E') inp[i][j].dir[1]=true;
else if(s[k]=='S') inp[i][j].dir[2]=true;
else if(s[k]=='W') inp[i][j].dir[3]=true;
}
if(i==row-1 && j==col-2) j++;
}
}
while(!q.empty()) q.pop();
memset(check,0,sizeof(check));
move=BFS();
if(move==-1) printf("no path to exit\n");
else printf("%d\n",move);
}
return 0;
}