神秘bfs+拆点,比赛时WA了9发,到后面AC * 65,WA * 2(世界上最绝望的亖法
赛后换了个写法就过了!过了?
#include <bits/stdc++.h>
using namespace std;
#define bug cout << "________________\n"
#define bug2 cout << "***\n"
#define Yes cout << "Yes\n"
#define YES cout << "YES\n"
#define No cout << "No\n"
#define NO cout << "NO\n"
#define fi first
#define se second
#define IX INT_MAX
#define IN INT_MIN
#define LX LLONG_MAX
#define LN LLONG_MIN
#define debug(n) for (int i = 1;i <= n;i++) printf ("%d %d\n" , i , a[i]);
#define add(u , v) edges[u].push_back(v);
#define add2(u , v , w) edges[u].push_back(Edge(v , w));
typedef long long ll;
typedef unsigned long long ull;
typedef __int128 i128;
typedef unsigned __int128 u128;
typedef double db;
typedef double ldb;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;
typedef pair <int , pii> piii;
typedef pair <ll , pll> plll;
typedef pair <pii , pii> piiii;
typedef pair <pll , pll> pllll;
const ll mod = 998244353;
mt19937 rng((int)time(0));
mt19937_64 rng_64((ull)(time(0)));
const int N = 510;
int H , W , sx , sy , ex , ey;
ll dist[N][N][2];
int D[4][2] = {{-1 , 0} , {1 , 0} , {0 , -1} , {0 , 1}};
char s[N][N];
queue <piii> q;
inline void bfs() {
memset(dist , 127 , sizeof(dist));
q.push({sx , {sy , 0}});
dist[sx][sy][0] = 0;
while (!q.empty()) {
int x = q.front().fi;
int y = q.front().se.fi;
int type = q.front().se.se;
q.pop();
for (int i = 0;i < 4;i++) {
int xx = x + D[i][0];
int yy = y + D[i][1];
if (xx < 1||xx > H||yy < 1||yy > W||s[xx][yy] == '#') continue;
if (s[xx][yy] == 'G'&&dist[xx][yy][type] > dist[x][y][type] + 1) {
dist[xx][yy][type] = dist[x][y][type] + 1;
printf ("%d\n" , dist[xx][yy][type]);
return;
}
if ((s[xx][yy] == '.'||s[xx][yy] == 'S')&&dist[xx][yy][type] > dist[x][y][type] + 1) {
dist[xx][yy][type] = dist[x][y][type] + 1;
q.push({xx , {yy , type}});
continue;
}
if (s[xx][yy] == '?'&&dist[xx][yy][type ^ 1] > dist[x][y][type] + 1) {
dist[xx][yy][type ^ 1] = dist[x][y][type] + 1;
q.push({xx , {yy , type ^ 1}});
continue;
}
if (((s[xx][yy] == 'o'&&type == 0)||(s[xx][yy] == 'x'&&type == 1))&&dist[xx][yy][type] > dist[x][y][type] + 1) {
dist[xx][yy][type] = dist[x][y][type] + 1;
q.push({xx , {yy , type}});
}
}
}
printf ("-1\n");
}
int main () {
scanf ("%d%d" , &H , &W);
for (int i = 1;i <= H;i++) scanf ("%s" , s[i] + 1);
for (int i = 1;i <= H;i++)
for (int j = 1;j <= W;j++) {
if (s[i][j] == 'S') sx = i , sy = j;
if (s[i][j] == 'G') ex = i , ey = j;
}
bfs();
}