sam-route(C++)

待施工

#include <iostream>
using namespace std;

//车的状态
struct State {
	int x;
	int y;
	int step;
};

//建筑物属性
struct Building {
    //左上角的位置
	int x;
	int y;
	//宽高
	int width;
	int height;
	//停车场位置
	int px;
	int py;
};

int dir[4][2] = {
	0,1,
	0,-1,
	1,0,
	-1,0
};

int visited[50][50] = { 0 };
int map[50][50] = { 0 };

//建筑的停车场位置
pair<int, int> buildings[10];
int buildID = 0;//当前建筑的ID

//增加建筑:建筑所在位置设为2,并将停车场位置记录下来
void addBuilding(Building b) {
	for (int i = b.x; i < b.x + b.width; i++) {
		for (int j = b.y; j < b.y + b.height; j++) {
			map[i][j] = 2;
		}
	}
	map[b.px][b.py] = 1;
	buildings[buildID++] = make_pair(b.px, b.py);
}

//获取两建筑间行驶距离
int getDistance(pair<int, int> p1, pair<int, int> p2) {
	State queue[50] = { 0 };
	int head = 0;
	int tail = 0;

	visited[p1.first][p1.second] = 1;
	State init = { p1.first,p1.second,0 };
	queue[tail] = init;
	tail = (tail + 1) % 50;
	while (tail != head) {
		State cur = queue[head];
		head = (head + 1) % 50;

		cout << cur.x << " " << cur.y << " " << cur.step << endl;

		if (cur.x == p2.first && cur.y == p2.second) {
			return cur.step;
		}

		int x, y;
		int step = cur.step + 1;

        //走右边:下边有房子或右下角有房子
		if (map[cur.x + 1][cur.y] == 2 || map[cur.x + 1][cur.y + 1] == 2) {
			x = cur.x;
			y = cur.y + 1;

			if (visited[x][y] == 1) continue;
			visited[x][y] = 1;//这里有问题
			if (map[x][y] != -1 && map[x][y] != 2) {
				State next = { x,y,step };
				queue[tail] = next;
				tail = (tail + 1) % 50;
			}
		}
		//走左边
		if (map[cur.x - 1][cur.y] == 2 || map[cur.x - 1][cur.y - 1] == 2) {
			x = cur.x;
			y = cur.y - 1;
			if (visited[x][y] == 1) continue;
			visited[x][y] = 1;
			if (map[x][y] != -1 && map[x][y] != 2) {
				State next = { x,y,step };
				queue[tail] = next;
				tail = (tail + 1) % 50;
			}
		}
		//走下边
		if (map[cur.x][cur.y - 1] == 2 || map[cur.x + 1][cur.y - 1] == 2) {
			x = cur.x + 1;
			y = cur.y;
			if (visited[x][y] == 1) continue;
			visited[x][y] = 1;
			if (map[x][y] != -1 && map[x][y] != 2) {
				State next = { x,y,step };
				queue[tail] = next;
				tail = (tail + 1) % 50;
			}
		}
		//走上边
		if (map[cur.x][cur.y + 1] == 2 || map[cur.x - 1][cur.y + 1] == 2) {
			x = cur.x - 1;
			y = cur.y;
			if (visited[x][y] == 1) continue;
			visited[x][y] = 1;
			if (map[x][y] != -1 && map[x][y] != 2) {
				State next = { x,y,step };
				queue[tail] = next;
				tail = (tail + 1) % 50;
			}
		}

        //如果在十字路口
		if (map[cur.x + 1][cur.y] != 2 && map[cur.x - 1][cur.y] != 2 && map[cur.x][cur.y + 1] != 2 && map[cur.x][cur.y - 1] != 2)
			for (int i = 0; i < 4; i++) {
				x = cur.x + dir[i][0];
				y = cur.y + dir[i][1];

				if (map[x][y] == -1) continue;
				if (visited[x][y] == 1) continue;

				visited[x][y] = 1;
				State next = { x,y,step };
				queue[tail] = next;
				tail = (tail + 1) % 50;
			}
	}
	return -1;
}

int main() {
	for (int i = 0; i < 50; i++) {
		map[0][i] = -1;
		map[49][i] = -1;
	}
	for (int i = 0; i < 50; i++) {
		map[i][0] = -1;
		map[i][49] = -1;
	}

	while (1) {
		cout << "1:添加建筑" << endl;
		cout << "2:求最短路径" << endl;
		char n;
		cin >> n;
		switch (n)
		{
		case '1':
			Building b;
			cin >> b.x >> b.y >> b.width >> b.height >> b.px >> b.py;
			addBuilding(b);
			break;
		case '2':
			int b1index, b2index;
			cin >> b1index >> b2index;
			cout << getDistance(buildings[b1index], buildings[b2index]);
			break;
		default:
			break;
		}
	}
}
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

河时有雨

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值