小编Gya*_*shu的帖子

数独解算器程序

solveSudokumain()函数调用函数.

我已经编写了以下函数来解决数独:

#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)

c++ sudoku backtracking

6
推荐指数
1
解决办法
1305
查看次数

给定一个不能复制的索引列表,将元素从向量复制到另一个元素的最有效方法

假设我有一个向量V = {5, 10, 2, 1, 6}和一个list of indices= {2, 3, 0}.现在,生成的数据结构U应该包含{10, 6}不一定按顺序排列的元素.天真的方法将具有时间复杂性O(n^2).我们可以更好吗?

c++ arrays stl list vector

2
推荐指数
1
解决办法
475
查看次数

Strtok里面的strtok没有使用原始令牌的副本 - C.

我意识到标题令人困惑,想不到更明确的方式来说出来.基本上,我在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)

c copy token strtok

1
推荐指数
1
解决办法
465
查看次数

标签 统计

c++ ×2

arrays ×1

backtracking ×1

c ×1

copy ×1

list ×1

stl ×1

strtok ×1

sudoku ×1

token ×1

vector ×1