题目链接:吃豆人

比赛的时候写的bfs,纠结要不要有vis数组设置已被访问,没有的话死循环,有的话就不一定是最优解了。【此时先到的不一定就是时间最短的。】于是换dfs,WA。

赛后写了个炒鸡聪明的dfs,TLE,才发现时间复杂度好像是4^(n*m)。T_T

依然感觉这个dfs很棒。

bfs已AC,怎么解决的这个问题呢,如果当前位置next 被优化了则加入队列,以此优化其他位置,否则不加入队列。T_T好有道理~~~

感觉bfs和dfs好神奇的说~

dfs TLE代码:

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <cmath>
#include <queue>
#define inf 100000000
using namespace std; char mp[30][30]; struct Node{
int x, y;
}st, ed, tool, temp, nxt; int vis[30][30];
double step[30][30];
int dir[4][2] = {1, 0, -1, 0, 0, 1, 0, -1};
int n, m; bool check(Node temp) {
if (temp.x >= 0 && temp.x < n && temp.y >= 0 && temp.y < m && !vis[temp.x][temp.y] && mp[temp.x][temp.y] != 'X') {
return true;
)}
return false;
} void get(Node temp, Node ed) {
if (temp.x == ed.x) {
int minn = min(temp.y, ed.y);
int maxm = max(temp.y, ed.y);
for (int i=minn+1; i<maxm; ++i) {
if (mp[ed.x][i] == 'X' || mp[ed.x][i] == 'S') return;
}
int t = abs(temp.y - ed.y);
step[ed.x][ed.y] = min(step[temp.x][temp.y]+t*0.2,step[ed.x][ed.y]);
}
else if (temp.y == ed.y) {
int minn = min(temp.x, ed.x);
int maxm = max(temp.x, ed.x);
for (int i=minn+1; i<maxm; ++i) {
if (mp[i][ed.y] == 'X' || mp[i][ed.x] == 'S') return;
}
int t = abs(temp.x - ed.x);
step[ed.x][ed.y] = min(step[temp.x][temp.y]+t*0.2, step[ed.x][ed.y]);
}
} void dfs(Node st, Node ed, bool v) {
for (int i=0; i<4; ++i) {
nxt.x = st.x + dir[i][0];
nxt.y = st.y + dir[i][1];
if (check(nxt)) {
double t = step[st.x][st.y];
if (v) t += 0.5;
else t += 1;
step[nxt.x][nxt.y] = min(step[nxt.x][nxt.y], t);
vis[nxt.x][nxt.y] = 1;
get(nxt, ed);
if (nxt.x == tool.x && nxt.y == tool.y) dfs(nxt, ed, true);
else dfs(nxt, ed, v);
}
}
vis[st.x][st.y] = 0;
return;
} int main() {
while(~scanf("%d%d", &n, &m)) {
bool v = false;
memset(vis, 0, sizeof(vis));
for (int i=0; i<n; ++i) {
for (int j=0; j<m; ++j) {
step[i][j] = inf;
}
}
getchar();
for (int i=0; i<n; ++i) {
for (int j=0; j<m; ++j) {
scanf("%c", &mp[i][j]);
if (mp[i][j] == 'P') {
st.x = i, st.y = j;
}
else if (mp[i][j] == 'B') {
ed.x = i, ed.y = j;
}
else if (mp[i][j] == 'S') {
tool.x = i, tool.y = j;
}
}
if (i != n-1) scanf("\n");
}
step[st.x][st.y] = 0;
vis[st.x][st.y] = 1;
dfs(st, ed, v);
get(st, ed);
double ans = step[ed.x][ed.y];
if (ans != inf)
printf("%.1lf\n", ans);
else printf("-1\n");
}
return 0;
}

bfs AC 代码:

/*
直接一个bfs,用两个数组保存有工具和没有工具时需要的时间。
*/ #include <stdio.h>
#include <string.h>
#include <iostream>
#include <queue>
#define inf 1000000000
using namespace std; char mp[30][30];
int n, m; struct Node {
int x, y, s; // 0 表示没有工具 1 表示有工具了。
}st, ed, temp, now, nxt; int step[30][30][2]; int dir[4][2] = {1, 0, -1, 0, 0, 1, 0, -1}; void bfs(Node st) {
queue<Node> que;
que.push(st);
step[st.x][st.y][0] = 0;
while(!que.empty()) {
now = que.front();
que.pop();
if (now.x == ed.x) {
int miny = min(now.y, ed.y);
int maxy = max(now.y, ed.y);
bool get = true;
for (int y=miny+1; y<maxy; ++y) {
if (mp[now.x][y] == 'X' || mp[now.x][y] == 'S') {
get = false;
break;
}
}
if (get) step[ed.x][ed.y][now.s] = min(step[now.x][now.y][now.s] + 2 * (maxy - miny), step[ed.x][ed.y][now.s]);
} if (now.y == ed.y) {
int minx = min(now.x, ed.x);
int maxx = max(now.x, ed.x);
bool get = true;
for (int x=minx+1; x<maxx; ++x) {
if (mp[x][ed.y] == 'X' || mp[x][ed.y] == 'S') {
get = false;
}
}
if (get) step[ed.x][ed.y][now.s] = min(step[now.x][now.y][now.s] + 2 * (maxx - minx), step[ed.x][ed.y][now.s]);
} int T;
for (int i=0; i<4; ++i) {
nxt.x = now.x + dir[i][0];
nxt.y = now.y + dir[i][1];
nxt.s = now.s;
if (nxt.x<0 || nxt.y<0 || nxt.x>=n || nxt.y>=m) continue;
if (mp[nxt.x][nxt.y] == 'X') continue;
if (now.s) T = 5;
else T = 10;
if (step[nxt.x][nxt.y][nxt.s] > step[now.x][now.y][now.s] + T) {
step[nxt.x][nxt.y][nxt.s] = step[now.x][now.y][now.s] + T;
if (mp[nxt.x][nxt.y] == 'S') {
nxt.s = 1;
step[nxt.x][nxt.y][1] = step[nxt.x][nxt.y][0];
}
que.push(nxt);
}
}
}
} int main() {
while(cin >> n >> m) {
for (int i=0; i<n; ++i) {
for (int j=0; j<m; ++j) {
for (int k=0; k<2; ++k) {
step[i][j][k] = inf;
}
}
} for (int i=0; i<n; ++i) {
for (int j=0; j<m; ++j) {
cin >> mp[i][j];
if (mp[i][j] == 'P') {
st.x = i, st.y = j;
}
else if (mp[i][j] == 'B') {
ed.x = i, ed.y = j;
}
}
} st.s = 0;
bfs(st);
int ans = min(step[ed.x][ed.y][0], step[ed.x][ed.y][1]);
if (ans == inf) cout << "-1\n";
else printf("%.1lf\n", ans*1.0/10);
}
return 0;
}

  

