rt。
题目就是一道基本的广搜模版。
题意简述:
有一个 n×n 的地图,从 mpx1,y1 走到 mpx2,y2,只能上下左右走,每次移动一次需要 m 的时间。1≤n≤1000,1≤m≤10000000000。
#include <bits/stdc++.h>
#define ll long long
#define int unsigned ll
using namespace std;
struct Node {
int x, y; // 当前坐标
int step; // 当前走的步数
};
int n, m;
// int mp[1005][1005];
int qx, qy, zx, zy;
queue <Node> q;
bool vis[10005][10005];
signed main() {
// freopen("in.txt", "r", stdin);
// freopen("out.txt", "w", stdout);
cin >> n >> m;
for (int i = 1; i <= n; ++i)
for (int j = 1; j <= n; ++j) {
cin >> vis[i][j];
}
cin >> qx >> qy >> zx >> zy;
Node u;
u.x = qx;
u.y = qy;
u.step = 0;
q.push(u);
vis[u.x][u.y] = 1;
if (u.x == zx && u.y == zy) {
cout << 0;
return 0;
}
while (!q.empty()) {
Node v;
u = q.front();
q.pop();
v.x = u.x + 1;
v.y = u.y;
v.step = u.step + m;
if (v.x <= n && !vis[v.x][v.y]) {
vis[v.x][v.y] = 1;
q.push(v);
}
if (v.x == zx && v.y == zy) {
cout << v.step << endl;
return 0;
}
// cout << v.x << " "<< v.y << " " << v.step << endl;
//------------------1------------------------
v.x = u.x - 1;
v.y = u.y;
v.step = u.step + m;
if (v.x > n && !vis[v.x][v.y]) {
vis[v.x][v.y] = 1;
q.push(v);
}
if (v.x == zx && v.y == zy) {
cout << v.step << endl;
return 0;
}
// cout << v.x << " "<< v.y << " " << v.step << endl;
//------------------2------------------------
v.x = u.x;
v.y = u.y + 1;
v.step = u.step + m;
if (v.x <= n && !vis[v.x][v.y]) {
vis[v.x][v.y] = 1;
q.push(v);
}
if (v.x == zx && v.y == zy) {
cout << v.step << endl;
return 0;
}
// cout << v.x << " "<< v.y << " " << v.step << endl;
//------------------3---------------------------
v.x = u.x;
v.y = u.y - 1;
v.step = u.step + m;
if (v.y > 0 && !vis[v.x][v.y]) {
vis[v.x][v.y] = 1;
q.push(v);
}
if (v.x == zx && v.y == zy) {
cout << v.step << endl;
return 0;
}
// cout << v.x << " "<< v.y << " " << v.step << endl;
//-----------------4-----------------
}
}
/*
4 2
0 1 1 0
0 0 0 0
1 1 1 0
1 1 1 0
1 1 4 4
*/