#56-[bfs]B

Description

清早6:00,Farmer John就离开了他的屋子,开始了他的例行工作:为贝茜挤奶。前一天晚上,整个农场刚经受过一场瓢泼大雨的洗礼,于是不难想见,FJ 现在面对的是一大片泥泞的土地。FJ的屋子在平面坐标(0, 0)的位置,贝茜所在的牛棚则位于坐标(X,Y) (-500 <= X <= 500; -500 <= Y <= 500)处。当然咯, FJ也看到了地上的所有N(1 <= N <= 10,000)个泥塘,第i个泥塘的坐标为 (A_i, B_i) (-500 <= A_i <= 500;-500 <= B_i <= 500)。每个泥塘都只占据了它所在的那个格子。 Farmer John自然不愿意弄脏他新买的靴子,但他同时想尽快到达贝茜所在的位置。为了数那些讨厌的泥塘,他已经耽搁了一些时间了。如果Farmer John 只能平行于坐标轴移动,并且只在x、y均为整数的坐标处转弯,那么他从屋子门口出发,最少要走多少路才能到贝茜所在的牛棚呢?你可以认为从FJ的屋子到牛棚总是存在至少一条不经过任何泥塘的路径。

Input

* 第1行: 3个用空格隔开的整数:X,Y 和 N

* 第2..N+1行: 第i+1行为2个用空格隔开的整数:A_i 和 B_i

Output

* 第1行: 输出1个整数,即FJ在不踏进泥塘的情况下,到达贝茜所在牛棚所需要 走过的最小距离

Sample Input

1 2 7
0 2
-1 3
3 1
1 1
4 2
-1 1
2 2
输入说明:
贝茜所在牛棚的坐标为(1, 2)。Farmer John能看到7个泥塘,它们的坐标分
别为(0, 2)、(-1, 3)、(3, 1)、(1, 1)、(4, 2)、(-1, 1)以及(2, 2)。
以下为农场的简图:(*为FJ的屋子,B为贝茜呆的牛棚)
4 . . . . . . . .
3 . M . . . . . .
Y 2 . . M B M . M .
1 . M . M . M . .
0 . . * . . . . .
-1 . . . . . . . .
-2-1 0 1 2 3 4 5
X

Sample Output

11

HINT

 

约翰的最佳路线是:(0,0),(一1,0),(一2,0),(一2,1),(一2,2),(一2,3),(一2,4),(一1,4),(0,4),  (0,3),  (1,3),  (1,2).

注意每个都要加503......不然数组越界,铼(Re)了。

#include <iostream>
#include <queue>
#include <cstring>

#define SIZE 1011

using namespace std;

struct node
{
	int x, y, dis;
};

bool a[SIZE][SIZE];
queue<node> q;
int dx[4] = {1, -1, 0, 0};
int dy[4] = {0, 0, 1, -1};

int main(void)
{
	int i, ex, ey, x, y, d, r, c, temp = SIZE - 1;
	
	memset(a, true, sizeof (a));
	
	scanf("%d%d%d", &ex, &ey, &i);
	if ((!ex) && (!ey))
	{
		printf("0");
		return 0;
	}
	ex += 503;
	ey += 503; // ey?如果变成3维的话...... 蛾子
	while (i--)
	{
		scanf("%d%d", &x, &y);
		a[x+503][y+503] = false; // 不加上503就铼了
	}
	
	for (i = 0; i < SIZE; ++i)
	{
		a[0][i] = a[i][0] = a[temp][i] = a[i][temp] = false;
	}
	q.push({503, 503, 0});
	while (!q.empty()) // 基本广搜
	{
		x = q.front().x;
		y = q.front().y;
		d = q.front().dis + 1;
		for (i = 0; i < 4; ++i)
		{
			r = x + dx[i];
			c = y + dy[i];
			if ((r == ex) && (c == ey))
			{
				printf("%d", d);
				return 0;
			}
			if (a[r][c])
			{
				a[r][c] = false;
				q.push({r, c, d});
			}
		}
		q.pop();
	}
	
	return 0;
}

 

import matplotlib.pyplot as plt from matplotlib.patches import Rectangle # 初始化参数 total_rows = 56 total_cols = 15 # 通道行号(从下往上数1-based) channel_rows = {1, 12, 23, 34, 45, 56} # 通道列号(0-based) channel_cols = {1, 4, 7, 10, 13} # 列宽配置(0-based索引) column_widths = [6 if col in channel_cols else 2.5 for col in range(total_cols)] # 行高配置(0-based索引对应从下往上的行号1-56) row_heights = [2.5 if (row + 1) in channel_rows else 1 for row in range(total_rows)] # 计算累积位置 col_positions = [0] for w in column_widths: col_positions.append(col_positions[-1] + w) row_positions = [0] for h in row_heights: row_positions.append(row_positions[-1] + h) # 创建画布 fig, ax = plt.subplots(figsize=(20, 20)) ax.set_xlim(0, sum(column_widths)) ax.set_ylim(0, sum(row_heights)) ax.set_axis_off() # 绘制每个单元格 for row in range(total_rows): current_row_number = row + 1 # 转换为从下往上的1-based行号 is_channel_row = current_row_number in channel_rows for col in range(total_cols): x = col_positions[col] y = row_positions[row] width = column_widths[col] height = row_heights[row] # 确定单元格类型 if is_channel_row: color = &#39;lightblue&#39; else: color = &#39;lightblue&#39; if col in channel_cols else &#39;lightgray&#39; # 绘制矩形 rect = Rectangle( (x, y), width, height, edgecolor=&#39;black&#39;, facecolor=color, linewidth=0.5 ) ax.add_patch(rect) 设通道格子可以通行,左下角为入口,车位格随机确定是否被占用,找到距离入口最近的空置车位格并绘制到达其的最短路线
最新发布
03-14
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值