首页 > poj一道基础广搜题,求解答

poj一道基础广搜题,求解答

原题地址

编译时出错了,好像是构造函数初始化的时候有问题?不知道怎么解决,求大神帮忙解答下?谢谢!

solution.cpp:17:37: error: ISO C++ forbids declaration of point’ with no type [-fpermissive]
   point(int p, int v, int xx, int yy) : position(p), value(v), x(xx), y(yy) {}
                                     ^

solution.cpp: In member function ‘int Point::point(int, int, int, int)’:
solution.cpp:17:41: error: only constructors take member initializers
   point(int p, int v, int xx, int yy) : position(p), value(v), x(xx), y(yy) {}
                             

代码:

#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string.h>
#include <string>
#include <vector>
using namespace std;

bool visited[25];
struct Point {
  int position;
  int value;
  int x;
  int y;
  Point(int p, int v, int xx, int yy) : position(p), value(v), x(xx), y(yy) {}
};

Point que[25];
int front = 0, rear = 25;

int main(int argc, char const *argv[]) {
  Point path[5][5];
  queue<Point> result;
  int cnt = 0;
  for (int i = 0; i < 5; i++)
    for (int j = 0; j < 5; j++) {
      cin >> path[i][j].value;
      cnt++;
      path[i][j].position = cnt + 1;
      path[i][j].x = i;
      path[i][j].y = j;
    }

  que[rear--] = path[0][0];
  visited[path[0][0].position] = true;
  result.push(path[0][0]);

  while (front != rear) {
    Point p = que[front];
    front++;
    if (p.position == 25) {
      for (size_t i = 0; i < result.size(); i++) {
        cout << "(" << result.front().x << "," << result.front().y << ")"
             << endl;
        result.pop();
      }
    } else {
      if (p.x - 1 >= 0 && !visited[p.position - 1] &&
          path[p.x - 1][p.y].value != 1) {
        que[rear--] = p(p.position - 1, path[p.x - 1][p.y].value, p.x - 1, p.y);
        result.push(p(p.position - 1, path[p.x - 1][p.y].value, p.x - 1, p.y));
        visited[p.position - 1] = true;
      }
      if (p.x + 1 <= 4 && !visited[p.position + 1] &&
          path[p.x + 1][p.y].value != 1) {
        que[rear--] = p(p.position + 1, path[p.x + 1][p.y].value, p.x + 1, p.y);
        result.push(p(p.position + 1, path[p.x + 1][p.y].value, p.x + 1, p.y));
        visited[p.position + 1] = true;
      }
      if (p.y - 1 >= 0 && !visited[p.position - 5] &&
          path[p.x][p.y - 1].value != 1) {
        que[rear--] = p(p.position - 5, path[p.x][p.y - 1].value, p.x, p.y - 1);
        result.push(p(p.position - 5, path[p.x][p.y - 1].value, p.x, p.y - 1));
        visited[p.position - 5] = true;
      }
      if (p.y + 1 <= 4 && !visited[p.position + 5] &&
          path[p.x][p.y + 1].value != 1) {
        que[rear--] = p(p.position + 5, path[p.x][p.y + 1].value, p.x, p.y + 1);
        result.push(p(p.position + 5, path[p.x][p.y + 1].value, p.x, p.y + 1));
        visited[p.position + 5] = true;
      }
    }
  }
  return 0;
}

因为这一句需要一个不带参数的构造函数,默认构造函数只有在缺省情况下才会合成,所以你这里要显式定义它

int main(int argc, char const *argv[]) {
  Point path[5][5];
        ^

Point结构体里加一个Point(){}

还有,不能这样调用构造函数。。

que[rear--] = p(p.position - 1, path[p.x - 1][p.y].value, p.x - 1, p.y);
              ^

正确的做法是先在类里定义一个拷贝构造函数(当然这里用它默认生成的也行),然后这一句改为

que[rear--] = Point(p.position - 1, path[p.x - 1][p.y].value, p.x - 1, p.y);

这题不难,思路理清楚,然后把数据结构优化一下就好了

【热门文章】
【热门文章】