【luogu P1825 [USACO11OPEN]玉米田迷宮Corn Maze】 題解
阿新 • • 發佈:2018-09-13
ios names cst https include 題解 d+ urn show
題目鏈接:https://www.luogu.org/problemnew/show/P1825
帶有傳送門的迷宮問題
#include <cstdio> #include <cstring> #include <iostream> #include <algorithm> using namespace std; const int maxn = 2001; char map[maxn][maxn]; int fx[4] = {0, 1, 0, -1}; int fy[4] = {1, 0, -1, 0}; int n, m, ex, ey, sx, sy; struct map{ int x, y, t; }q[4000001]; struct door{ int x1, y1, x2, y2; }d[40]; void bfs() { int head = 1, tail = 2; q[head].x = sx, q[head].y = sy, q[head].t = 0; while(head != tail) { for(int i = 0; i < 4; i++) { int nowx = q[head].x + fx[i]; int nowy = q[head].y + fy[i]; if(nowx == ex && nowy == ey) { printf("%d", q[head].t + 1); return ; } if(nowx > n || nowx <= 0 || nowy > m || nowy <= 0 || map[nowx][nowy] == '#') continue; if(map[nowx][nowy] >= 'A' && map[nowx][nowy] <= 'Z') { if(nowx == d[map[nowx][nowy]-'A'].x1 && nowy == d[map[nowx][nowy]-'A'].y1) { q[tail].x = d[map[nowx][nowy]-'A'].x2; q[tail].y = d[map[nowx][nowy]-'A'].y2; q[tail].t = q[head].t + 1; tail++; } if(nowx == d[map[nowx][nowy]-'A'].x2 && nowy == d[map[nowx][nowy]-'A'].y2) { q[tail].x = d[map[nowx][nowy]-'A'].x1; q[tail].y = d[map[nowx][nowy]-'A'].y1; q[tail].t = q[head].t + 1; tail++; } } else { map[nowx][nowy] = '#'; q[tail].x = nowx; q[tail].y = nowy; q[tail].t = q[head].t + 1; tail++; } } head++; } } int main() { cin>>n>>m; for(int i = 1; i <= n; i++) for(int j = 1; j <= m; j++) { cin>>map[i][j]; if(map[i][j] == '@') sx = i, sy = j; if(map[i][j] == '=') ex = i, ey = j; if(map[i][j] <= 'Z' && map[i][j] >= 'A') { if(d[map[i][j]-'A'].x1 == 0 && d[map[i][j]-'A'].y1 == 0) {d[map[i][j]-'A'].x1 = i, d[map[i][j]-'A'].y1 = j; continue;} if(d[map[i][j]-'A'].x2 == 0 && d[map[i][j]-'A'].y2 == 0) {d[map[i][j]-'A'].x2 = i, d[map[i][j]-'A'].y2 = j; continue;} } } bfs(); return 0; }
【luogu P1825 [USACO11OPEN]玉米田迷宮Corn Maze】 題解