如何在C++中将接送坐标转换为 2D 字符数组中的行和列?

How to convert pick-up and drop-off coordinates into rows & columns in a 2D character array in C++?

本文关键字:数组 字符 2D 转换 C++ 坐标      更新时间:2023-10-16

以前我问过一个关于如何将字符文本文件打开为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