FZU 2124 吃豆人 bfs的更多相关文章

  1. FZU 2124 FOJ 2124 吃豆人【BFS】

     Problem 2124 吃豆人 Accept: 134    Submit: 575 Time Limit: 1000 mSec    Memory Limit : 32768 KB  Probl ...

  2. Fzu2124 - 吃豆人 BFS

    Description 吃豆人是一款非常经典的游戏,游戏中玩家控制吃豆人在地图上吃光所有豆子,并且避免被怪物抓住. 这道题没有怪物,将游戏的画面分成n*m的格子,每格地形可能为空地或者障碍物,吃豆人可 ...

  3. TurnipBit开发板DIY呼吸的吃豆人教程实例

    转载请以链接形式注明文章来源(MicroPythonQQ技术交流群:157816561,公众号:MicroPython玩家汇) 0x00前言 吃豆人是耳熟能详的可爱形象,如今我们的TurnipBit也 ...

  4. [代码]解析nodejs的require,吃豆人的故事

    最近在项目中需要对nodejs的require关键字做解析,并且替换require里的路径.一开始我希望nodejs既然作为脚本语言,内核提供一个官方的parser库应该是一个稳定可靠又灵活的渠道,然 ...

  5. Unity项目 - 吃豆人Pacman

    项目展示 Github项目地址:Pacman 涉及知识 切片制作 Animations 状态机设置,any state切换,重写状态机 按键读取进行整数距离的刚体移动 用射线检测碰撞性 渲染顺序问题 ...

  6. 利用纯css写三角形,弧度箭头,吃豆人,气泡。放大镜,标签的源码

    1. 向上三角形

  7. css吃豆人动画

    一. Css吃豆人动画 1. 上半圆:两个div,内部一个圆div,外部设置宽高截取半圆 外部div动画:animation: 动画样式 1s(时长) ease(动画先低速后快速) infinite( ...

  8. 用python的turtle作图(二)动画吃豆人

    本文是用python的turtle作图的第二篇,通过这个例子可以了解动画的原理,用python自带的turtle库制作一些小动画. 1.问题描述 在上一篇"用python的turtle作图( ...

  9. Pac-Man 吃豆人

    发售年份 1980 平台 街机 开发商 南梦宫(Namco) 类型 迷宫 https://www.youtube.com/watch?v=dScq4P5gn4A

随机推荐

  1. Python3基础 set 自动将重复合并掉 不支持索引

             Python : 3.7.0          OS : Ubuntu 18.04.1 LTS         IDE : PyCharm 2018.2.4       Conda ...

  2. Linux内存管理--用户空间和内核空间【转】

    本文转载自:http://blog.csdn.net/yusiguyuan/article/details/12045255 关于虚拟内存有三点需要注意: 4G的进程地址空间被人为的分为两个部分--用 ...

  3. 支持github的chrome插件octotree

    1.这款插件有何作用? 方便浏览github上的源码 2.安装方法 通过以下链接进行安装: https://chrome.google.com/webstore

  4. luogu3261 懒惰左偏树 [JLOI2015]城池攻占

    目录 题目 思路 错误&&反思 代码 题目 luogu 原来左偏树真的能懒惰下放 那这篇博客应该要咕咕了 一开始我按照那篇博客想了一下,感觉emm,还是瞄了一眼看到了pushdown ...

  5. CodeForces 1029E div3

    题目链接 第一道场上自己做出来的E题...虽然是div3,而且是原题... 当时做完ABC,D题没有思路就去怼E了,然后发现貌似原题? 事实上就是原题... 给个原题链接... [HNOI2003]消 ...

  6. Codeforces Round #323 (Div. 2) C. GCD Table map

    题目链接:http://codeforces.com/contest/583/problem/C C. GCD Table time limit per test 2 seconds memory l ...

  7. String中hashCode方法的线程安全

    class String{ //默认值是0 int hash; public int hashCode() { //将成员变量hash缓存到局部变量 int h = hash; //这里使用的是局部变 ...

  8. python 加密与解密

    加密算法分类 对称加密算法: 对称加密采用了对称密码编码技术,它的特点是文件加密和解密使用相同的密钥 发送方和接收方需要持有同一把密钥,发送消息和接收消息均使用该密钥. 相对于非对称加密,对称加密具有 ...

  9. js 字符串匹配

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  10. 使用 data.table 包操作数据

    在第一节中,我们回顾了许多用于操作数据框的内置函数.然后,了解了 sqldf 扩展包,它使得简单的数据查询和统计变得更简便.然而,两种方法都有各自的局限性.使用内置函数可能既繁琐又缓慢,而相对于各式各 ...