Demo entry 6658580

数图作业

   

Submitted by 冶鹏豪 on Nov 07, 2017 at 15:14
Language: C++. Code size: 2.0 kB.

/*
  原图像转换为二维数组
  在原数组外加了一层0来解决边界问题
*/
int Map[9][12] = { 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,0,0,0,1,0,0,0,1,0,0,0,
		   0,0,0,0,1,0,0,1,1,0,0,0,
		   0,0,0,1,0,0,1,1,0,0,0,0,
		   0,0,0,1,0,0,1,0,0,0,0,0,
		   0,0,0,1,1,1,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,0,0,0,0,0};
/*
  坐标变换
*/
int dx[8] = { 1, -1,-1,  1 ,1,0,-1, 0, };
int dy[8] = { 1,  1,-1, -1 ,0,1, 0,-1, };
        /*
	 搜索,计数
	*/
	while (1)
	{
		p = que.front();
		Map[p.first][p.second] = 0;
		que.pop();
		if (p.first == gx && p.second == gy) break;
		for (int i = 0; i < 8; i++)
		{
			nx = p.first + dx[i];
			ny = p.second + dy[i];
			if (i == 0)
			{
				if (Map[p.first][p.second + 1] == 1 || Map[p.first + 1][p.second] == 1) { continue; }
				else if(Map[nx][ny]==1)
				{
					que.push(P(nx, ny));
					Map[nx][ny] = 0;
					Step[nx][ny] = Step[p.first][p.second] + 1;
				}
				else continue;
			}
			if (i == 1)
			{
				if (Map[p.first][p.second + 1] == 1 || Map[p.first - 1][p.second] == 1) { continue; }
				else if (Map[nx][ny] == 1)
				{
					que.push(P(nx, ny));
					Map[nx][ny] = 0;
					Step[nx][ny] = Step[p.first][p.second] + 1;
				}
				else continue;
			}
			if (i == 2)
			{
				if (Map[p.first - 1][p.second] == 1 || Map[p.first][p.second - 1] == 1) { continue; }
				else if (Map[nx][ny] == 1)
				{
					que.push(P(nx, ny));
					Map[nx][ny] = 0;
					Step[nx][ny] = Step[p.first][p.second] + 1;
				}
				else continue;
			}
			if (i == 3)
			{
				if (Map[p.first + 1][p.second] == 1 || Map[p.first][p.second - 1] == 1) { continue; }
				else if (Map[nx][ny] == 1)
				{
					que.push(P(nx, ny));
					Map[nx][ny] = 0;
					Step[nx][ny] = Step[p.first][p.second] + 1;
				}
				else continue;
			}
			if (Map[nx][ny] == 1)
			{
				que.push(P(nx, ny));
				Map[nx][ny] = 0;
				Step[nx][ny] = Step[p.first][p.second] + 1;
			}

		}
	}

This snippet took 0.01 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).