Aspose Cells. Get rows by group - aspose

How can I read rows by group using Aspose?
example

See the following sample code using Aspose.Cells for your reference:
e.g.
Sample code:
//Loading the file
Workbook book = new Workbook("e:\\test2\\Bk_readgrouped.xlsx");
//Get the first worksheet in the workbook
Worksheet sheet = book.Worksheets[0];
int maxRow = sheet.Cells.MaxDataRow;
int maxCol = sheet.Cells.MaxDataColumn;
int chk = 0;
bool pname = false;
Console.WriteLine("Retrieving each group's data");
for (int i = 0; i <= maxRow; i++)
{
int rowOutlineLevel = sheet.Cells.GetGroupedRowOutlineLevel(i);
if (rowOutlineLevel > 0)
{
pname = true;
if (pname== true & chk != rowOutlineLevel)
{
Console.WriteLine("\n");
Console.WriteLine("Group:" + rowOutlineLevel);
pname = false;
chk = rowOutlineLevel;
}
for (int j = 0; j <= maxCol; j++)
{
Console.Write(sheet.Cells[i, j].StringValue + "\t");
}
Console.WriteLine();
}
}
Hope, this helps a bit.
PS. I am working as Support developer/ Evangelist at Aspose.

This works for me:
public virtual void ProcessExcelSheetTree (Worksheet Excelsheet, int current_level)
{
int current_row = 1;
int maxRow = Excelsheet.Cells.MaxDataRow;
int firstGroupIndex = 0;
while(Excelsheet.Cells[current_row, 0].StringValue != "")
{
var currentRow = Excelsheet.Cells.Rows[current_row];
if(currentRow.GroupLevel == current_level)
{
string targetName = Excelsheet.Cells[current_row, 0].StringValue;
Console.WriteLine(targetName);
firstGroupIndex = currentRow.Index;
for (int i = firstGroupIndex + 1; i <= maxRow; i++)
{
if(Excelsheet.Cells.Rows[i].GroupLevel > current_level)
{
string subTargetName = Excelsheet.Cells[i, 0].StringValue;
Console.WriteLine(" - " + subTargetName);
}
else
{
break;
}
}
}
current_row++;
}
}

Related

(C++) How could I use these methods to convert between ASCII and utf16? for windows API

I have some methods I wrote. I tried my best. When it comes to how I understand ASCII and UTF16 these should work. I would like some kind of function like these so that I can work with the win32 API stuff. I would like to be able to convert between ASCII and UTF16 because I just need things to work for the English language.
wchar_t* fromascii(const char* inv) {
wchar_t* ret;
vector<char> trans;
int cnt = 0;
while (inv[cnt] != 0) {
trans.push_back(inv[cnt]);
cnt++;
}
trans.push_back(0);
ret = new wchar_t[trans.size()];
for (int i = 0; i < trans.size(); i++) ret[i] = trans.at(i);
return ret;
}
wchar_t* fromascii(string inv) {
wchar_t* ret;
ret = new wchar_t[inv.size() + 1];
for (int i = 0; i < inv.size(); i++) {
const char* sam = inv.substr(i, 1).c_str();
int val = sam[0];
ret[i] = val;
}
ret[inv.size()] = 0;
return ret;
}
string toString(wchar_t* inv) {
string ret;
vector<char> trans;
int cnt = 0;
int sam;
while (inv[cnt] != 0) {
if (inv[cnt] > 31 && inv[cnt] < 127) {
sam = inv[cnt];
trans.push_back(cnt);
}
cnt++;
}
for (int i = 0; i < trans.size(); i++) ret += trans.at(i);
return ret;
}
char* toanascii(wchar_t* inv){
char* ret;
vector<char> trans;
int cnt = 0;
int sam;
while (inv[cnt] != 0) {
if (inv[cnt] > 31 && inv[cnt] < 127) {
sam = inv[cnt];
trans.push_back(cnt);
}
cnt++;
}
trans.push_back(0);
ret = new char[trans.size()];
for (int i = 0; i < trans.size(); i++) ret[i] = trans.at(i);
return ret;
}
Somehow they don't.

My word search II solution is very slow (Leetcode 212)

