微型鼠标.从文本文件读取迷宫数据,对其进行处理并写入输出文件(算法存在问题) [英] Micromouse. Read maze data from text file, manipulate it and write to output file (problems with algorithm)

查看:115
本文介绍了微型鼠标.从文本文件读取迷宫数据,对其进行处理并写入输出文件(算法存在问题)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

/*鉴于以下减少的微型鼠标迷宫(3x3 = 9个单元)存储在文本文件"testin.txt"中:

/* Given the following reduced micromouse maze (3x3=9 cells) stored in text file ''testin.txt'':

+-+-+-+
|6 7 8|
+-+-+ +
|3 4|5|
+ + + +
|0|1 2|
+-+-+-+



该程序将打开文件,然后使用getline一次将一行读入缓冲区.缓冲区中的元素移到镜像"数组中,该数组存储迷宫,以使cell_0位于左上角.这样做是为了简化将迷宫墙的图形表示转换成布尔表示到数组"MAP"的过程.因此,镜像"变为:



The program opens the file, then uses getline to read a line at a time into a buffer. The elements of the buffer are moved into an array, ''mirrored'', which stores the maze such that cell_0 is in the top left corner. This is done to simplify the process of translating the graphical representation to a Boolean representation of the maze''s walls into the array ''MAP''. Thus, ''mirrored'' becomes:

+-+-+-+
|0|1 2|
+ + + +
|3 4|5|
+-+-+ +
|6 7 8|
+-+-+-+



第一行``+-+-+-+''逐元素进行分析,忽略``+'',并根据是否存在''''或``-'',将``0'' ''或``1''被写入``MAP''. (-"和"|"表示墙壁",而''表示没有墙壁).

上面迷宫的布尔数组如下所示:

然后,程序将"MAP"数组的内容写入屏幕.



The first line ''+-+-+-+'' is analyzed element by element, ''+'' are ignored and depending on whether there is a '' '' or a ''-'', a ''0'' or ''1'' is written to ''MAP''. (''-'' and ''|'' denote ''walls'' and '' '' denotes the absence of a wall).

The bool array for the above maze would look like this:

The program then writes the content of the ''MAP'' array to the screen.

Map[cell][N,S,W,E bits]
-----------------------
0         0,1,1,1
1         0,1,1,0
2         0,1,0,1
3         1,0,1,0
4         1,0,0,1
5         0,0,1,1
6         1,1,1,0
7         1,1,0,0  
8         1,0,0,1
-----------------------



正确的屏幕输出应为:



The correct screen output should be:

0         1, 0,1,1
1         1, 0,1,0
2         1, 0,0,1
3         0, 1,1,0
4         0, 1,0,1
5         0, 0,1,1
6         1, 1,1,0
7         1, 1,0,0
8         0, 1,0,1



目前,我正在尝试至少使N位正确,但是必须存在一个我没有看到的错误.我相信我已经排除了局部变量或全局变量的问题,而最初我认为可能是引起该问题的原因...

来自知识渊博的人的任何帮助将不胜感激!
提前谢谢.
*/



For the time being, I am trying to at least get the N-bits right, but there must be a bug that I am not seeing. I believe I have excluded problems of local or global variables, which I initially thought might be causing it...

Any help from someone knowledgeable would be greatly appreciated!
Thanks in advance.
*/

#include <fstream.h>

