0

编辑:

     #include<iostream>
using namespace std;
#include<conio.h>
#include<string.h>

void dfsvisit(int a[][30], int i, const char *color[])
     {
        int v;
        int p[30];
        int f[30]={};
        static int j=1;
        color[i]="gray";

        for(v=1;v<31;v++)
            while(a[i][v]!=0)
                if(!strcmp(color[a[i][v]],"white"))
                    {
                        p[a[i][v]]=i;
                        dfsvisit(a,i,color);
                    }

        color[i]="black";
        j++;
        f[i]=1;
        cout<<f[i]<<" ";
     }

int main()
{
    int a[][30]={{2, 16},{4, 8},{5, 16, 21},{1, 2},{1, 9, 27},{1, 10, 15},{4, 6, 11, 12},{4, 16},{2, 19, 29},{3, 14, 28},{3, 13, 15, 17},{8, 9, 18, 26},{1, 7, 10, 19},{5, 8, 20, 24},{3, 21, 29},{1, 2, 8},{16, 23, 28, 29},{4, 12, 21, 24},   {3, 15, 16},{2, 3, 6, 22},{4, 15, 25},{4, 6, 20, 24},{9, 10, 11, 12},{19, 26, 30},{2, 27, 29},{1, 28, 29, 30},{8, 16, 29},{6, 10, 30},{19, 21, 27},{1, 2, 3, 22}};
    int i;
    const char *color[30];

    for(i=1;i<31;i++)
        color[i]="white";

    for(i=1;i<31;i++)
        if(!strcmp(color[i],"white"))
            dfsvisit(a,i,color);

    return 0;

}

只是想确定我是否使用const char c正确?如果是,我在哪里犯了错误?我正在尝试在代码块上运行它。它编译得很好,但是当我尝试运行它时,它显示程序已停止工作。

谢谢。

4

6 回答 6

3

改变:

for(i=1;i<=30;u++)

至:

for (i = 0; i < 30; i++)
于 2011-03-24T21:24:38.747 回答
2

你这样说:

for(i=1;i<=30;u++)

编译?

我建议将其更改ui...

编辑: 好的,所以你说这u是一个错字。下一步:在调试器中启动事物,等待它循环,进入它并单步执行。这应该告诉你循环变量发生了什么事情。

编辑 2:
<niggle>
我支持建议您使用 std::string 的每个人。您可能会考虑删除不规则数组以支持std::vectors of std::vectors,通过使用迭代器,它会为您捕获循环中的fencepost 错误。一般来说,STLvector比阵列更安全、更舒适,同时又快又小。
虽然是这样,但最好的做法是在函数范围( RAIIfor的开头而不是在函数范围的开头声明循环变量。当然,除非您在循环之后需要该值。 </niggle>

编辑 3:
有关 std::string、std::vector 和 std::otherStuff 的更多信息:Accelerated C++一书很好地介绍了 C++,尤其是 STL。像char*or 数组这样的概念只是在 STL 的做事方式被安全地覆盖和实践之后才出现。我真的很想在 Boost 库中看到类似的东西......

于 2011-03-24T21:22:32.443 回答
2

首先,你的for循环不正确,应该是这样的:

for(i = 0; i < 30; i++)

其次,您的数组int a[][30]应该是int a[30][](2、3 或 4 个整数的 30 个元素)。

第三,你不能像这样传递一个数组,使用一个指针。

好吧,这段代码不清楚,递归函数可能会导致堆栈溢出。

我希望它会帮助你。

于 2011-03-24T23:49:00.690 回答
2

您正在比较指向常量字符串的指针,当字符串相等时,它可能相等,也可能不相等。enum对这种算​​法使用 an :

enum { BLACK, GRAY, WHITE };
于 2011-03-24T21:17:05.250 回答
1

不,您没有正确使用 C 样式字符串 ( char *)。放弃它并改用它std::string

例如:

const char *color[30];

for(i=1;i<31;i++)
    color[i]="white";

上面的片段复制了指针并且不复制文本。这可能是也可能不是您想要的。

更安全地重写:

const char * color[30]; // 30 pointers to char.
const char text_white[] = "white";

for (i = 0; i < 30; ++i)  // Note addition of spaces
{
   color[i] = text_white;
}

const char text_white[]声明一个指向text_white常量数据的指针。这将使编译器知道您何时尝试分配给文字。

用于此的 C++ 代码:

typedef std::vector<std::string> Text_Container;
Text_Container texts;
const std::string white_text("white");
const unsigned int MAX_TEXTS = 30;
for (i = 0; i < MAX_TEXTS; ++i)
{
    texts[i] = white_text;  // Makes a copy of the string.
}

char *除非您的程序需要节省内存,否则请放弃文本。更喜欢std::stringstd::vector

于 2011-03-24T23:46:58.920 回答
1

您想使用 strcmp 而不是 == 进行字符串比较。

但是,尚不清楚为什么会挂起。您已删除可能使其进入无限循环的代码。

于 2011-03-24T21:20:47.500 回答