题目链接

http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3865

思路

一个迷宫题 但是每次的操作数和普通的迷宫题不一样

0.按下按钮 即向当前方向盘的方向前进一格

1.往左移动方向盘

2.往右移动方向盘

3.不动

然后记录时间

易知,可以从四个方向到达终点,最后从这四个方向找最小值就是答案

当然要判断 不能通达的结果

AC代码

#include <cstdio>
#include <cstring>
#include <ctype.h>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <deque>
#include <vector>
#include <queue>
#include <string>
#include <map>
#include <stack>
#include <set>
#include <numeric>
#include <sstream>
#include <iomanip>
#include <limits> #define CLR(a) memset(a, 0, sizeof(a))
#define pb push_back using namespace std;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;
typedef pair<string, int> psi;
typedef pair<string, string> pss; const double PI = acos(-1);
const double E = exp(1);
const double eps = 1e-30; const int INF = 0x3f3f3f3f;
const int maxn = 5e4 + 5;
const int MOD = 1e9 + 7; char dis[4][4] =
{
'l', 'r', 'u', 'd',
'd', 'l', 'r', 'u',
'u', 'd', 'l', 'r',
'r', 'u', 'd', 'l',
}; map <char, int> M; int Move[4][2]
{
-1, 0, // up
1, 0, //down
0,-1, // left
0, 1, //right
}; char G[15][15]; int n, m, p; int dp[12][12][4];
int ans[12][12][4]; struct Node
{
int x, y, d;
}tmp; queue <Node> q; int sx, sy, ex, ey; bool ok(int x, int y, int d)
{
if (x < 0 || x >= n || y < 0 || y >= m)
return false;
if (G[x][y] == '*' || dp[x][y][d] != -1)
return false;
return true;
} void bfs()
{
while (!q.empty())
{
int x = q.front().x;
int y = q.front().y;
int d = q.front().d;
q.pop();
if (x == ex && y == ey && ans[x][y][d] == -1)
ans[x][y][d] = dp[x][y][d];
int nx = x + Move[M[dis[dp[x][y][d] / p % 4][d]]][0];
int ny = y + Move[M[dis[dp[x][y][d] / p % 4][d]]][1];
// press botton
if (ok(nx, ny, d))
{
dp[nx][ny][d] = dp[x][y][d] + 1;
tmp.x = nx;
tmp.y = ny;
tmp.d = d;
q.push(tmp);
} // move left
int nd = d - 1;
if (nd < 0)
nd = 3;
if (ok(x, y, nd))
{
dp[x][y][nd] = dp[x][y][d] + 1;
tmp.x = x;
tmp.y = y;
tmp.d = nd;
q.push(tmp);
} // move right
nd = d + 1;
if (nd > 3)
nd = 0;
if (ok(x, y, nd))
{
dp[x][y][nd] = dp[x][y][d] + 1;
tmp.x = x;
tmp.y = y;
tmp.d = nd;
q.push(tmp);
} // stay
if (dp[x][y][d] <= 200)
{
dp[x][y][d]++;
tmp.x = x;
tmp.y = y;
tmp.d = d;
q.push(tmp);
}
}
} void init()
{
M['u'] = 0;
M['d'] = 1;
M['l'] = 2;
M['r'] = 3;
} int main()
{
init();
int T;
cin >> T;
while (T--)
{
memset(dp, -1, sizeof(dp));
memset(ans, -1, sizeof(ans));
scanf("%d%d%d", &n, &m, &p);
int x = -1, y = -1;
for (int i = 0; i < n; i++)
{
scanf("%s", &G[i]);
for (int j = 0; j < m; j++)
{
if (G[i][j] == '@')
{
sx = i;
sy = j;
}
else if (G[i][j] == '$')
{
ex = i;
ey = j;
}
}
}
dp[sx][sy][0] = 0;
tmp.x = sx;
tmp.y = sy;
tmp.d = 0;
q.push(tmp);
bfs();
int Ans = INF;
for (int i = 0; i < 4; i++)
{
if (ans[ex][ey][i] != -1)
Ans = min(Ans, ans[ex][ey][i]);
}
if (Ans != INF)
printf("%d\n", Ans);
else
printf("YouBadbad\n");
}
}

