FZU 2028 时空门问题
题目链接:时空门问题
简单bfs,每个格子移动的方式除了上下左右,还有时空门,开始想着用邻接表保存每个点能通过时空门到达的点就ok了。很快的敲出来,很快的WA了。长久的dbug并没有发现error。然后换成vector存储,AC,再换成邻接表WA......感觉明明一模一样的好吗...讨厌bug!【怒】
=============================第二天晚上,神奇的大腿发现,我的head数组开成了maxn,然而实际上是maxn*maxn...沃日...最多有maxn*maxn个点啊....................早知道重构好了....
邻接表代码:AC
#include <stdio.h>
#include <string.h>
#include <iostream>
#define maxn 600
using namespace std; char mp[maxn][maxn];
int n, m; struct Node {
int u, v, nxt;
}edge[maxn*maxn]; struct Point {
int x, y;
int step;
}point[maxn*maxn], st, ed, temp; int head[maxn*maxn];
int tot; void addEdge(int u, int v) {
edge[tot].u = u;
edge[tot].v = v;
edge[tot].nxt = head[u];
head[u] = tot++;
} Point que[maxn*maxn];
int top, tail;
bool vis[maxn][maxn]; int dir[4][2] = {1, 0, -1, 0, 0, 1, 0, -1}; bool check(Point a) {
if (a.x >= 0 && a.x < n && a.y >= 0 && a.y < m && mp[a.x][a.y] != '#' && !vis[a.x][a.y])
return true;
return false;
} void bfs(Point st) {
top = 0, tail = -1;
vis[st.x][st.y] = 1;
que[++tail] = st; while(top <= tail) {
Point now = que[top++];
if (now.x == ed.x && now.y == ed.y) {
ed.step = now.step;
return;
}
for (int i=0; i<4; ++i) {
Point nxt;
nxt.x = now.x + dir[i][0];
nxt.y = now.y + dir[i][1];
if (check(nxt)) {
vis[nxt.x][nxt.y] = 1;
nxt.step = now.step + 1;
que[++tail] = nxt;
}
} int stp = now.x * m + now.y;
for (int i=head[stp]; i!=-1; i=edge[i].nxt) {
int edp = edge[i].v;
Point nxt;
nxt.x = edp / m, nxt.y = edp % m;
if (check(nxt)) {
vis[nxt.x][nxt.y] = 1;
nxt.step = now.step + 1;
que[++tail] = nxt;
}
}
}
return;
} int main() {
while(~scanf("%d%d", &n, &m)) {
tot = 0;
memset(head, -1, sizeof(head));
memset(vis, 0, sizeof(vis));
getchar();
for (int i=0; i<n; ++i) {
for (int j=0; j<m; ++j) {
scanf("%c", &mp[i][j]);
if (mp[i][j] == 's') {
st.x = i, st.y = j;
st.step = 0;
}
else if (mp[i][j] == 't') {
ed.x = i, ed.y = j;
}
}
if (i != n-1) scanf("\n");
} for (int i=0; i<n*m; ++i) {
int t;
scanf("%d", &t);
for (int j=0; j<t; ++j) {
int edx, edy;
scanf("%d%d", &edx, &edy);
edx -= 1, edy -= 1;
addEdge(i, edx*m+edy);
}
} // for (int i=0; i<n*m; ++i) {
// cout << head[i] << "....\n";
// for (int j=head[i]; j!=-1; j=edge[j].nxt) {
// cout << edge[j].u << " " << edge[j].v << endl;
// }
// cout << "++++++++++++++\n";
// } bfs(st);
printf("%d\n", ed.step);
}
return 0;
} /*
2 3
s.#
..t
1
2 2
0
0
0
1
1 2
0 */
vector代码:AC
#include <stdio.h>
#include <string.h>
#include <iostream>
#define maxn 600
#include <vector>
using namespace std; char mp[maxn][maxn];
int n, m; vector<int>num[maxn*maxn]; struct Point {
int x, y;
int step;
}point[maxn*maxn], st, ed, temp; int tot;
Point que[maxn*maxn];
int top, tail;
bool vis[maxn][maxn]; int dir[4][2] = {1, 0, -1, 0, 0, 1, 0, -1}; bool check(Point a) {
if (a.x >= 0 && a.x < n && a.y >= 0 && a.y < m && mp[a.x][a.y] != '#' && !vis[a.x][a.y])
return true;
return false;
} void bfs(Point st) {
top = 0, tail = -1;
vis[st.x][st.y] = 1;
que[++tail] = st; while(top <= tail) {
Point now = que[top++];
if (now.x == ed.x && now.y == ed.y) {
ed.step = now.step;
return;
}
for (int i=0; i<4; ++i) {
Point nxt;
nxt.x = now.x + dir[i][0];
nxt.y = now.y + dir[i][1];
if (check(nxt)) {
vis[nxt.x][nxt.y] = 1;
nxt.step = now.step + 1;
que[++tail] = nxt;
}
} int stp = now.x * m + now.y;
for (int i=0; i<num[stp].size(); ++i) {
int edp = num[stp][i];
Point nxt;
nxt.x = edp / m, nxt.y = edp % m;
if (check(nxt)) {
vis[nxt.x][nxt.y] = 1;
nxt.step = now.step + 1;
que[++tail] = nxt;
}
}
}
return;
} int main() {
while(~scanf("%d%d", &n, &m)) {
tot = 0;
memset(vis, 0, sizeof(vis));
getchar();
for (int i=0; i<n; ++i) {
for (int j=0; j<m; ++j) {
scanf("%c", &mp[i][j]);
if (mp[i][j] == 's') {
st.x = i, st.y = j;
st.step = 0;
}
else if (mp[i][j] == 't') {
ed.x = i, ed.y = j;
}
num[i*m+j].clear();
}
if (i != n-1) scanf("\n");
} for (int i=0; i<n*m; ++i) {
int t;
scanf("%d", &t);
for (int j=0; j<t; ++j) {
int edx, edy;
scanf("%d%d", &edx, &edy);
edx -= 1, edy -= 1;
num[i].push_back(edx*m+edy);
}
} bfs(st);
printf("%d\n", ed.step);
}
return 0;
} /*
2 3
s.#
..t
1
2 2
0
0
0
1
1 2
0 */
FZU 2028 时空门问题的更多相关文章
- FZU Problem 2028 时空门问题
Problem Description 在一个N*M的地图上旅行.地图上有些地方可以走用. 表示,不能走用 # 表示.在可以走的地方上下左右移动一格需要一个单位时间.可以走的地方还有一些时空之门.时空 ...
- FZU Problem 2028 时空门问题(DFS+优化)
一开始是MLE,后来想到了用vector,化二维为一维,做了这一步优化后,这就是很基础的一个广搜了 #include<iostream> #include<cstdio> #i ...
- FZU 2028 BFS+vector
一个普通的bfs 如果不看样例和input的解释... 四个0真是神样例 又被input误导 以为每个点都按顺序有标号 传送门的终点给的是一个点的标号 然后结果是什么呢?无尽的runtime erro ...
- FZU 2137 奇异字符串 后缀树组+RMQ
题目连接:http://acm.fzu.edu.cn/problem.php?pid=2137 题解: 枚举x位置,向左右延伸计算答案 如何计算答案:对字符串建立SA,那么对于想双延伸的长度L,假如有 ...
- FZU 1914 单调队列
题目链接:http://acm.fzu.edu.cn/problem.php?pid=1914 题意: 给出一个数列,如果它的前i(1<=i<=n)项和都是正的,那么这个数列是正的,问这个 ...
- ACM: FZU 2105 Digits Count - 位运算的线段树【黑科技福利】
FZU 2105 Digits Count Time Limit:10000MS Memory Limit:262144KB 64bit IO Format:%I64d & ...
- FZU 2112 并查集、欧拉通路
原题:http://acm.fzu.edu.cn/problem.php?pid=2112 首先是,票上没有提到的点是不需要去的. 然后我们先考虑这个图有几个连通分量,我们可以用一个并查集来维护,假设 ...
- ACM: FZU 2107 Hua Rong Dao - DFS - 暴力
FZU 2107 Hua Rong Dao Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I6 ...
- ACM: FZU 2112 Tickets - 欧拉回路 - 并查集
FZU 2112 Tickets Time Limit:3000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u P ...
随机推荐
- Nginx RTMP 专题
说明: 记录器 - 记录器名称 path - 记录文件路径(recorded file path) (/tmp/rec/mystream-1389499351.flv)filename - 省略目录的 ...
- 学习日记day8:移动端页面流程优化
一:切图 1:切那些(移动端能不用图片就不用图片:logo单独切因为要带链接) 2:普通切(快捷键:复制到新图层 选中 复制 新建 粘贴 保存) 3:类似切 (选中多个图层 矩形框多选 选择工 ...
- bootstrap学习笔记<五>(表单一)
表单 bootstrap为表单提供三种样式:默认表单,水平表单,内联表单. <form class="form-horizontal" role="form&quo ...
- JavaWeb学习总结(七)—HttpServletRequest
一.Request概述 request是Servlet.service()方法的一个参数,类型为javax.servlet.http.HttpServletRequest.在客户端发出每个请求时,服务 ...
- 【论文阅读记录】Real-Time Correlative Scan Matching
这篇文章是谷歌的Cartograph中实现real_time_correlative_scan_matcher的论文 Real-Time Correlative Scan MatchingEdwin ...
- 2016年GitHub排名前20的Python机器学习开源项目(转)
当今时代,开源是创新和技术快速发展的核心.本文来自 KDnuggets 的年度盘点,介绍了 2016 年排名前 20 的 Python 机器学习开源项目,在介绍的同时也会做一些有趣的分析以及谈一谈它们 ...
- ubuntu server nginx 安装与配置
ubuntu server nginx 安装与配置 一:关于nginx http://wiki.ubuntu.org.cn/Nginx http://nginx.org/cn http://wiki. ...
- 在腾讯云上创建您的SQL Cluster(1)
版权声明:本文由李斯达原创文章,转载请注明出处: 文章原文链接:https://www.qcloud.com/community/article/247 来源:腾云阁 https://www.qclo ...
- dedecms5.7怎么取消邮箱验证以及dedecms 会员发布的文章不需要审核的解决方法
后台 ——系统基本参数——会员设置——会员权限开通状态——改为0 1.实现会员发布文章不需要审核,非会员发布需要审核 在member这个文件夹下找到archives_sg_add.php这个文件,打开 ...
- linux笔记:linux常用命令-用户管理命令
用户管理命令:useradd(添加用户) 用户管理命令:passwd(设置和修改用户密码) 用户管理命令:who(查看所有登录用户的信息)