solveSudoku从main()函数调用函数.
我已经编写了以下函数来解决数独:
#include <iostream>
#include <vector>
using namespace std;
int isvalid(char k, vector<vector<char> > A, int i, int j) { //Checking if putting the current element is not in same row, column or box
for(int t = 0; t < 9; t++) {
if(A[t][j] == k) //Checking jth column
return 0;
if(A[i][t] == k) //Checking ith row
return 0;
if(A[(i/3)*3+t/3][(j/3)*3+t%3] == k) //Checking current box
return 0;
}
return 1;
}
bool sudoku(vector<vector<char> > &A, int i, …Run Code Online (Sandbox Code Playgroud) 假设我有一个向量V = {5, 10, 2, 1, 6}和一个list of indices= {2, 3, 0}.现在,生成的数据结构U应该包含{10, 6}不一定按顺序排列的元素.天真的方法将具有时间复杂性O(n^2).我们可以更好吗?
我意识到标题令人困惑,想不到更明确的方式来说出来.基本上,我在strtok循环中调用strtok循环,但是当内部strtok函数从runCommand返回时,我的第一个strtok循环停止.它只是退出循环,即使第一个分号后面还有其他参数.当我不调用runCommand()时,它按预期工作,并解析我用分号分隔的所有命令.
此代码的目标是解析由分号分隔的一行命令,然后解析命令和命令参数以便稍后进入execvp.这是我遇到麻烦的唯一部分.这里是:
void parseCommand(char *userLine)
{
if(strchr(userLine, ';'))
{
// Get first token
token = strtok(userLine, ";");
// Loop through all tokens
while(token != NULL)
{
// Make a copy
char *copy = malloc(strlen(token) + 1);
strcpy(copy, token);
runCommand(copy);
free(copy);
printf("process returned!\n");
token = strtok(NULL, ";");
}
}
}
void runCommand(char *token)
{
char *args[20];
char **command = args;
//Tokenize each command based on space
char *temp = strtok(token, " \n");
while (temp != NULL)
{
*command++ = temp;
temp …Run Code Online (Sandbox Code Playgroud)