My solution is correct and passes all test cases, but my solution is very slow (faster than 7% of C++ solutions).
Question:
Given an m x n board of characters and a list of strings words, return all words on the board.
Each word must be constructed from letters of sequentially adjacent cells, where adjacent cells are horizontally or vertically neighboring. The same letter cell may not be used more than once in a word.
For example:
Input: board = [["o","a","a","n"],["e","t","a","e"],["i","h","k","r"],["i","f","l","v"]], words = ["oath","pea","eat","rain"]
Output: ["eat","oath"]
By the way, I don't have Leetcode premium and looked at the discussion. I see that other people are using recursion. I am using a stack, but this shouldn't really be a problem. Does anyone see any performance issues with my code? The complexity should be O(n^2*3^n)
class Solution {
public:
vector<string> findWords(vector<vector<char>>& board, vector<string>& words) {
vector<string> ret;
Trie* root = new Trie();
for (const auto &i:words) {
root->insert(i);
}
Trie* cnode = root;
int numRow = board.size();
int numCol = board[0].size();
vector<int> temp(numCol, 0);
vector<vector<int>> visited(numRow, temp);
for (int i = 0; i < numRow; ++i) {
for (int j = 0; j < numCol; ++j) {
stack<pair<int, int>> searcher;
searcher.push(make_pair(i,j));
while (!searcher.empty()) {
int row = searcher.top().first;
int col = searcher.top().second;
int cur = board[row][col] - 97;
if (visited[row][col]) {
cnode = cnode->parent;
visited[row][col] = 0;
searcher.pop();
} else if (cnode->child[cur] == nullptr) {
searcher.pop();
visited[row][col] = 0;
} else {
visited[row][col] = 1;
cnode = cnode->child[cur];
if (cnode->contain != "") {
ret.push_back(cnode->contain);
cnode->contain = "";
}
if (row + 1 < numRow && !visited[row + 1][col]) {
searcher.push(make_pair(row+1, col));
}
if (row - 1 >= 0 && !visited[row-1][col]) {
searcher.push(make_pair(row-1, col));
}
if (col + 1 < numCol && !visited[row][col+1]) {
searcher.push(make_pair(row, col+1));
}
if (col - 1 >= 0 && !visited[row][col-1]) {
searcher.push(make_pair(row, col-1));
}
}
}
}
}
return ret;
}
class Trie {
public:
vector<Trie*> child;
Trie* parent;
string contain;
Trie() {
child = vector<Trie*>(26, nullptr);
contain = "";
parent = nullptr;
}
void insert(string word) {
Trie* root = this;
for (int i = 0; i < word.size(); ++i) {
int loc = word[i] - 97;
if (root->child[loc] == nullptr) {
root->child[loc] = new Trie();
root->child[loc]->parent = root;
}
root = root->child[loc];
}
root->contain = word;
}
};
};

TSP recursive solution to its iterative form

