Topcoder SRM 590 DIV2 EASY

be careful with the boundary...
/* Topcoder SRM */
/*****************************************************************************/
string FoxAndGomoku::win(vector <string> board) {
    for (int i=0; i<sz(board); i++) {
        for (int j=0; j<sz(board[0]); j++) {
            if (board[i][j]=='.') {
                continue;
            }
            int k;
            for (k=1; k<5&&i+k>=0&&i+k<sz(board)&&board[i+k][j]=='o'; k++) { }
            if (k==5) { return "found"; }
            for (k=1; k<5&&j+k>=0&&j+k<sz(board[0])&&board[i][j+k]=='o'; k++) { }
            if (k==5) { return "found"; }
            for (k=1; k<5&&i+k>=0&&i+k<sz(board)&&j+k>=0&&j+k<sz(board[0])&&board[i+k][j+k]=='o'; k++) { }
            if (k==5) { return "found"; }
            for (k=1; k<5&&i+k>=0&&i+k<sz(board)&&j-k>=0&&j-k<sz(board[0])&&board[i+k][j-k]=='o'; k++) { }
            if (k==5) { return "found"; }
        }
    }
    return "not found";
}
/*****************************************************************************/

No comments :

Post a Comment