2013-10-09 169 views
0

任何人都可以帮助我吗?它给了我一个错误的号码。矩阵[i] [j] .spath填充了正确的值,但是当我返回任何两个节点之间的最短路径时,它会给我一个错误的数字。编译器给我这个函数返回错误的数字?

警告:控制到达非void函数

末但是if语句,其中i检查是否达到最终总是会执行这样return语句,因为我在main()中设置结束坐标。但我注意到当我添加返回1或返回任何在函数结束时,它会给出正确的结果。这是一种规则还是什么?我已经写了一个这样的函数,我有一个if语句和唯一的return语句,并且它没有问题。谢谢:)

#include <iostream> 
#include <queue> 

using namespace std; 

struct node 
{ 
    int x,y,spath,val; 
}v,c; 

node mat[100][100]; 
int dy[] = {-1,1,0,0}, dx[] = {0,0,-1,1}, n, m; 

void input() 
{ 
    cin >> n >> m; 
    for (int i=0; i<n; i++) { 
     for (int j=0; j<m; j++) { 
      cin >> mat[i][j].val; 
      mat[i][j].spath = 0; 
     } 
    } 
} 

int shortest_path(node start, node end) 
{ 
    queue<node> q; 
    q.push(start); 
    mat[start.y][start.x].val = 1; 

    while (!q.empty()) 
    { 
     v = q.front(); 
     q.pop(); 

     for (int i=0; i<4; i++) { 
      c.y = v.y + dy[i]; 
      c.x = v.x + dx[i]; 

      if (c.y == end.y && c.x == end.x) { 
       return mat[v.y][v.x].spath + 1; 
      } 
      else if (c.y >=0 && c.y < n && c.x >=0 && c.x < m && mat[c.y][c.x].val == 0) 
       { 
        mat[c.y][c.x].val = 1; 
        mat[c.y][c.x].spath = mat[v.y][v.x].spath + 1; 
        q.push(c); 
       } 
     } 
    } 
} 

int main() 
{ 
    node start,end; 
    start.x = start.y = 0; 
    end.y = end.x = 4; 
    input(); 
    cout << shortest_path(start,end) << endl; 


    return 0; 
} 
+1

你期望输出是什么?你觉得'shortest_path'会返回什么结果? –

+1

你应该在shortest_path(node start,node end)结束时返回一些有意义的东西:如果没有从开始到结束的路径,会发生什么(例如,cin为mat [] []。val提供了一个非零次对角线)?然后你用一个未定义的值从shortest_path中删除。 – Tobias

+0

你使用什么编译器/平台?似乎使用g ++/linux正常工作。 – LeGEC

回答

0

关于警告:

您的代码将无法防止错误的输入:如果end是你n x m电网之外,或者是在“墙”,或者如果没有从路startend您的功能退出而不执行返回语句。

编译器无法预测将输入到该函数的输入。

+0

我忘了提及它给这个输入提供了错误的输出:5 5 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0.所以,这个输入一切正常,每两个节点之间存在路径,并且存在坐标4,4。 – LearningMath

+0

你能为我计算0吗? – LeGEC

+0

它是5x5矩阵所以25个零:)编辑:我在Windows 7上使用gnu编译器 – LearningMath

0

正如你注意到的那样,问题在于它错过了一个return语句。你可能知道它总是会经过if语句的返回,但是编译器不会,因此是警告。 你认为输入是正确的,但你应该“永远不要相信用户输入”。永远不能。

对于所有执行可能在非void函数中执行的路由,始终应该有一个return语句。

+0

我忘了提及它给这个输入提供了错误的输出:5 5 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0。因此,这个输入一切都正常,每两个节点之间存在一条路径,并且存在坐标4,4。 – LearningMath

0

看来你正在写BFS.Here是我的代码:

#include"stdio.h" 
#include"string.h" 
#include"queue" 
using namespace std; 
#define N 200 
int n,m; 
int move[][2]={{0,1},{1,0},{0,-1},{-1,0}}; 
struct node 
{ 
    node(int _x=0,int _y=0) 
    { 
     x=_x,y=_y; 
    } 
    int x,y; //mark the Coord of the node 
}; 
int data[N][N];//store data. 
bool map[N][N]; //mark whether the node is visited. 
int input()//input & initialize the array 
{ 
    memset(map,false,sizeof(map)); 
    scanf("%d%d",&n,&m); 
    for(int i=0;i<n;i++) 
     for(int j=0;j<m;j++) 
    { 
     int t; 
     scanf("%d",&t); 
     data[i][j]=t; 
     map[i][j]=false; 
    } 
    return 0; 
} 
bool judge(node x) 
{ 
    if(x.x<n&&x.x>=0&&x.y<m&&x.y>=0) return true; 
    return false; 
} 
int shortest_path(node s,node e) 
{ 
    queue<int>dist;//means 'spath' in your code. 
    int dst=0; 
    queue<node>q; 
    q.push(s); 
    map[s.x][s.y]=true; 
    dist.push(0); 
    node v,c; 
    while(!q.empty()) 
    { 
     v=q.front(); 
     q.pop(); 
     dst=dist.front(); 
     dist.pop(); 
     for(int i=0;i<4;i++) 
     { 
      c.x=v.x+move[i][0]; 
      c.y=v.y+move[i][1]; 
      if(judge(c)&&!map[c.x][c.y]) 
      { 
       dist.push(dst+1); 
       q.push(c); 
       map[c.x][c.y]=true; 
       if(c.x==e.x&&c.y==e.y) 
        return dst+1; 
      } 
     } 
    } 
    return -1;//if the path not found return -1; 
}; 
int main() 
{ 
    input(); 
    node s(0,0),e(4,4); 
    printf("%d\n",shortest_path(s,e)); 
    return 0; 
} 

输入应该是: N> = 5 M> = 5,因为终点是(4,4)。 和n * m矩阵。