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 ...
随机推荐
- iOS - OC 术语表
1.术语表
- Redis基础知识之————如何处理客户端连接
redis 连接建立 Redis Redis 通过监听一个 TCP 端口或者 Unix socket 的方式来接收来自客户端的连接,当一个连接建立后,Redis 内部会进行以下一些操作: 首先,客户端 ...
- Java集合的Stack、Queue、Map的遍历
Java集合的Stack.Queue.Map的遍历 在集合操作中,常常离不开对集合的遍历,对集合遍历一般来说一个foreach就搞定了,但是,对于Stack.Queue.Map类型的遍历,还是有一 ...
- 转:Singleton模式
C++完美实现Singleton模式 转自:http://www.cppblog.com/dyj057/archive/2005/09/20/346.html boost库的Singleton的实现 ...
- java按值传递相关理解
Java没有引用传递只有按值传递,没有引用传递只有按值传递,值传递. 1. public class Test { public static void main(String[] args ...
- D3.js 简介和安装
一.What´s D3.js D3.js是一种数据操作类型的javascript库(也可视其为插件):结合HTML,SVG和CSS,D3可以图形化的,生动的展现数据. D3 的全称是(Data-Dri ...
- Android布局_相对布局RelativeLayout
一.RelativeLayout(相对布局)概述 RelativeLayout是相对布局控件,它包含的子控件将以控件之间的相对位置或者子类控件相对父类容器的位置的方式排列 二.RelativeLayo ...
- IE和Firefox的Javascript兼容性总结
长久以来JavaScript兼容性一直是Web开发者的一个主要问题.在正式规范.事实标准以及各种实现之间的存在的差异让许多开发者日夜煎熬.为此,主要从以下几方面差异总结IE和Firefox的Javas ...
- sensitivity and specificity(敏感性和特异性)
医学.机器学习等等,在统计结果时时长会用到这两个指标来说明数据的特性.
- 指针类型(C# 编程指南)
原文地址:https://msdn.microsoft.com/zh-cn/library/y31yhkeb.aspx 在不安全的上下文中,类型可以是指针类型.值类型或引用类型. 指针类型声明采用下列 ...