ZOJ - 3865 Superbot 【BFS】的更多相关文章

  1. BFS+模拟 ZOJ 3865 Superbot

    题目传送门 /* BFS+模拟:dp[i][j][p] 表示走到i,j,方向为p的步数为多少: BFS分4种情况入队,最后在终点4个方向寻找最小值:) */ #include <cstdio&g ...

  2. 【bfs】抓住那头牛

    [题目] 农夫知道一头牛的位置,想要抓住它.农夫和牛都位于数轴上,农夫起始位于点N(0≤N≤100000),牛位于点K(0≤K≤100000).农夫有两种移动方式: 1.从X移动到X-1或X+1,每次 ...

  3. 【bfs】拯救少林神棍(poj1011)

    Description 乔治拿来一组等长的木棒,将它们随机地砍断,使得每一节木棍的长度都不超过50个长度单位.然后他又想把这些木棍恢复到为裁截前的状态,但忘记了初始时有多少木棒以及木棒的初始长度.请你 ...

  4. 【bfs】Knight Moves

    [题目描述] 输入nn代表有个n×nn×n的棋盘,输入开始位置的坐标和结束位置的坐标,问一个骑士朝棋盘的八个方向走马字步,从开始坐标到结束坐标可以经过多少步. [输入] 首先输入一个nn,表示测试样例 ...

  5. 【bfs】1252 走迷宫

    [题目描述] 一个迷宫由R行C列格子组成,有的格子里有障碍物,不能走:有的格子是空地,可以走. 给定一个迷宫,求从左上角走到右下角最少需要走多少步(数据保证一定能走到).只能在水平方向或垂直方向走,不 ...

  6. 【bfs】献给阿尔吉侬的花束

    [题目描述] 阿尔吉侬是一只聪明又慵懒的小白鼠,它最擅长的就是走各种各样的迷宫.今天它要挑战一个非常大的迷宫,研究员们为了鼓励阿尔吉侬尽快到达终点,就在终点放了一块阿尔吉侬最喜欢的奶酪.现在研究员们想 ...

  7. 【bfs】迷宫问题

    [题目描述] 定义一个二维数组: int maze[5][5] = { 0,1,0,0,0, 0,1,0,1,0, 0,0,0,0,0, 0,1,1,1,0, 0,0,0,1,0, }; 它表示一个迷 ...

  8. 【bfs】仙岛求药

    [题目描述] 少年李逍遥的婶婶病了,王小虎介绍他去一趟仙灵岛,向仙女姐姐要仙丹救婶婶.叛逆但孝顺的李逍遥闯进了仙灵岛,克服了千险万难来到岛的中心,发现仙药摆在了迷阵的深处.迷阵由M×N个方格组成,有的 ...

  9. 【bfs】BZOJ1102- [POI2007]山峰和山谷Grz

    最后刷个水,睡觉去.Bless All! [题目大意] 给定一个地图,为FGD想要旅行的区域,地图被分为n*n的网格,每个格子(i,j) 的高度w(i,j)是给定的.若两个格子有公共顶点,那么他们就是 ...

随机推荐

  1. php根据日期时间生成随机编码

    订单.申请单之类的需要一个编码,通过下面方法可得 代码如下: $PNUM="PG" . date("YmdHis").rand(1000,9999);

  2. openTK学习

    简介 the Open Tool Kit (OpenTK), 是对 OpenGL.OpenAL.OpenCL 的跨平台的封装,使用 C# 编写,它可以用在Mono.dotNet的语言:c#.VB.C+ ...

  3. Drools环境搭建

    Eclipse3.5安装Drools6.5.0.Final插件 到Drools下载页面(现在是http://www.jboss.org/drools/downloads.html) -下载并解压Dro ...

  4. js ioc 实现

    var ar=[]; var o1={ id:'o1', o2:null } ar.push(o1); var o2={ id:'o2', o1:null } ar.push(o2) var ioc= ...

  5. vue2.0中怎么获取元素

    在元素上添加 v-el:food-wrapper (不用驼峰的写法) vue1版本 报错: vue2版本 (vue2把vue1中的 v-el 改为了 ref vue1 v-el:foods-wrapp ...

  6. Python setup.py和MANIFEST.in文件

    Setup.py文件 from setuptools import setup from codecs import open # 第三方依赖包及版本号 requires = ['beautifuls ...

  7. mongodb配置副本集(多台服务器间的副本集搭建) replica[ˈrɛplɪkə]

    副本集具有多个副本保证了容错性,就算一个副本挂掉了还有很多副本存在,并且解决了“主节点挂掉了,整个集群内会自动切换”的问题.我们来看看mongoDB副本集的架构图: 由图可以看到客户端连接到整个副本集 ...

  8. dockerfile VOLUME 对外暴露目录设置问题

    最近遇到一个特殊问题, 容器对外映射挂载目录 , 发现容器中的目录并没有映射到宿主机上. 后经排查是是镜像中没有指定 volume # 例如 FROM debian:wheezy VOLUME /da ...

  9. 5.2 calendar--通用日期的相关函数(3)

    prmonth(theyear, themonth, w=0, l=0) 打印指定年和月的日历.格式与formatmonth()函数一样. 样例: #python 3.4 import calenda ...

  10. 应用程序之UITableView的编辑模式

    cell分层结构 效果展示 代码实现 一.cell的分层结构 二.效果展示 三.代码实现 // // ViewController.m // 01-TableView的删除实现 // // Creat ...