幅検索の注意.

SRM453.5 div2 medium

int longestPath(vector  maze, int startRow, int startCol, vector  moveRow, vector  moveCol) {
    int row = maze.size();
    int col = maze[0].length();
    int step[row][col];
    //初期化
    for(int i = 0; i < row; i++) {
      for(int j = 0; j < col; j++) {
	step[i][j] = INF;
      }
    }
    queue > que;
    step[startRow][startCol] = 0;
    que.push(pair(startRow, startCol));
// int cnt = 0  //とかはいらない.
    int sz = moveRow.size();
    // move,if que is empty.
    while(!que.empty()) {  //!をわすれないように。
      pair q = que.front(); que.pop();
    //cnt++  //つけちゃだめ!!
      for(int i = 0; i < sz; i++) {
	int newRow = q.first + moveRow[i];
	int newCol = q.second + moveCol[i];
	//within range
	if(newRow >= 0 && newRow < row && newCol >= 0 && newCol < col ) {
	    //if he can passed and hadn't yet passed
	    if(maze[newRow][newCol] == '.' && step[newRow][newCol] == INF) {  
            //newRowとrowの区別をしっかり.

//ここを cnt++にするとか、step[newRow][newCol]++するとかはしちゃだめ.
	      step[newRow][newCol] = step[q.first][q.second]+1;
	      que.push(pair(newRow, newCol) );
	      cout << newRow <<"::"<< newCol<< endl;
	    }
	  } //end if boundage decision
       } //end for
    } //end while


      int res = 0;
      for(int i = 0; i < row; i++) {
	for(int j = 0; j < col; j++) {
	  if(maze[i][j] == '.') {
	    res = max(step[i][j], res);
	  }
	}
      }
      return (res == INF) ? -1 : res;
  }