10.14号出现数组传值的错误
error: creating array of references( declaration of 'a' as array)
void func(int& a[], int n)
初衷:通过建立引用型形参使得func能够修改数组a的元素的值。
错因:引用型形参实际上是取实参的地址,从而获得修改实参的能力。而这里给函数传递的是数组a的首地址,地址是无法再取地址的。实际上,把a的首地址传给函数后,函数已经获得修改数组a元素的能力。
解决方法:把函数func的参数列表中的“int& a[]”改为“int a[]”即可。
原文链接:https://blog.csdn.net/cs_zlg/article/details/8332622
代码段
#include <iostream>
#include <queue>
using namespace std;
const int MAX_A = 100;
const int MAX_B = 100;
const int INF = 1000000;
typedef pair<int, int> P;
int dx[4] = { 1,0,-1,0 }, dy[4] = { 0,1,0,-1 };
void bfs(int xs,int ys,int xe,int ye,int A,int B,int d[][MAX_B],char maze[][MAX_B + 1])
{
int sx=xs;
int sy=ys;
int gx=xe;
int gy=ye;
int M=B;
int N=A;
queue<P> que;
for (int i = 0; i < N; i++)
for (int j = 0; j < M; j++)
d[i][j] = INF;
que.push(P(sx, sy));
d[sx][sy] = 0; //从起点出发将距离设为0,并放入队列首端
while (que.size())
{
P p = que.front(); que.pop();//弹出队首元素
int i;
for (i = 0; i < 4; i++)
{
int nx = p.first + dx[i];
int ny = p.second + dy[i];//移动后的坐标
//判断可移动且没到过
if (0 <= nx&&nx < N
&& 0 <= ny&&ny < M
&&maze[nx][ny] != '#'
&&d[nx][ny] == INF)//之前到过的话不用考虑,因为距离在队列中递增,肯定不会获得更好的解
{
que.push(P(nx, ny)); //可以移动则设定距离为之前加一,放入队列
d[nx][ny] = d[p.first][p.second] + 1;
if(nx==gx && ny==gy) break;
}
}
if(i!=4) break;//跳for循环且不等于4,说明找到了终点。如果等于4,就接着执行whlie循环,将队首元素出队
}
}
int main()
{
int n;
cin>>n;
while(n--)
{
char maze[MAX_A][MAX_B + 1];
int d[MAX_A][MAX_B];//储存起点到某一点的距离
int A, B;
int xs, ys,C; //起点的位置
int xe, ye; //终点的位置
cin>>A>>B;
cin>>C;
while(C--)
{
int x,y;
cin>>x>>y;
maze[x][y]='#';
}
cin>>xs>>ys>>xe>>ye;
maze[xs][ys]='S';
maze[xe][ye]='G';
bfs(xs,ys,xe,ye,A,B,d,maze);
if (d[xe][ye] < INF)//判断是否找到终点
{
cout << d[xe][ye] << endl;
}
else
{
cout << "Not arrive\n";
}
}
return 0;
}
void bfs(int xs,int ys,int xe,int ye,int A,int B,int d[][MAX_A],char maze[][MAX_B + 1]);