int main()
{

	const int size=3;  // Example, a standard micromouse maze is 16x16 cells...

	const int rows=(2*size + 1);
	const int columns=rows;
	const int cells=size*size;
	const int bits=4;  // North, South, West, East

	char mazeFile[80]= "testin.txt";
//	char mazeCopy[80]= "testout.txt";

	char buffer[columns+2];  // 2 elements added for EndOfLine
	char mirrored[rows][columns];  // store contents of mazeFile such that cell_0 is at top left corner
	
	bool MAP[cells][bits];

	int m=0; int n=0;



//	Initialize all bits to 1
	for (n=0; n<cells; n++){
		for (m=0; m<bits; m++){
			MAP[n][m]=1;
		};
	};

//	Initialize all bits to 1
	for (n=0; n<rows; n++){
		for (m=0; m<columns; m++){
			mirrored[n][m]=1;
		};
	};



	ifstream fin(mazeFile);  // open for reading
//	ofstream fout(mazeCopy);  // open for writing



	int row=0; int col;  // don't confuse 'row' with 'rows' 

	while (row<rows)
	{
		fin.getline(buffer,columns+2);
//		fout << buffer << endl;
//		cout << buffer << endl;

		row++;

		for (col=0; col<columns; col++)
		{
			mirrored[rows-row][col] = buffer[col];
		} 	

	}


	fin.close();		
//	fout.close();  // close the file, ready for reopen


	for (row=0; row<rows; row++){
		for (col=0; col<columns; col++){
			cout << mirrored[row][col];
		}
		cout<<endl;
	}



//	int count=0;	
	row=0;
	int POS=0;  // current cell POSition
	int count=0;

	while (row<rows)  // For standard maze, rows= 16+16+1= 33
	{


		if (row%2==0)  // if row is EVEN (0,2,4,6,..) - update N (and S bits)
		{
			for (col=1; col<(columns-1); col++)
			{
				if (col%2!=0)  // if Odd col (1,3,5,..) - update N (and S bits)
				{
					if (mirrored[row][col] == '-') {MAP[POS][0]=1;} // (and MAP[POS-3][1]=0;)
						else {MAP[POS][0]=0;} // (and MAP[POS-3][1]=1;)
					POS++;
				}
			}
			POS=(POS-size); //count++;
		}

		
		else	// if row is ODD (1,3,5,..)- update W (and E bits)
		{
			for (col=0; col<columns; col++)
			{
				if (col%2==0)  //if Even col (0,2,4,6,..) - update W (and E bits)
				{
					if (mirrored[row][col] == '|') {MAP[POS][2]=1;} // (and MAP[POS-1][3]=0;) 
						else {MAP[POS][2]=0;} //(and MAP[POS-1][3]=0;)
					POS++;
				}
			}
			POS=(POS-size-1); //count++;
		}


		row++; count++;


//		cout << "\nRow: " << row;
//		cout << "\nCount: " << count;
//		cout << "\nPosition: " << POS << endl;

		if (count!=3) {
		} else {count=0; POS=POS+size;}


	}



//	output MAP data to screen

	for (m=0; m<cells; m++){
	
		cout << "cell_" << m << " ";
		for (n=0; n<bits; n++){
			cout << MAP[m][n];
		};
		cout << endl;
	}; 	
	
	cout << endl;






return 0;
}

推荐答案

感谢您的澄清.我认为您的问题是思维的惯性,这使您使用原始数组.同样,从一开始就不以一般形式解决问题(至少使迷宫具有任意尺寸的算法)会在以后给您带来麻烦.

您需要获取一些单元格的数组,每个单元格都是一个结构或类实例.数组的尺寸不应硬编码,而应根据数据计算.根据输入数据,将邻居信息放入每个单元格中.这将是多余的,那又如何呢?更重要的是,数据将以单元为中心且对称(所有单元都相等).表示成员的一种方式是表示方向的对象的列表(矢量或固定大小的数组,其中包含0..8个元素,有些未使用)–这样,您将仅列出具有开放路径的方向.用一对有符号字节来表示方向,比如说(-1,1)代表WN.迷宫描述将变得更接近算法.

之后,对鼠标进行建模,模拟移动历史,历史元素是指向同一单元格的指针.在此之上,对搜索算法建模.依此类推...

—SA
Thank you for clarification. I think your problem is some inertia of thinking which makes you using primitive arrays. Also, not solving the problem in general form from the very beginning (at least make the algorithm for arbitrary dimensions of the maze) will give you troubles later.

You need to get an array of some cells, each being a structure or class instance. The dimensions of an array should not be hard-coded but should be calculated from data. From input data, put neighbor information in each cell. It will be redundant, so what? More important, the data will be cell-centered and symmetric (all cells are equal). One way to represent members would be a list (vector or fixed-size array of 0..8 elements, some unused) of objects representing direction — this way, you would list only the directions with open path. Represent direction with the pair of signed bytes, say (-1, 1) representing WN. The maze description will become closer to the algorithm.

After that, model the mouse, history of moves, with elements of history being a pointer to the same cell. On top of it, model the algorithm of the search. And so on…

—SA


这篇关于微型鼠标.从文本文件读取迷宫数据,对其进行处理并写入输出文件(算法存在问题)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