I have a function which returns the shortest distance possible between n cities. AKA Travelling salesman problem.
int tsp(int mask, int pos, int n)
{
if (mask == VISITED_ALL)
{
return dist[pos][0];
}
int result = 2147483647;
for (int i = 0; i < n; i++)
{
if ((mask & (1 << i)) == 0)
{
int new_result = dist[pos][i] + tsp(mask | (1 << i), i, n);
result = min(result, new_result);
}
}
return result;
}
I would like to modify this recursive solution to iterative form, however I am struggling to do so. I am following this guide which describes conversion from recursive solution to iterative with couple of exapmles https://www.codeproject.com/Articles/418776/How-to-replace-recursive-functions-using-stack-and
THIS is what I've tried, but doesn't seem to work
int tspIterative(int mask, int pos, int n)
{
struct MyStructure
{
int mask;
int pos;
int n;
int new_result;
int result;
int stage;
};
int retVal = 0;
stack<MyStructure> myStack;
MyStructure currentStruct;
currentStruct.mask = mask;
currentStruct.pos = pos;
currentStruct.n = n;
currentStruct.new_result = 0;
currentStruct.result = 2147483647;
currentStruct.stage = 0;
myStack.push(currentStruct);
while (myStack.empty() != true)
{
currentStruct = myStack.top();
myStack.pop();
switch (currentStruct.stage)
{
case 0:
if (currentStruct.mask == VISITED_ALL)
{
retVal = dist[pos][0];
continue;
}
else
{
currentStruct.stage = 1;
myStack.push(currentStruct);
for (int i = 0; i < currentStruct.n; i++)
{
if ((currentStruct.mask & (1 << i)) == 0)
{
MyStructure newStruct;
newStruct.mask = currentStruct.mask | (1 << i);
newStruct.pos = i;
newStruct.n = currentStruct.n;
newStruct.result = currentStruct.result;
newStruct.new_result = currentStruct.new_result;
newStruct.stage = 0;
myStack.push(newStruct);
}
}
continue;
break;
case 1:
for (int i = 0; i < currentStruct.n; i++)
{
if ((currentStruct.mask & (1 << i)) == 0)
{
currentStruct.new_result = dist[currentStruct.pos][i] + retVal;
currentStruct.result = min(currentStruct.result, currentStruct.new_result);
retVal = currentStruct.result;
}
}
continue;
break;
}
}
return retVal;
}
}
Okay, so I have figured it out. Here's my solution if anyone's interested.
int tspIterative(int mask, int pos, int n)
{
struct MyStructure
{
int mask;
int pos;
int n;
int new_result;
int result;
int nextPos;
int stage;
};
int retVal = 0;
int k = 0;
stack<MyStructure> myStack;
MyStructure currentStruct;
currentStruct.mask = mask;
currentStruct.pos = pos;
currentStruct.n = n;
currentStruct.new_result = 0;
currentStruct.result = 2147483647;
currentStruct.nextPos = 0;
currentStruct.stage = 0;
myStack.push(currentStruct);
while (myStack.empty() != true)
{
currentStruct = myStack.top();
myStack.pop();
switch (currentStruct.stage)
{
case 0:
{
jump:
if (currentStruct.mask == VISITED_ALL)
{
retVal = dist[currentStruct.pos][0];
continue;
}
else
{
currentStruct.stage = 1;
myStack.push(currentStruct);
for (int i = currentStruct.nextPos + k; i < currentStruct.n; i++)
{
if ((currentStruct.mask & (1 << i)) == 0)
{
MyStructure newStruct;
newStruct.mask = (currentStruct.mask) | (1 << i);
newStruct.pos = i;
newStruct.n = currentStruct.n;
newStruct.result = 2147483647;
newStruct.new_result = 0;
newStruct.nextPos = 0;
newStruct.stage = 0;
currentStruct = myStack.top();
myStack.pop();
currentStruct.nextPos = i;
myStack.push(currentStruct);
myStack.push(newStruct);
break;
}
}
continue;
}
break;
}
case 1:
{
k = 0;
for (int i = currentStruct.nextPos + k; i < currentStruct.n; i++)
{
if ((currentStruct.mask & (1 << i)) == 0)
{
if (k > 0)
{
goto jump;
}
currentStruct.new_result = dist[currentStruct.pos][i] + retVal;
currentStruct.result = min(currentStruct.result, currentStruct.new_result);
retVal = currentStruct.result;
k = 1;
}
}
continue;
break;
}
}
}
return retVal;
}

Detect consecutive objects with a common attribute in 2-D array

