JOIN
Get Time
forums  Revision History
Search My Post History  |  My Watches  |  User Settings
Forums Round Tables General Career Discussions Re: Question Revision History (1 edit)
Re: Question (response to post by Minilek)
My guess is they really are looking for something like
for (int i=0; i<3; i++)
  for (int j=0; j<3; j++) {
     int hor = 0;
     int vert = 0;
     int square = 0;
     for (int x=0; x<3; x++)
       for (int y=0; y<3; y++) {
         hor |= 1<< grid[3*i + j][3*x + y];
         vert |= 1<< grid[3*x+y][3*i+j];
         square |= 1<<grid[3*i+x][3*j+y];
       }
     }
     if (hor != 0x3FE || vert != 0x3FE || square != 0x3FE) 
       return false;
  }
}
return true;   


If they really wanted a single unnested loop, it's possible using
int hor = 0;
int vert = 0;
int square = 0;
for (int c = 0; c < 81; c++) {
  int x = c/27;
  int y = (c/9)%3;
  int i = (c/3)%3;
  int j = c%3;
  if (x+y==0) hor = vert = square = 0;
  hor |= 1<< grid[3*i + j][3*x + y];
  vert |= 1<< grid[3*x+y][3*i+j];
  square |= 1<<grid[3*i+x][3*j+y];
  if (x+y==4 && (hor != 0x3FE || vert != 0x3FE || square != 0x3FE)) 
       return false;
}
return true;


Edit: removed extra opening [ on the hor lines.
Re: Question (response to post by Minilek)
My guess is they really are looking for something like
for (int i=0; i<3; i++)
  for (int j=0; j<3; j++) {
     int hor = 0;
     int vert = 0;
     int square = 0;
     for (int x=0; x<3; x++)
       for (int y=0; y<3; y++) {
         hor |= 1<< grid[3*i + j][[3*x + y];
         vert |= 1<< grid[3*x+y][3*i+j];
         square |= 1<<grid[3*i+x][3*j+y];
       }
     }
     if (hor != 0x3FE || vert != 0x3FE || square != 0x3FE) 
       return false;
  }
}
return true;   


If they really wanted a single unnested loop, it's possible using
int hor = 0;
int vert = 0;
int square = 0;
for (int c = 0; c < 81; c++) {
  int x = c/27;
  int y = (c/9)%3;
  int i = (c/3)%3;
  int j = c%3;
  if (x+y==0) hor = vert = square = 0;
  hor |= 1<< grid[3*i + j][[3*x + y];
  vert |= 1<< grid[3*x+y][3*i+j];
  square |= 1<<grid[3*i+x][3*j+y];
  if (x+y==4 && (hor != 0x3FE || vert != 0x3FE || square != 0x3FE)) 
       return false;
}
return true;