题目链接:时空门问题

简单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 时空门问题的更多相关文章

  1. FZU Problem 2028 时空门问题

    Problem Description 在一个N*M的地图上旅行.地图上有些地方可以走用. 表示,不能走用 # 表示.在可以走的地方上下左右移动一格需要一个单位时间.可以走的地方还有一些时空之门.时空 ...

  2. FZU Problem 2028 时空门问题(DFS+优化)

    一开始是MLE,后来想到了用vector,化二维为一维,做了这一步优化后,这就是很基础的一个广搜了 #include<iostream> #include<cstdio> #i ...

  3. FZU 2028 BFS+vector

    一个普通的bfs 如果不看样例和input的解释... 四个0真是神样例 又被input误导 以为每个点都按顺序有标号 传送门的终点给的是一个点的标号 然后结果是什么呢?无尽的runtime erro ...

  4. FZU 2137 奇异字符串 后缀树组+RMQ

    题目连接:http://acm.fzu.edu.cn/problem.php?pid=2137 题解: 枚举x位置,向左右延伸计算答案 如何计算答案:对字符串建立SA,那么对于想双延伸的长度L,假如有 ...

  5. FZU 1914 单调队列

    题目链接:http://acm.fzu.edu.cn/problem.php?pid=1914 题意: 给出一个数列,如果它的前i(1<=i<=n)项和都是正的,那么这个数列是正的,问这个 ...

  6. ACM: FZU 2105 Digits Count - 位运算的线段树【黑科技福利】

     FZU 2105  Digits Count Time Limit:10000MS     Memory Limit:262144KB     64bit IO Format:%I64d & ...

  7. FZU 2112 并查集、欧拉通路

    原题:http://acm.fzu.edu.cn/problem.php?pid=2112 首先是,票上没有提到的点是不需要去的. 然后我们先考虑这个图有几个连通分量,我们可以用一个并查集来维护,假设 ...

  8. ACM: FZU 2107 Hua Rong Dao - DFS - 暴力

    FZU 2107 Hua Rong Dao Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I6 ...

  9. ACM: FZU 2112 Tickets - 欧拉回路 - 并查集

     FZU 2112 Tickets Time Limit:3000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u P ...

随机推荐

  1. PHP 全局变量 $_SERVER

    $_SERVER['SERVER_ADDR']    当前运行脚本所在的服务器的 IP 地址. $_SERVER['REQUEST_TIME']    请求开始时的时间戳.从 PHP 5.1.0 起可 ...

  2. 如何读懂 STATSPACK 报告 (转) & Toad 结合

    可与 toad 相结合的内容, 用 这种颜色可以利用 toad(database->monitor->server statistics)查看到下边的很多信息, 比如 wait event ...

  3. Android 随想录之 Android 系统架构

    应用层(Application) Android 的应用层由运行在 Android 设备上的所有应用程序共同构成(系统预装程序以及第三方应用程序). 系统预装应用程序包含拨号软件.短信.联系人.邮件客 ...

  4. iOS开发之 在release版本禁止输出NSLog内容

    因为NSLog的输出还是比较消耗系统资源的,而且输出的数据也可能会暴露出App里的保密数据,所以发布正式版时需要把这些输出全部屏蔽掉. 我们可以在发布版本前先把所有NSLog语句注释掉,等以后要调试时 ...

  5. golang json

    1.Go语言的JSON 库 Go语言自带的JSON转换库为 encoding/json 1.1)其中把对象转换为JSON的方法(函数)为 json.Marshal(),其函数原型如下 func Mar ...

  6. Tomcat:IOException while loading persisted sessions: java.io.EOFException解决手记

    原文:http://blog.csdn.net/lifuxiangcaohui/article/details/37659905 一直用tomcat一段时间都正常无事,最近一次启动tomcat就发生以 ...

  7. 【服务器防护】VPN的ip变更,导致无法连接服务器,解决方法【阿里云ECS】

    在阿里云的管理控制台,云服务器ECS - 对应服务器 - 选“管理” - “连接管理终端” 通过这个入口,可以进入Linux云服务器,修改防火墙限制的IP即可

  8. ltib学习抄录

    linux -- LTIB学习笔记 一 安装篇二 运行篇三 修改工具包 四 编译新的内核 ---------相关资料------------------------------------------ ...

  9. c#动态创建ODBC数据源

    使用C#有两种方法可以动态的创建ODBC数据源,这里我用比较常用的SQL2000作为例子. 方法1:直接操作注册表,需要引用Microsoft.Win32命名空间 /// <summary> ...

  10. VC++编译GSL

    目录 第1章 VC++    1 1.1 修改行结束符    1 1.2 修改#include "*.c" 为 #include "*.inl"    2 1. ...