Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/154.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
骑士巡回赛c++;递归的 我试图用递归函数在C++中完成骑士巡游,但是这个程序只需多次执行就可以退出。主要的概念就是用一个函数来强行上路,该函数通过跳转到任何开放的地方并尝试去下一个地方来找到一条路。如果它在内存中阻塞了自身,则只需返回false并尝试下一个选项,依此类推。我是完全放弃了这种方法,还是错过了什么 #include <iostream> #include <math.h> #include <numeric> #include <cstdlib> #include <ctime> #include <string.h> #include <stdio.h> #include <sstream> #include <fstream> #include <limits> using namespace std; struct sah{ int pos[8][8] = {{0}}; //fill whole board with 0 int x; int y; int fre=64; //number of free spaces on board bool free (int xx,int yy); }; bool sah::free (int xx,int yy) { pos[xx][yy]=1; for(int a=0;a!=8;a++){ for(int b=0;b!=8;b++){ cout<<pos[a][b]<<" "; } cout<<endl; } if(pos[xx+2][yy-1]==0&&pos[xx+2][yy-1]!=NULL&&free(xx+2,yy-1)!=false) cout<<"hai"; else if(pos[xx-2][yy-1]==0&&pos[xx-2][yy-1]!=NULL&&free(xx-2,yy-1)!=false) cout<<"hai"; else if(pos[xx+2][yy+1]==0&&pos[xx+2][yy+1]!=NULL&&free(xx+2,yy+1)!=false) cout<<"hai"; else if(pos[xx-2][yy+1]==0&&pos[xx-2][yy+1]!=NULL&&free(xx-2,yy+1)!=false) cout<<"hai"; else if(pos[xx+1][yy-2]==0&&pos[xx+1][yy-2]!=NULL&&free(xx+1,yy-2)!=false) cout<<"hai"; else if(pos[xx-1][yy-2]==0&&pos[xx-1][yy-2]!=NULL&&free(xx-1,yy-2)!=false) cout<<"hai"; else if(pos[xx+1][yy+2]==0&&pos[xx+1][yy+2]!=NULL&&free(xx+1,yy+2)!=false) cout<<"hai"; else if(pos[xx-1][yy+2]==0&&pos[xx-1][yy+2]!=NULL&&free(xx-1,yy+2)!=false) cout<<"hai"; else{ pos[xx][yy]=0; cout<<"kek"<<xx<<yy; return false; } for(int n=0;n!=8;n++){ for(int i=0;i!=8;i++){ if(pos[n][i]==1) fre=fre-1; } } cout<<fre<<" "; } int main(int argc, char** argv) { sah chess; chess.x=0; chess.y=0; if(chess.free(chess.x,chess.y)!=false) cout<<"end"; }_C++ - Fatal编程技术网

骑士巡回赛c++;递归的 我试图用递归函数在C++中完成骑士巡游,但是这个程序只需多次执行就可以退出。主要的概念就是用一个函数来强行上路,该函数通过跳转到任何开放的地方并尝试去下一个地方来找到一条路。如果它在内存中阻塞了自身,则只需返回false并尝试下一个选项,依此类推。我是完全放弃了这种方法,还是错过了什么 #include <iostream> #include <math.h> #include <numeric> #include <cstdlib> #include <ctime> #include <string.h> #include <stdio.h> #include <sstream> #include <fstream> #include <limits> using namespace std; struct sah{ int pos[8][8] = {{0}}; //fill whole board with 0 int x; int y; int fre=64; //number of free spaces on board bool free (int xx,int yy); }; bool sah::free (int xx,int yy) { pos[xx][yy]=1; for(int a=0;a!=8;a++){ for(int b=0;b!=8;b++){ cout<<pos[a][b]<<" "; } cout<<endl; } if(pos[xx+2][yy-1]==0&&pos[xx+2][yy-1]!=NULL&&free(xx+2,yy-1)!=false) cout<<"hai"; else if(pos[xx-2][yy-1]==0&&pos[xx-2][yy-1]!=NULL&&free(xx-2,yy-1)!=false) cout<<"hai"; else if(pos[xx+2][yy+1]==0&&pos[xx+2][yy+1]!=NULL&&free(xx+2,yy+1)!=false) cout<<"hai"; else if(pos[xx-2][yy+1]==0&&pos[xx-2][yy+1]!=NULL&&free(xx-2,yy+1)!=false) cout<<"hai"; else if(pos[xx+1][yy-2]==0&&pos[xx+1][yy-2]!=NULL&&free(xx+1,yy-2)!=false) cout<<"hai"; else if(pos[xx-1][yy-2]==0&&pos[xx-1][yy-2]!=NULL&&free(xx-1,yy-2)!=false) cout<<"hai"; else if(pos[xx+1][yy+2]==0&&pos[xx+1][yy+2]!=NULL&&free(xx+1,yy+2)!=false) cout<<"hai"; else if(pos[xx-1][yy+2]==0&&pos[xx-1][yy+2]!=NULL&&free(xx-1,yy+2)!=false) cout<<"hai"; else{ pos[xx][yy]=0; cout<<"kek"<<xx<<yy; return false; } for(int n=0;n!=8;n++){ for(int i=0;i!=8;i++){ if(pos[n][i]==1) fre=fre-1; } } cout<<fre<<" "; } int main(int argc, char** argv) { sah chess; chess.x=0; chess.y=0; if(chess.free(chess.x,chess.y)!=false) cout<<"end"; }

