因该最短路总结
P1735 字母迷宫
一看简单题,一做两行泪。显然,标准的
1.时空之门,显然与以前写的迷宫题目中的
2.海洋之门,首先,我们要明白
这样的好处就是,每次跑到一个点所有的时间一定是所有时间中最小的。因为,每个人都是一个一个时间的走,如果有更快的办法早就有人到了。所以,我们只要把这个们看作一个操作即可。
3.天堂之门,我们上述中说过,我们是按一定的时间跑的,但是这个要花费的时间却是
因为假设有一个点只要一步就到了,现在这个天堂之门聚气后也可以到。然后,我们又先找到了天堂之门,这不就毁了吗?
我们就会认为要多一步走到一个位置。那么,为了防止这种情况的出现,我们决定加一个标记,如果还没有聚气我们就停留一步。这样,我们就拆分成了两个相同时间的操作。
几个值得注意的点:首先如果开头或起点是
还有本题要求的是长度,所以别忘了最后加个
code
#include<bits/stdc++.h>
#define int long long
using namespace std;
int n;
bool vis[1205][1205];
char a[1205][1205];
struct pi{
int x,y,s;
bool fr;
};
int dx[] = {0 , 0 , -1 , 1 , 0 , 0 , -2 , 2 , -1 , -1 , 1 , 1};
int dy[] = {-1 , 1 , 0 , 0 , -2 , 2 , 0 , 0 , 1 , -1 , 1 , -1};
bool check(int x,int y){
if(x >= 1 && x <= n && y >= 1 && y <= n && !vis[x][y] && a[x][y] != '*'){
return true;
}
return false;
}
int bfs(){
queue<pi>q;
if(a[1][n] != '*') q.push({1 , n , 0 , 0});
if(a[1][1] != '*') q.push({1 , 1 , 0 , 0});
if(a[n][1] != '*') q.push({n , 1 , 0 , 0});
vis[1][n] = vis[1][1] = vis[n][1] = 0;
while(!q.empty()){
pi u = q.front();
q.pop();
int lst = 0,d = 1;
if(a[u.x][u.y] == 'B'){
lst = 4;
}
if(a[u.x][u.y] == 'C'){
lst = 8;
}
// cout << u.x << " " << u.y << "\n";
for(int i = 0 ; i < 4 ; i ++){
int sx = u.x + dx[lst + i];
int sy = u.y + dy[lst + i];
if(check(sx , sy)){
if(lst == 8){
if(u.fr == 0){
q.push({u.x , u.y , u.s + 1 , 1});
continue;
}
q.push({sx , sy , u.s + 1 , 0});
vis[sx][sy] = 1;
if(sx == n && sy == n){
// cout << sx << " " << sy << "\n";
return u.s + 1;
}
continue;
}
vis[sx][sy] = 1;
q.push({sx , sy , u.s + 1 , 0});
if(sx == n && sy == n){
// cout << u.x << " " << u.y << " " << sx << " " << sy << "\n";
return u.s + 1;
}
}
}
}
return -1;
}
signed main(){
ios::sync_with_stdio(0);
cin.tie(0),cout.tie(0);
cin >> n;
for(int i = 1 ; i <= n ; i ++){
for(int j = 1 ; j <= n ; j ++){
cin >> a[i][j];
}
}
int x = bfs();
if(x == -1){
cout << "No answer";
}else{
cout << x + 1;
}
return 0;
}
Rescue
首先,我们可以发现,本题的代价不一致。那么,怎么办呢?我们不是要代价一致才行吗?其实,我们可以用优先队列解决。
但是,用优先队列又很多限制。比如,我们从一个点出发后又两条路劲,这两条路径都是经过了一个点到达终点。前一步中,中间的代价一个大于另一个。
但是,后一半原本代价更大的却更小了。可是,按照原本的方法我们到达终点后就直接认定这是最优解不会考虑其他情况了。也就是说,我们的标记将另一种情况删去了。
那么,怎么解决呢?简单我们在出队的时候再标记就可以了。然后我们就发现,这不就是
那么,我们想象为什么这样可以。我们知道再出队之前,我们将所有路劲都比较了遍,那么,我们用最小的路径更新路径也会更新出最小的路径。
那么,我们的优先队列
code
#include<bits/stdc++.h>
#define int long long
using namespace std;
int n,m,sx,sy,tx,ty;
bool vis[1205][1205];
int dis[1205][1205];
char a[1205][1205];
int dx[] = {0 , 0 , -1 , 1};
int dy[] = {-1 , 1 , 0 , 0};
bool check(int x,int y){
if(x >= 1 && x <= n && y >= 1 && y <= m && a[x][y] != '#'){
return true;
}
return false;
}
struct pi{
int x,y,dis;
bool operator < (const pi &rhs) const {
return dis > rhs.dis;
}
};
void bfs(){
memset(dis , 0x3f , sizeof(dis));
priority_queue<pi>q;
dis[sx][sy] = 0;
q.push({sx , sy , 0});
while(!q.empty()){
pi u = q.top();
q.pop();
if(u.x == tx && u.y == ty){
cout << u.dis << "\n";
return;
}
for(int i = 0 ; i < 4 ; i ++){
int nx = sx + dx[i];
int ny = sy + dy[i];
if(check(nx , ny)){
if(a[nx][ny] == 'x' && dis[nx][ny] > dis[u.x][u.y] + 2){
dis[nx][ny] = dis[u.x][u.y] + 2;
q.push({nx , ny , u.dis + 2});
}
if((a[nx][ny] == '.' || a[nx][ny] == 'a') && dis[nx][ny] > dis[u.x][u.y] + 1){
dis[nx][ny] = dis[u.x][u.y] + 1;
q.push({nx , ny , u.dis + 1});
}
}
}
}
cout << "Poor ANGEL has to stay in the prison all his life.\n";
return;
}
signed main(){
ios::sync_with_stdio(0);
cin.tie(0),cout.tie(0);
while(cin >> n >> m){
for(int i = 1 ; i <= n ; i ++){
for(int j = 1 ; j <= m ; j ++){
cin >> a[i][j];
}
}
for(int i = 1 ; i <= n ; i ++){
for(int j = 1 ; j <= m ; j ++){
if(a[i][j] == 'a'){
tx = i,ty = j;
}
if(a[i][j] == 'r'){
sx = i,sy = j;
}
}
}
bfs();
}
return 0;
}