void solveNQueens(vector<vector<string>>& res, vector<string>& s, int& n, int row) {
     if(row == n) {
         res.push_back(s);
         return;
     }
     for(int column = 0; column < n; column++) {
         if(isValidPos(s, n, row, column)) {
             s[row][column] = 'Q';
             solveNQueens(res, s, n, row + 1);
             s[row][column] = '.';  // reset to find another solutions
         }
     }
 }
Exemple #2
0
int CellGrid::getNumLiveNeighbors( int px, int py )
{
	int ln = ( isCellAlive( px, py ) ) ? -1 : 0;

	for( int x = px - 1, xc = 0; xc < 3; xc++, x++ )
	{
		for( int y = py - 1, yc = 0; yc < 3; yc++, y++ )
		{
			if( isValidPos( x, y ) && isCellAlive( x, y ) )
			{
				ln++;
			}
		}
	}

	return ln;
}
 void solver(vector<string> &cur, int row) {
     if(row == cur.size())
     {
         result.push_back(cur);
         return;
     }
     
     for(int col=0; col<cur.size(); col++)
     {
         if(isValidPos(cur, row, col))
         {
             cur[row][col] = 'Q';
             solver(cur, row+1);
             cur[row][col] = '.';
         }
     }
 }
void move( pos &charaPos, char dir )
{
	pos p = { charaPos.x, charaPos.y };
	switch (dir)
	{
	case 'W': case 'w':
		{
			p.y -= 1;
		}
		break;

	case 'S': case 's':
		{
			p.y += 1;
		}
		break;

	case 'A': case 'a':
		{
			p.x -= 1;
		}
		break;

	case 'D': case 'd':
		{
			p.x += 1;
		}
		break;
	}

	// 如果是有效的位置,就改变角色的位置。
	if( isValidPos( p ) )
	{
		charaPos.x = p.x;
		charaPos.y = p.y;
	}
}