实现图,节点/类语法c++

Implementing Graphs, node/class syntax C++

本文关键字:语法 c++ 节点 实现      更新时间:2023-10-16

我正在阅读关于图形的TopCoder教程http://community.topcoder.com/tc?module=Static&d1=tutorials&d2=graphsDataStrucs2,我正在尝试将伪代码实现到c++中。我的问题是:类和结构有什么不同?哪一个更适合处理图形?如何在c++中传递类节点到堆栈中?我试过这样做,但是当我通过s.push(node(top.x+1, top.y));时,它说没有匹配的函数找到。但是如果我像这样传递:

node a;
a.x=x;
a.y=y;
s.push(a);

不会产生任何错误。我只是想实现和编写问题的解决方案GrafixMask(伪代码在上面的链接中给出)。下面是我的部分实现:

#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <stack>
#include <queue>
#include <vector>
using namespace std;
bool fills[600][400];
class node
{
public:
int x;
int y;};
int doFill(int x, int y)
{
    int result=0;
    stack<node> s;
    node a;
    a.x=x;
    a.y=y;
    s.push(a);
    while(s.empty()==false)
{
    node top=s.top();
    s.pop();
    if(top.x < 0 || top.x>=600) continue;
    if (top.y < 0 || top.y >=400) continue;
    if(fills[top.x][top.y]) continue;
    fills[top.x][top.y]=true;
    result++;
    s.push(node(top.x+1, top.y));
    s.push(node(top.x, top. y+1));
    s.push(node(top.x, top.y-1));
    s.push(node(top.x-1, top.y));
}
return result;
}
int main()
{
for(int i=0; i < 599; i++)
    for(int j=0; j < 399; j++)
        fills[i][j]=false;
}

您的node类没有接受两个整数的构造函数。如果您想以这种方式构造节点,则需要有:

class node
{
public:
    int x;
    int y;
    node() : x( 0 ), y( 0 ) {}
    node( int pX, int pY ) : x( pX ), y( pY ) {}
};