题意:给你一个图,求起点 到 终点的最少时间

每次有两种选择①:往前走1~3步                ②原地选择90°   费时皆是1s

图中1为障碍物,而且不能出边界。还要考虑机器人的直径

思路:

bfs,但是在判断时出了点问题/(ㄒoㄒ)/,想复杂了,导致一直wr。

用vis[x][y][dir] 表示状态,即在(x,y)点的dir方向

问题:

①考虑判断条件时出现问题,开始想得太简单,后来想得太复杂= =

②最开始写的搜索部分太乱

③题意理解不准确。。

#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <cstdlib>
#include <queue>
#include <algorithm>
typedef long long ll;
using namespace std; int tmap[60][60];
int vis[60][60][5];
struct node
{
int x,y;
int step;
int dir;
};
int n,m;
char to[10];
int fx,fy,tx,ty;
int dir[5][2] = {{-1,0},{0,1},{1,0},{0,-1}};
int ans = -1; bool judge(node a) //判断当前是否包含了黑点
{
if(a.x<1||a.x>=n||a.y<1||a.y>=m)
return false;
if(tmap[a.x][a.y]||tmap[a.x+1][a.y]||tmap[a.x][a.y+1]||tmap[a.x+1][a.y+1])
return false;
return true;
} void bfs()
{
queue<node>q; node cur;
ans = -1;
cur.x = fx;
cur.y = fy;
cur.step = 0; if(to[0] == 's') cur.dir = 2;
if(to[0] == 'n') cur.dir = 0;
if(to[0] == 'w') cur.dir = 3;
if(to[0] == 'e') cur.dir = 1;
vis[cur.x][cur.y][cur.dir] = 1;
q.push(cur);
while(!q.empty())
{
cur = q.front();
q.pop();
// printf("%d %d %d %d\n",cur.x,cur.y,cur.dir,cur.step);
for(int i = 1; i <= 3; i++)
{
node t;
int dr = cur.dir;
t.dir = cur.dir;
t.x = cur.x + dir[dr][0]*i;
t.y = cur.y + dir[dr][1]*i;
t.step = cur.step+1;
if(!judge(t))
break;
if(t.x == tx && t.y == ty)
{
ans = t.step;
// printf("%d %d %d\n",t.x,t.y,t.dir);
return ;
}
if(!vis[t.x][t.y][t.dir])
{
vis[t.x][t.y][t.dir] = 1;
q.push(t);
}
}
for(int i = 0; i < 4; i++)
{
if(max(i,cur.dir)-min(i,cur.dir) == 2)
continue;
if(vis[cur.x][cur.y][i])
continue;
node t = cur;
t.dir = i;
t.step = cur.step+1;
vis[t.x][t.y][t.dir] = 1;
q.push(t);
}
}
return ;
} int main()
{
while(scanf("%d%d",&n,&m) != EOF)
{
if(n == 0 && m == 0)
break;
memset(vis,0,sizeof(vis));
for(int i = 1; i <= n; i++)
for(int j = 1; j <= m; j++)
{
scanf("%d",&tmap[i][j]);
}
scanf("%d%d%d%d",&fx,&fy,&tx,&ty);
scanf("%s",to);
if(tmap[tx][ty])
{
printf("-1\n");
continue;
}
if(fx == tx && fy == ty)
{
printf("0\n");
continue;
}
bfs(); if(ans == -1)
printf("-1\n");
else
printf("%d\n",ans);
}
return 0;
}

  

