poj 1367 robot(搜索)
题意:给你一个图,求起点 到 终点的最少时间
每次有两种选择①:往前走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(搜索)的更多相关文章
- 模拟 POJ 1573 Robot Motion
题目地址:http://poj.org/problem?id=1573 /* 题意:给定地图和起始位置,robot(上下左右)一步一步去走,问走出地图的步数 如果是死循环,输出走进死循环之前的步数和死 ...
- POJ 1573 Robot Motion(BFS)
Robot Motion Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 12856 Accepted: 6240 Des ...
- poj 3628 (搜索or背包)
好久没看背包题目了!!!生疏了!!!! 这题是背包题!!!不过对于这题,解决方法还是搜索省时!!! 题意:第一行给你一个N和VV,接下来N行,每行一个数,求得是任选N个数组合求和,求组合的和大于VV而 ...
- POJ 2046 Gap 搜索- 状态压缩
题目地址: http://poj.org/problem?id=2046 一道搜索状态压缩的题目,关键是怎样hash. AC代码: #include <iostream> #include ...
- POJ 1573 Robot Motion(模拟)
题目代号:POJ 1573 题目链接:http://poj.org/problem?id=1573 Language: Default Robot Motion Time Limit: 1000MS ...
- POJ 1573 Robot Motion
Robot Motion Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 12978 Accepted: 6290 Des ...
- POJ 1011 sticks 搜索
Sticks Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 125918 Accepted: 29372 Descrip ...
- POJ 3669 广度优先搜索
题意:巨大流星雨即将袭来.每个流星会对击中的地方以及周围(上下左右四格)造成破坏.Bessie开始时位于(0, 0)位置,并希望逃到一处不会被袭击到的地方(在第一象限内).已知每移动一格需要1个时间单 ...
- POJ 3009 深度优先搜索
问题:打冰球.冰球可以往上下左右4个方向走,只有当冰球撞到墙时才会停下来,而墙会消失.当冰球紧贴墙时,不能将冰球往那个方向打.冰球出界就当输,超过10次还没将冰球打到目标位置也当输.求用最小次数将冰球 ...
随机推荐
- Cocoapods最全完整使用教程
什么是cocoapods cocoapods是库管理工具. cocoapods的用途 解决库之间的依赖关系.如前文所述: 一个开源的项目可能是另一个项目的基础, A依赖B, B依赖C和D, D又依赖E ...
- Linux安装mongodb总结
由于自己的博客上线部署时需要用到mongodb来存储图片文件,所以先在本地电脑上安装了mongodb做测试,由于之前没接触过mongodb,所以安装过程中遇到了各种小问题,折腾了好久终于安装好并成功启 ...
- codevs 1283 等差子序列
http://codevs.cn/problem/1283/ 题目描述 Description 给一个 1 到 N 的排列{Ai},询问是否存在 1<=p1<p2<p3<p4& ...
- sql 几种循环方式
1:游标方式 ALTER PROCEDURE [dbo].[testpro] as ) --日期拼接 ) --仪表编号 ) --数据采集表 ) --数据采集备份表 ) ) begin set @yea ...
- Java看书学习笔记
1.POM:maven ,项目管理工具存放Jar包的文件2.mybatis-generator-core-1.3.2 生成文件 生成语句: java -jar mybatis-generator-co ...
- ELK学习总结(3-1)elk的基本查询
基本查询:内置条件 组合查询:组合基本查询 过滤:查询同时,通过filter筛选数据 准备工作 GET /library/books/_mget { "ids":["1 ...
- Web 项目报错No suitable driver found for jdbc:mysql://localhost:3306/book 的一个解决办法
确认jar包加入到了build path中,然后注意版本是否与数据库相配,还要留意将jar包放入WEB-INF下的lib文件夹中
- SpringBoot(四):banner的控制
banner在springboot中是一个支持可配(banner的样式,banner的颜色,banner的内容).是否显示. 1)banner显示内容配置: 默认springboot如果在src/re ...
- Struts(十三):通用标签
Struts标签简介: Struts2标签库提供了主题.模板支持,极大地简化了视图页面的编写,而且,struts2的主题.模板都提供了很好的扩展性,实现了更好的代码复用.Struts2允许在页面中使用 ...
- [论文阅读] Joint Face Detection and Alignment using Multi-task Cascaded Convolutional Networks(MTCNN)
相关论文:Joint Face Detection and Alignment using Multi-task Cascaded Convolutional Networks 概论 用于人脸检测和对 ...