关于队列和引用
查看原帖
关于队列和引用
1103295
butterbutterfly楼主2024/8/24 23:38
#include <iostream>
#include<cstring>
#include <cmath>
#include <utility>
#include <queue>
int n,m;
short start_x,start_y;
short ** map;
int max_step;
int incre_x[8]={-2,-1,1,2,2,1,-1,-2};
int incre_y[8]={-1,-2,-2,-1,1,2,2,1};

int main (void)
{
	std::cin>>n>>m>>start_x>>start_y;
	map=new short * [n];
	for(int i=0;i<n;i++)
	{
		map[i]=new short[m]();
		memset(map[i],-1,sizeof(short)*m);
	}
	std::queue<std::pair<short,short> > arr;	
	arr.push(std::make_pair(start_x-1,start_y-1));
	map[start_x-1][start_y-1]=0;	
	std::pair<short,short>& p=arr.front();	
	while(!arr.empty())
	{
		p=arr.front();
		
		int curr_level=map[p.first][p.second];		
		for( short j=0;j<8;j++)
		{
			if(p.first+incre_x[j]>=0&&p.first+incre_x[j]<n&&p.second+incre_y[j]>=0&&p.second+incre_y[j]<m&&map[p.first+incre_x[j]][p.second+incre_y[j]]==-1)
			{
				arr.push(std::make_pair(p.first+incre_x[j],p.second+incre_y[j]));
				map[p.first+incre_x[j]][p.second+incre_y[j]]=curr_level+1;
			}
		}
		arr.pop();	
	}
	for(int i=0;i<n;i++)
	{
		for(int j=0;j<m;j++)
			std::cout<<map[i][j]<<' ';
		std::cout<<std::endl; 
	}
	return 0;
 } 

为什么我将p的类型设置为pair的引用会导致超出内存空间

2024/8/24 23:38
加载中...