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次还没将冰球打到目标位置也当输.求用最小次数将冰球 ...
随机推荐
- django搭建web (四) models.py
demo 该demo模型主要是用于问题,选择单个或多个答案的问卷形式应用 # -*- coding: utf-8 -*- from __future__ import unicode_literals ...
- 直方图均衡化及matlab实现
在处理图像时,偶尔会碰到图像的灰度级别集中在某个小范围内的问题,这时候图像很难看清楚.比如下图: 它的灰度级别,我们利用一个直方图可以看出来(横坐标从0到255,表示灰度级别,纵坐标表示每个灰度级别的 ...
- 易错点---所有的字符都自带bool值
所有的字符都自带布尔值,只有0,None,空为False,其他全部为真!!!!!!!!!!! count = 0 while count < 3 : inp_age =input('Enter ...
- 电子称DIY(贴应变片+写代码)
第一步.应变片介绍 ---------------------------------------------------------------------------------------- ...
- 新概念英语(1-109)A Good Idea
Lesson 109 A good idea 好主意 Listen to the tape then answer this question. What does Jane have with he ...
- HTTP协议扫盲(三)HTTP协议的请求头列表和分类描述
一.请求报头和响应报头列表 1.Requests 头列表 Header 解释 示例 Accept 指定客户端能够接收的内容类型 Accept: text/plain, text/html Accept ...
- eclipse开发Groovy代码,与java集成,maven打包编译
今天尝试了一下在eclipse里面写Groovy代码,并且做到和Java代码相互调用,折腾了一下把过程记录下来. 首先需要给eclipse安装一下Groovy的插件,插件地址:https://gith ...
- Django 使用celery任务队列的配置
celery 情景:用户发起request,并等待response返回.在本些views中,可能需要执行一段耗时的程序,那么用户就会等待很长时间,造成不好的用户体验,比如发送邮件.手机验证码等. 使用 ...
- 基于JWT标准的用户认证接口实现
前面的话 实现用户登录认证的方式常见的有两种:一种是基于 cookie 的认证,另外一种是基于 token 的认证 .本文以基于cookie的认证为参照,详细介绍JWT标准,并实现基于该标签的用户认证 ...
- linux文件访问权限(像rw-r--rw-是什么意思)
Linux的文件访问权限分为 读.写.执行三种 r:可读(4) w:可写(2)对目录来说则可新建文件 x:可执行(1)对目录来说则可进入该目录 可用 ls -l 查看文件 像上图的-rw-r--rw- ...