ENG  RUSTimus Online Judge
Online Judge
Problems
Authors
Online contests
About Online Judge
Frequently asked questions
Site news
Webboard
Links
Problem set
Submit solution
Judge status
Guide
Register
Update your info
Authors ranklist
Current contest
Scheduled contests
Past contests
Rules
back to board

Discussion of Problem 1033. Labyrinth

What's wrong with my input code? I always have WA #1
Posted by me 1 Oct 2010 13:48
My program seems to work correctly on all tests I tried, including all the tests that could be found in topics of this problem's discussion. Nevertheless, I always have WA #1 when I submit it as a solution, so I guess something is wrong not with the algorithm, but with I/O code. Here it is (I know that the rules of the board deny posting code, but this is only input/output code, no part of algorithm is exposed):

int main(int argc, char **argv){
  int n;
  std::cin >> n;

  Labyrinth lab(n);

  for(int i = 0; i < n; i++){
    std::string s;
    std::getline(std::cin, s);
    // strip the last linebreak, if any
    if(s[s.length() - 1] == '\n') s.resize(s.length() - 1);
    // if the string is empty
    // (for example,  after reading the
    // number from the first line),
    // reread it (modifying the current line index)
    if(s.length() == 0) { i--; continue; }
    for(int j = 0; j < n; j++){
      if(s[j] == '#') lab.add_wall_block(j, i);
    }
  }
  std::cout << 9 * lab.total_walls_visible() << std::endl;
  return 0;
}

(if the formatting is broken, here is the alternative: http://itpaste.ru/92461 )

Edited by author 01.10.2010 13:49
Re: What's wrong with my input code? I always have WA #1
Posted by me 1 Oct 2010 14:37
Oops, I'm sorry, I got AC without any modifications of I/O code. The problem was rather unexpected though: I use g++ on 64-bit linux, and it aligns all the variables to the 8 byte blocks. There was one line in algorithm where I accessed the integer which was one-off the array. Because of the alignment, there was some free space after the array (silently initialized to zero by g++), so accessing the variable was harmless: I was reading the point to visit next, and I always got (0, 0) as the coordinate, which was to visit anyway.
Timus's compiler works on 32-bit machine (as far as I know) and didn't align the array, so I was getting an invalid coordinate. Thanks to the valgrind, I found the problem.