骑士巡回赛c++;递归的 我试图用递归函数在C++中完成骑士巡游,但是这个程序只需多次执行就可以退出。主要的概念就是用一个函数来强行上路,该函数通过跳转到任何开放的地方并尝试去下一个地方来找到一条路。如果它在内存中阻塞了自身,则只需返回false并尝试下一个选项,依此类推。我是完全放弃了这种方法,还是错过了什么 #include <iostream> #include <math.h> #include <numeric> #include <cstdlib> #include <ctime> #include <string.h> #include <stdio.h> #include <sstream> #include <fstream> #include <limits> using namespace std; struct sah{ int pos[8][8] = {{0}}; //fill whole board with 0 int x; int y; int fre=64; //number of free spaces on board bool free (int xx,int yy); }; bool sah::free (int xx,int yy) { pos[xx][yy]=1; for(int a=0;a!=8;a++){ for(int b=0;b!=8;b++){ cout<<pos[a][b]<<" "; } cout<<endl; } if(pos[xx+2][yy-1]==0&&pos[xx+2][yy-1]!=NULL&&free(xx+2,yy-1)!=false) cout<<"hai"; else if(pos[xx-2][yy-1]==0&&pos[xx-2][yy-1]!=NULL&&free(xx-2,yy-1)!=false) cout<<"hai"; else if(pos[xx+2][yy+1]==0&&pos[xx+2][yy+1]!=NULL&&free(xx+2,yy+1)!=false) cout<<"hai"; else if(pos[xx-2][yy+1]==0&&pos[xx-2][yy+1]!=NULL&&free(xx-2,yy+1)!=false) cout<<"hai"; else if(pos[xx+1][yy-2]==0&&pos[xx+1][yy-2]!=NULL&&free(xx+1,yy-2)!=false) cout<<"hai"; else if(pos[xx-1][yy-2]==0&&pos[xx-1][yy-2]!=NULL&&free(xx-1,yy-2)!=false) cout<<"hai"; else if(pos[xx+1][yy+2]==0&&pos[xx+1][yy+2]!=NULL&&free(xx+1,yy+2)!=false) cout<<"hai"; else if(pos[xx-1][yy+2]==0&&pos[xx-1][yy+2]!=NULL&&free(xx-1,yy+2)!=false) cout<<"hai"; else{ pos[xx][yy]=0; cout<<"kek"<<xx<<yy; return false; } for(int n=0;n!=8;n++){ for(int i=0;i!=8;i++){ if(pos[n][i]==1) fre=fre-1; } } cout<<fre<<" "; } int main(int argc, char** argv) { sah chess; chess.x=0; chess.y=0; if(chess.free(chess.x,chess.y)!=false) cout<<"end"; },c++,C++,对于任何对最终工作代码/解决方案感兴趣的人,这里是最终版本。这仍然只是一种蛮力方法,远未达到最佳效果,但它可能有助于: #include <iostream> using namespace std; struct chess{ int pos[8][8] = {{0}}; //fill whole board with 0 int x; int y; int all=0; int fre=64;

对于任何对最终工作代码/解决方案感兴趣的人,这里是最终版本。这仍然只是一种蛮力方法,远未达到最佳效果,但它可能有助于:

#include <iostream>
using namespace std;

struct chess{

    int pos[8][8] = {{0}};          //fill whole board with 0
    int x;
    int y;
    int all=0;
    int fre=64;                     //number of free spaces on board

    bool free (int xx,int yy);

};

