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次还没将冰球打到目标位置也当输.求用最小次数将冰球 ...
随机推荐
- window.showModalDialog
//新版本谷歌没有window.showModalDialog,创建一个window.openif(window.showModalDialog == undefined){ window.show ...
- JAVA_SE基础——31.this关键字
黑马程序员入学blog... 也算是学习笔记体会. this的通俗解释: 有一个A类,一个B方法,一个C变量,其中B和C都在类A中 this.B()就是调用A类中的B方法 this.C=1(假设C是一 ...
- 关于读取Sql Server数据库时间前端处理问题
var time = this.CreateTime; this.CreateTime = new Date(time.replace("T", " ")).F ...
- java将一个正整数分解质因数。例如:输入90,打印出90=2*3*3*5。
首先我们的算法是:例如 输入的是 90 1.找到90的最小公约数(1除外)是 2 2.然后把公约数 2 输出 3.接着用 90 / 2 = 45 (如果这里是素数,就结束,否则继续找最小公约数) 4. ...
- Django之中间件
中间件简介 什么是中间件 中间件是一个用来处理Django的请求和响应的框架级别的钩子.它是一个轻量.低级别的插件系统,用于在全局范围内改变Django的输入和输出.每个中间件组件都负责做一些特定的功 ...
- Go语言的核心Routine-Channel
前言 Go语言通过routine,提供了并发编程的支持. Routine特性 (1) goroutine是Go语言运行库的功能,不是操作系统提供的功能,goroutine不是用线程实现的. 例:启动一 ...
- mysql自带的example测试数据库导入Centos6.5
1.下载数据库 下载地址: [test数据库] (https://github.com/datacharmer/test_db) 不出意外的话,下载下来是个unzip文件. 2.上传到数据库服务器 r ...
- AES(高级加密)
AES(高级加密) #3.6安装 pip3 install pycryptodome #mac pip3 install pycrypto a. 事例: ####################### ...
- MySQL基础操/下
MySQL基础操 一.自增补充 desc (表名)t1: 查看表格信息内容 表的信息 show create table t1(表名):也是查看信息,还不多是横向查看 show create tabl ...
- 如何使用Visual Studio 2017自带的源代码反编译功能
反编译C#源代码,大家可能第一时间想到 .NET Reflector 这个工具.但是这个工具反编译出来的代码跟实际源码还是有一定差距的,阅读起来不是很便利. 本人在查看Visual Studio 20 ...