poj 1367 robot(搜索)的更多相关文章

  1. 模拟 POJ 1573 Robot Motion

    题目地址:http://poj.org/problem?id=1573 /* 题意:给定地图和起始位置,robot(上下左右)一步一步去走,问走出地图的步数 如果是死循环,输出走进死循环之前的步数和死 ...

  2. POJ 1573 Robot Motion(BFS)

    Robot Motion Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 12856   Accepted: 6240 Des ...

  3. poj 3628 (搜索or背包)

    好久没看背包题目了!!!生疏了!!!! 这题是背包题!!!不过对于这题,解决方法还是搜索省时!!! 题意:第一行给你一个N和VV,接下来N行,每行一个数,求得是任选N个数组合求和,求组合的和大于VV而 ...

  4. POJ 2046 Gap 搜索- 状态压缩

    题目地址: http://poj.org/problem?id=2046 一道搜索状态压缩的题目,关键是怎样hash. AC代码: #include <iostream> #include ...

  5. POJ 1573 Robot Motion(模拟)

    题目代号:POJ 1573 题目链接:http://poj.org/problem?id=1573 Language: Default Robot Motion Time Limit: 1000MS ...

  6. POJ 1573 Robot Motion

    Robot Motion Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 12978   Accepted: 6290 Des ...

  7. POJ 1011 sticks 搜索

    Sticks Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 125918   Accepted: 29372 Descrip ...

  8. POJ 3669 广度优先搜索

    题意:巨大流星雨即将袭来.每个流星会对击中的地方以及周围(上下左右四格)造成破坏.Bessie开始时位于(0, 0)位置,并希望逃到一处不会被袭击到的地方(在第一象限内).已知每移动一格需要1个时间单 ...

  9. POJ 3009 深度优先搜索

    问题:打冰球.冰球可以往上下左右4个方向走,只有当冰球撞到墙时才会停下来,而墙会消失.当冰球紧贴墙时,不能将冰球往那个方向打.冰球出界就当输,超过10次还没将冰球打到目标位置也当输.求用最小次数将冰球 ...

随机推荐

  1. jQuery函数学习

    函数:after(content) 功能:在每个匹配的元素后面添加html内容 返回:jQuery对象 参数:content (<Content>): Content to insert ...

  2. iOS开发-OC中TabView的编辑

    UITableView编辑 1> UITableView 编辑流程 2> UITableView 编辑步骤(四步) ① 第一步 : 让 TableView 处于编辑状态(在按钮点击事件方法 ...

  3. 08-TypeScript中的类

    类的概念通常是在后端开发中实现的思想,比如C#.C++或Java,传统的JavaScript开发通过使用原型模式来模拟类的功能.在TypeScript中,天生就是支持类 的,可以让前端的开发更加具有面 ...

  4. JAVA_SE基础——16.方法

    接触过C语言的同学,这小章节很容易接受.Java中的方法是类似与C语言中的函数  功能和调用方法都类似  只不过叫法不一样  因为java是面向对象  c是面向过程    仅仅是叫法不同.. . 看到 ...

  5. js 开发注意事项

    涉及api post 请求的, 涉及sqlite 存储的, conent 用encodeURIComponent, decodeURIComponent ,处理 JSON.parse 最好加上try ...

  6. Solaris 11 system package 安装与更新(如:assembler)

    最近在VirtualBox虚拟机中导入了Solaris 11.3.在里面安装Oracle数据库时,先行条件检查没通过,提示缺少程序包assembler. 在网上看了许多,这方面的信息还比较少.最后在O ...

  7. 常用cmd代码片段及.net core打包脚本分享

    bat基础命令 注释:rem 注释~~ 输出:echo hello world 接收用户输入:%1 %2,第n个变量就用%n表示 当前脚本路径:%~dp0 当前目录路径:%cd% 设置变量:set c ...

  8. Spring Cloud的DataRest(二)

    一.创建工程 1.主程序 2.依赖 3.配置 二.案例开发 1.entity 2.repository 三.案例验证 安装postman4.13,启动应用,执行如下案例验证! 1.create - p ...

  9. ELK学习总结(2-4)bulk 批量操作-实现多个文档的创建、索引、更新和删除

    bulk 批量操作-实现多个文档的创建.索引.更新和删除 ----------------------------------------------------------------------- ...

  10. Docker学习实践 - Docker安装MySql数据库

    Docker安装MySQL数据库 1.Ubuntu安装MySQL安装 (1)安装编译源码需要的包 sudo apt-get install make cmake gcc g++ bison libnc ...