I have a 2-D array of gems, all the gems have been given random color. Now i want to detect if three or more consecutive gems in a row or column has the same color. If so i want to do some action with those gems.
Gem gems[10][10];
for(int i=0;i<10;++i){
for(int j=0;j<10;++j){
gems[i][j].setColor(GetRandInRange(0,6));
}
}
bool detectMatch(){
for(int i=0;i<10;++i){
for(int j=0;j<10;++j){
// Code to detect three or more consecutive gems with same color
// Give some special color to the matched gems
}
}
Here is how i tried but it doesn't work
bool GameBoard::findMatch(){
for(int i=0;i<10;++i){
int count=0;
for(int j=0;j<10;++j){
if(j!=0){
if(gems[i][j].getColor()==gems[i][j-1].getColor()){ //Color same with previous one
int a=i, b=j;
while(gems[a][b].getColor()==gems[i][j].getColor() && (a<10 && b<10)){ // Check till the color does not match
count++;
a++;
b++;
}
if(count>=3){ //If 3 or more have matched
for(int a=i, b=j, c=0;c<count;++c){
gems[a][b].setColor(0);
}
return true;
}
}
}
}
}
return false;
}
if you can help me with this code please
Here's how I'd go about it. You need to do two scans. First, you need to scan for runs in one direction, then the other. It's much simpler than trying to loop through once.
I first check for horizontal runs, exiting as soon as one longer than 2 is found. Same with the vertical ones. Your function has a bool signature, so I've assumed you'll use another function to determine the location of the run - you could easily return a struct that held the position, direction and length from the findMatch method.
"use strict";
window.addEventListener('load', onLoaded, false);
var gems;
function onLoaded(evt) {
// create and initialize the array
gems = new Array();
for (var row = 0; row < 10; row++) {
let curRow = new Array();
for (var col = 0; col < 10; col++) {
let curCell = new Gem();
curCell.setColor(GetRandInRange(0, 6));
curRow.push(curCell);
}
gems.push(curRow);
}
// display it for the user
displayGems();
// check if there's 3 or more in a vertical or horizontal line.
console.log(hasMatch());
}
class Gem {
setColor(colIndex) {
this.color = colIndex;
}
getColor() {
return this.color;
}
};
function displayGems() {
var str = '';
for (var row = 0; row < 10; row++) {
if (row != 0)
str += "\n";
var dat = gems[row];
dat.forEach((gem, idx) => {
if (idx != 0)
str += ', ';
str += gem.getColor();
});
}
console.log(str);
}
function GetRandInRange(lower, upper) {
return ((Math.random() * (upper - lower)) + lower + 0.5) >> 0;
}
function hasMatch() {
let matchFound = 0;
// scan #1 - horizontally on each row
for (var row = 0; row < 10; row++) {
let last = undefined;
let runLength = 0;
for (var col = 0; col < 10; col++) {
let cur = gems[row][col].getColor();
if (cur == last) {
runLength++
if (runLength > 2) {
console.log(`horiz - ${row+1}`);
return true;
}
} else {
runLength = 0;
last = cur;
}
}
}
for (var col = 0; col < 10; col++) {
let last = undefined;
let runLength = 1;
for (var row = 0; row < 10; row++) {
let cur = gems[row][col].getColor();
if (cur == last) {
runLength++;
if (runLength > 2) {
console.log(`vert - ${col+1}`);
return true;
}
} else {
runLength = 1;
last = cur;
}
}
}
return false;
}

Implementing Minimax Algorithm with two tasks

I have a major problem in my minimax algorithm. I have to implement a game, where is a player and an AI player. Both of them can step up,down,right,left diagonal and after that they have to block a cell, where the other player can't step (ofc, where the other player stays is a blocked cell and can't block that one, and you can't block an already blocked cell too). I wrote a little minimax algorithm for that, but for a strange thing the AI can't decide the best root.
struct Position
{
int x, y;
} MaxP, MinP;
int k = 2;
char board[10][10];
int currentPlayer = 2;
//Here are my global variables, MaxP is the AI, and MinP is the player.
I set the cells B (as blocked) around my table.
The minimax calculation algorithm:
int nyertesErtek(int result) //return a winning value, if result=1 Ai Wins, if result=2 Player Wins, if result=0, then the game it's still going
{
if (result == 1) return 10;
if (result == 2) return -10;
}
int minimax(char pBoard[10][10], int depth, bool isMaximizing)
{
int result = 0;
if (isMaximizing==true)
{
int checkX[8] = { -1,-1,0,1,1,1,0,-1 };
int checkY[8] = { 0,1,1,1,0,-1,-1,-1 };
bool lephetMax = false;
bool lephetMin = false;
for (int i = 0; i < 8; i++)
{
if (pBoard[MaxP.x + checkX[i]][MaxP.y + checkY[i]] != 'B')
{
lephetMax = true;
break;
}
}
if (lephetMax == false)
{
result = 2;
}
}
if (isMaximizing == false)
{
int checkX[8] = { -1,-1,0,1,1,1,0,-1 };
int checkY[8] = { 0,1,1,1,0,-1,-1,-1 };
bool lephetMin = false;
for (int i = 0; i < 8; i++)
{
if (pBoard[MinP.x + checkX[i]][MinP.y + checkY[i]] != 'B')
{
lephetMin = true;
break;
}
}
if (lephetMin == false)
{
result = 1;
}
}
if (result != 0) //ha nem terminal state
{
//cout << "Resultra jutott"<<endl;
//cout << "---------------->Kovetkezne a: " << isMaximizing << " Result=" << result << " NyeresErtek=" << nyertesErtek(result) <<" depth="<<depth<< "<---------------"<<endl;
//printBoard();
return nyertesErtek(result);
}
int checkX[8] = { -1,-1,0,1,1,1,0,-1 };
int checkY[8] = { 0,1,1,1,0,-1,-1,-1 };
if (isMaximizing)
{
int bestScore = -INT_MAX;
int szabadX;
int szabadY;
for (int l = 0; l < 8; l++)
{
if (pBoard[MaxP.x + checkX[l]][MaxP.y + checkY[l]] != 'B')
{
int oldMaxPX = MaxP.x;
int oldMaxPY = MaxP.y;
MaxP.x = MaxP.x + checkX[l];
MaxP.y= MaxP.y + checkY[l];
pBoard[MaxP.x][MaxP.y] = 'B';
pBoard[oldMaxPX][oldMaxPY] = 'o';
for (int i = 1; i <= k; i++)
{
for (int j = 1; j <= k; j++)
{
if (pBoard[i][j] != 'B')
{
szabadX = i;
szabadY = j;
pBoard[szabadX][szabadY] = 'B';
//cout << "Maximizing, depth=" << depth << endl;
//printBoard();
int score = minimax(pBoard, depth + 1, false);
pBoard[szabadX][szabadY] = 'o';
if (score > bestScore)
{
bestScore = score;
}
}
}
}
pBoard[MaxP.x][MaxP.y] = 'o';
pBoard[oldMaxPX][oldMaxPY] = 'B';
MaxP.x = oldMaxPX;
MaxP.y = oldMaxPY;
}
}
return bestScore;
}
else
{
int bestScore = INT_MAX;
int szabadX;
int szabadY;
for (int l = 0; l < 8; l++)
{
if (pBoard[MinP.x + checkX[l]][MinP.y + checkY[l]] != 'B')
{
int oldMinPX = MinP.x;
int oldMinPY = MinP.y;
MinP.x = MinP.x + checkX[l];
MinP.y = MinP.y + checkY[l];
//pBoard[MinP.x + checkX[l]][MinP.y + checkY[l]] = 'B';
pBoard[MinP.x][MinP.y] = 'B';
pBoard[oldMinPX][oldMinPY] = 'o';
//cout << "Minimizing depth= " << depth << endl;
//printBoard();
for (int i = 1; i <= k; i++)
{
for (int j = 1; j <= k; j++)
{
if (pBoard[i][j] != 'B')
{
szabadX = i;
szabadY = j;
pBoard[szabadX][szabadY] = 'B';
int score = minimax(pBoard, depth + 1, true);
//pBoard[MinP.x + checkX[l]][MinP.y + checkY[l]] = 'o';
pBoard[szabadX][szabadY] = 'o';
if (score < bestScore)
{
bestScore = score;
}
}
}
}
pBoard[MinP.x][MinP.y] = 'o';
pBoard[oldMinPX][oldMinPY] = 'B';
MinP.x = oldMinPX;
MinP.y = oldMinPY;
}
}
return bestScore;
}
}
And moving the AI is in this function:
void bestMove() {
int checkX[8] = { -1,-1,0,1,1,1,0,-1 };
int checkY[8] = { 0,1,1,1,0,-1,-1,-1 };
int bestScore = -INT_MAX;
Position move;
int AIBlockX, AIBlockY;
int szabadX;
int szabadY;
for (int l = 0; l < 8; l++)
{
if (board[MaxP.x + checkX[l]][MaxP.y + checkY[l]] != 'B')
{
int oldMaxPX = MaxP.x;
int oldMaxPY = MaxP.y;
MaxP.x = MaxP.x + checkX[l];
MaxP.y = MaxP.y + checkY[l];
board[MaxP.x][MaxP.y] = 'B';
board[oldMaxPX][oldMaxPY] = 'o';
for (int i = 1; i <= k; i++)
{
for (int j = 1; j <= k; j++)
{
if (board[i][j] != 'B')
{
szabadX = i;
szabadY = j;
board[szabadX][szabadY] = 'B';
int score = minimax(board, 0, false);
cout << endl << "Ennyi a score amit kaptam pech=" << score << endl;
board[szabadX][szabadY] = 'o';
if (score > bestScore)
{
bestScore = score;
move.x = MaxP.x;
move.y = MaxP.y;
AIBlockX = szabadX;
AIBlockY = szabadY;
cout << "BESTMOVE: " << move.x << " " << move.y << " bestscore= " << bestScore << endl;
}
}
}
}
board[MaxP.x][MaxP.y] = 'o';
board[oldMaxPX][oldMaxPY] = 'B';
MaxP.x = oldMaxPX;
MaxP.y = oldMaxPY;
}
}
board[move.x][move.y] = 'B';
board[MaxP.x][MaxP.y] = 'o';
MaxP.x = move.x;
MaxP.y = move.y;
board[AIBlockX][AIBlockY] = 'B';
currentPlayer = 2;
}
I think my main problem is something in the minimax function: when I check where the player can go -> and after that all the possible blocking moves. And in that triple for when I rewrite the best score with the current score I can't return the value directly because I have to check other possible moves in the firs for iteration.
So after all (:D) my question is that how can I save the best move after the cell blocking was done and return that to my AI to step there? Thank you for you answer. :)
ps:I'm sorry if this post is too long, but I thought to fully understand my problem, you also have to see the algorithm part.