bool chess::free (int xx,int yy)
{
    all++;

pos[xx][yy]=1;
    for(int n=0;n!=8;n++){
        for(int i=0;i!=8;i++){
            if(pos[n][i]==1)
            fre=fre-1;  
        }
    }
    cout<<endl<<endl;
    for(int a=0;a!=8;a++){
        for(int b=0;b!=8;b++){
            cout<<pos[a][b]<<" ";   
        }
        cout<<endl;
    }
    cout<<endl;

    if(pos[xx+2][yy-1]==0&&yy-1>0&&xx+2<9&&free(xx+2,yy-1)!=false)
    cout<<"success";
    else if(pos[xx-2][yy-1]==0&&xx-2>0&&yy-1>0&&free(xx-2,yy-1)!=false)
    cout<<"success";        
    else if(pos[xx+2][yy+1]==0&&yy+1<9&&xx+2<9&&free(xx+2,yy+1)!=false)
    cout<<"success";        
    else if(pos[xx-2][yy+1]==0&&yy+1<9&&xx-2>0&&free(xx-2,yy+1)!=false)
    cout<<"success";        
    else if(pos[xx+1][yy-2]==0&&xx+1<9&&yy-2>0&&free(xx+1,yy-2)!=false)
    cout<<"success";        
    else if(pos[xx-1][yy-2]==0&&xx-1>0&&yy-2>0&&free(xx-1,yy-2)!=false)
    cout<<"success";        
    else if(pos[xx+1][yy+2]==0&&yy+2<9&&xx+1<9&&free(xx+1,yy+2)!=false)
    cout<<"success";    
    else if(pos[xx-1][yy+2]==0&&yy+2<9&&xx-1>0&&free(xx-1,yy+2)!=false)
    cout<<"success";    
    else{
            if(fre==0)
            return true;
            pos[xx][yy]=0;
            cout<<"   "<<xx<<","<<yy;
            return false;
    }
}

int main(int argc, char** argv) {

    chess knight;
    knight.x=0;
    knight.y=0;
    if(knight.free(knight.x,knight.y)==true)
    cout<<endl<<endl<<endl<<knight.all;
    return 0;
}
#包括
使用名称空间std;
结构象棋{
int pos[8][8]={{0};//用0填充整个电路板
int x;
int-y;
int all=0;
int fre=64;//板上的可用空间数
无布尔(整数xx,整数yy);
};
布尔国际象棋::免费(整数xx,整数yy)
{
全部++;
pos[xx][yy]=1;
对于(int n=0;n!=8;n++){
for(int i=0;i!=8;i++){
如果(位置[n][i]==1)
fre=fre-1;
}
}

cout您的问题在于
if
条件,如

pos[xx+2][yy-1]==0&&pos[xx+2][yy-1]!=NULL

这总是
false
,因为
NULL
和0在您这里使用的上下文中是等效的。所以您有
x==0和x!=0
,它们总是
false

我注意到您的代码中有以下错误

  • sha::free
    在关闭
    }
    之前没有
    return
    语句。这就是未定义行为的原因

    我不清楚当函数到达该点时,返回值应该是
    false
    还是
    true

  • 您正在使用
    pos[xx+2][yy-1]!=空
    。您似乎试图与指针进行比较,但
    pos[xx+2][yy-1]
    不是指针。它是一个整数。从你的帖子中不清楚你的意图是什么

  • 您正在使用无效索引访问
    pos
    ,例如
    pos[xx+2][yy-1]
    pos[xx-2][yy-1
    ,这也是导致未定义行为的原因

    如果
    xx
    为6或更大,
    xx+2
    为无效索引。如果
    yy
    为0,
    yy-1
    为无效索引

    我建议对索引进行以下修复

    xx+2
    需要是
    (xx+2)%8

    yy-1
    需要是
    (yy-1+8)%8

    xx+1
    xx-1
    xx-1
    yy+1
    yy+2
    yy-2
    也需要进行类似的更改

    您可能需要使用函数来封装逻辑

    例如

    然后使用:

    // Don't use this
    // if( pos[xx+2][yy-1]==0 &&pos[xx+2][yy-1]!=NULL && free(xx+2,yy-1)!=false )
    
    // Use this.
    if ( pos[plusIndex(xx, 2)][minusIndex(yy, 1)] != 0 &&
         ...
    
  • 使用时,在递归调用中将无效索引传递给
    free

    free(xx+2,yy-1)
    
    它们中的一个或两个都可能是无效索引。请使用

    free(plusIndex(xx, 2), minusIndex(yy, 1))
    
  • 免责声明


    上述更改不会解决您发布的代码中的任何算法错误。

    调整第3点(索引)。通过您的修复,无效值变为有效值,就像考虑一个环形板。无效索引不应该(在板外)吗刚刚被拒绝?@Bob_uuuu,这是有道理的。当它们无效时,该项目根本不存在于电路板上。这就是为什么帖子中的粗体免责声明:)谢谢你的帮助。主要问题是,正如你所说的!=NULL。该程序的工作原理就是修复该状态,不允许任何大于或小于电路板大小的整数。可能有还有一些错误,因为这段代码不是最终版本,但大多数情况下应该可以工作。谢谢!
    free(xx+2,yy-1)
    
    free(plusIndex(xx, 2), minusIndex(yy, 1))