如何在C++中将接送坐标转换为 2D 字符数组中的行和列?
How to convert pick-up and drop-off coordinates into rows & columns in a 2D character array in C++?
以前我问过一个关于如何将字符文本文件打开为2D字符数组的问题。这是一张地图,机器人被分配去取包裹。让他们在另一个有道路、建筑物的坐标点下车;一条需要花费更多时间来移动的中心道路。我能看地图[15][15]&;存储成二维字符数组。但是现在,假设机器人的起始点是0。48,我如何将该数字转换为第4行,第3列的二维字符数组映射?我如何从这一步继续?没有。可能是随机的但我只是想知道如何让字符数组知道哪一行&那一列是不。在。
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
ifstream in;
char s;
char map[15][15];
int i,j, r = 0, c = 0;
in.open("city.txt");
if(!in.is_open())
{
cout << "File open error! " << endl;
}
else
{
while(!in.eof())
{
in >> s;
if(in.eof()) break;
map[r][c] = s;
c++;
if(c > 14)
{
r++;
c = 0;
}
}
in.close();
for(i = 0; i < 15; i++)
{
for(j = 0; j < 15; j++)
cout << map[i][j];
cout << endl;
}
}
return 0;
}
int row =pos/15;
int column=pos%15
其中pos
为bot的位置
取N=4的NxN网格。您的号码如下:
00, 01, 02, 03,
04, 05, 06, 07,
08, 09, 10, 11,
12, 13, 14, 15,
您可以使用整数除法(/)来检索行:
2/N = row 0
10/N = row 2
15/N = row 3
您可以使用模数(%)来检索列:
2%N = column 2
10%N = column 2
15%N = column 3
在你的例子中,你使用N=15
相关文章:
- 指向指向字符数组的指针数组的指针
- 比较字符数组
- 如何使用Crypto++并为RSA返回可打印的字节/字符数组
- 使用无符号字符数组有效存储内存
- 按字符值访问int数组
- 错误:字符数组的初始值设定项太多
- 对字符数组中的元素执行逐位操作
- C++,在int数组中输入字符串或字符会输出0,而不是ascii或error
- C++ 无法在字符数组中使用 for 循环打印字母模式
- 如何在 C++ 中从文件中读取字符数组(带有一些空格)
- 移动二维数组中的字符
- C++ 传递二维字符数组
- 无法在 C++ 中输入字符数组
- C++ 带结构的数组字符
- 数组字符包含量超出预期
- 读取文件并添加到数组字符
- 如何在Qt中更改或替换数组字符
- 为什么编译器不在参数中传递数组字符 *arr[] 的大小?
- 井字棋数组字符错误
- 分割数组字符