洛谷 P1825 【[USACO11OPEN]玉米田迷宫Corn Maze】
2024-09-05 17:06:53
P1825
简单的题意
就是一个有传送门的迷宫问题(我一开始以为是只有1个传送门,然后我就凉了).
大体思路
先把传送门先存起来,然后跑一下\(BFS\)。
然后,就做完了.
代码鸭
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <iomanip>
#include <vector>
#define A 305
using namespace std;
int ans, n, m, map[A][A], lu[400010][4];
int u[5] = {0, 0, 0, -1, 1}, v[5] = {0, -1, 1, 0, 0};
int dx, dy, sx, sy;
struct node {
int x, y;
bool s;
} w1[27], w2[27];
void bfs() {
int head = 0, tail = 1;
lu[tail][1] = sx, lu[tail][2] = sy, lu[tail][3] = 1;
map[sx][sy] = 1;
while(head < tail) {
head++;
for(int i = 1; i <= 4; i++) {
int x = lu[head][1] + u[i], y = lu[head][2] + v[i];
//if(w1.x == x && w1.y == y) x = w2.x, y = w2.y;
//if(w2.x == x && w2.y == y) x = w1.x, y = w1.y;
for(int i = 1; i <= 26; i++)
if(x == w1[i].x && y == w1[i].y) {
x = w2[i].x, y = w2[i].y;
break;
} else if(x == w2[i].x && y == w2[i].y) {
x = w1[i].x, y = w1[i].y;
break;
}
if(x >= 1 && x <= n && y >= 1 && y <= m && map[x][y] == 0) {
tail++;
map[x][y] = 1;
lu[tail][1] = x, lu[tail][2] = y, lu[tail][3] = lu[head][3] + 1;
if(x == dx && y == dy) {
ans = lu[tail][3];
head = tail;
break;
}
}
}
}
}
int main() {
char s;
scanf("%d%d",&n, &m);
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++) {
cin>>s;
if (s == '=') dx = i, dy = j, map[i][j] = 0;
if (s == '@') sx = i, sy = j, map[i][j] = 0;
if (s == '#') map[i][j] = 1;
if (s == '.') map[i][j] = 0;
if (s >= 'A' && s <= 'Z') {
map[i][j] = 0;
if (w1[s - 'A' + 1].s == 0) w1[s - 'A' + 1].s = 1, w1[s - 'A' + 1].x = i, w1[s - 'A' + 1].y = j;
else w2[s - 'A' + 1].s = 1, w2[s - 'A' + 1].x = i, w2[s - 'A' + 1].y = j;
}
}
bfs();
cout<<ans - 1;
}
最新文章
- Thread Object wait() notify()基本
- Android Studio一些常用快捷键及快捷键冲突解决
- AngularJs项目文件以及文件夹结构
- mfc打开程序
- 连接到Windows Azure Point to Site VPN
- UEFI与MBR区别
- Android手机同步电脑端google chrome书签
- c# 模拟get和post
- CodeForces 682C	Alyona and the Tree (树+dfs)
- mvc 路由 使用
- sql server 2008 在安装了活动目录以后无法启动服务了
- 【JSP】JSP与oracle数据库交互案例
- 3TB硬盘的容量已经超出了传统分区标准的支持
- ES 6 : 变量的解构赋值
- 【转】JDBC学习笔记(9)——DBUtils的使用
- Sqoop2安装记录
- 在socket的server端处理client端发来的数据
- ERROR org.hibernate.hql.internal.ast.ErrorCounter unexpected token: form 异常解决
- SkylineGlobe 6.5 如何实现简单多边形的动态绘制 C#示例代码
- Python小白学习之路(二十一)—【迭代器】