[洛谷]P1746 离开中山路

读地图的时候没有空格  无语啦

#include <bits/stdc++.h>
using namespace std;
int n;
int a[1001][1001];

//3
//001
//101
//100
//1 1 3 3
int b[1001][1001] = {0};
struct xy {
	int x, y;
};
int dx[] = {1, -1, 0, 0};
int dy[] = {0, 0, 1, -1};
queue <xy> q;

int main() {
	cin >> n;
	for (int i = 1; i <= n; i++) {
		for (int j = 1; j <= n; j++) {
			scanf("%1d", &a[i][j]);
		}
	}
	int x1, y1, x2, y2;
	cin >> x1 >> y1 >> x2 >> y2;
	q.push({x1, y1});

	while (!q.empty()) {
		xy u = q.front();
		if (u.x == x2 && u.y == y2)
			break;
		q.pop();
		for (int i = 0; i < 4; i++) {
			int xx = u.x + dx[i];
			int yy = u.y + dy[i];
			if (xx >= 1 && xx <= n && yy >= 1 && yy <= n && a[xx][yy] == 0 && b[xx][yy] == 0) {
				q.push({xx, yy});
				b[xx][yy] = b[u.x][u.y] + 1;
			}
		}


	}
	cout << b[x2][y2];

	return 0;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
P1807 最长路是一道洛谷的题目,要求计算一个有向无环图中1到n之间的最长路径。关于这个问题,可以使用广搜算法来依次搜索图中的结点,并记录当前结点的最长路并不断更新。具体的实现可以使用邻接矩阵来存储边权,并注意可能存在重边的情况需要取最大值。此外,为了避免超时,可以在入队时加上一个判断语句,只有当到达该点的最长路需要更新时才进行入队操作。以下是一种可能的解法: ```cpp #include <bits/stdc++.h> #define endl '\n' #define inf 0x3f3f3f3f using namespace std; typedef long long ll; const ll mo=80112002; int n, m, u, v, ww, w = max(w[u][v], ww); } q.push(1); memset(a, -1, sizeof(a)); a = 0; while (!q.empty()) { int x = q.front(); q.pop(); for (int i=1; i<=n; i++) { if (w[x][i && a[i < a[x + w[x][i]) { a[i = a[x + w[x][i]; q.push(i); } } } cout << a[n]; return 0; } ``` 以上是一种可能的代码实现,使用了广搜算法来计算最长路径。该算法使用邻接矩阵存储边权,通过不断更新当前结点的最长路来得到1到n之间的最长路径。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [洛谷P1807 最长路(BFS)](https://blog.csdn.net/m0_60543948/article/details/126431087)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* *3* [图论——最长路(洛谷 P1807)](https://blog.csdn.net/weixin_44572229/article/details/120743825)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值