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 ...
随机推荐
- 查询计划Hash和查询Hash
查询计划hash和查询hash 在SQL Server 2008中引入的围绕执行计划和缓冲的新功能被称为查询计划hash和查询hash.这是使用针对查询或查询计划的算法来生成二进制hash值的二进制对 ...
- mongoDB中的ID的生成原则
- spring对事物的支持
<!-- 事务管理器 对mybatis操作数据库事务控制,spring使用jdbc的事务控制类 --> <bean id="transactionManager" ...
- [转载] 高流量大并发Linux TCP 性能调优
原文: http://cenwj.com/2015/2/25/19 本文参考文章为: 优化Linux下的内核TCP参数来提高服务器负载能力 Linux Tuning 本文所面对的情况为: 高并发数 高 ...
- Linux输入子系统(转)
Linux输入子系统(Input Subsystem) 1.1.input子系统概述 输入设备(如按键,键盘,触摸屏,鼠标等)是典型的字符设备,其一般的工作机制是低层在按键,触摸等动作发生时产生一个中 ...
- mysql 聚集函数需要注意的问题
1.当没有记录的时候,使用聚集函数,会导致出现一条记录,记录的取值都是NULL,如下:mysql> select name from student where name='David';Emp ...
- php生成mysql的数据字典
<?php header('content-type:text/html;charset=utf-8'); define('DB_HOST','localhost'); define('DB_U ...
- js打印出对象的方法
var description = ""; for (var i in order) { var property = order[i]; description += i + & ...
- 【Todo】Python字符编码学习
Python中经常出现字符编码问题,在这里统一整理吧. 参考这篇文章:http://www.cnblogs.com/huxi/archive/2010/12/05/1897271.html 另外这个人 ...
- Java 文件IO
文件IO Java IO IO流用来处理设备之间的数据传输 Java对数据的操作是通过流的方式 Java用于操作流的对象都在IO包中 按操作数据分为 字节流和字符流 字符流的 ...