hdu 1254(搜索题)
推箱子
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 7062 Accepted Submission(s): 2009
箱子是一个很经典的游戏.今天我们来玩一个简单版本.在一个M*N的房间里有一个箱子和一个搬运工,搬运工的工作就是把箱子推到指定的位置,注意,搬运工
只能推箱子而不能拉箱子,因此如果箱子被推到一个角上(如图2)那么箱子就不能再被移动了,如果箱子被推到一面墙上,那么箱子只能沿着墙移动.
现在给定房间的结构,箱子的位置,搬运工的位置和箱子要被推去的位置,请你计算出搬运工至少要推动箱子多少格.
入数据的第一行是一个整数T(1<=T<=20),代表测试数据的数量.然后是T组测试数据,每组测试数据的第一行是两个正整数M,N(2&
lt;=M,N<=7),代表房间的大小,然后是一个M行N列的矩阵,代表房间的布局,其中0代表空的地板,1代表墙,2代表箱子的起始位置,3代
表箱子要被推去的位置,4代表搬运工的起始位置.
5 5
0 3 0 0 0
1 0 1 4 0
0 0 1 0 0
1 0 2 0 0
0 0 0 0 0
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <queue>
#include <algorithm>
using namespace std;
int n,m;
int graph[][];
bool flag[][][];
bool vis[][];
int dir[][] = {{,},{-,},{,},{,-}};
struct Node
{
int x,y;
int step;
int g[][];
}s,person,t;
bool check(int x,int y)
{
if(x<||x>=n||y<||y>=m) return false;
return true;
}
bool bfs2(Node s,Node target){
person = s;
for(int i=;i<n;i++){
for(int j=;j<m;j++){
if(person.g[i][j]==){
person.x = i;
person.y = j;
person.step = ;
}
}
}
queue<Node>q;
memset(vis,false,sizeof(vis));
q.push(person);
vis[person.x][person.y] = true;
while(!q.empty()){
Node now = q.front();
q.pop();
if(now.x==target.x&&now.y==target.y){
return true;
}
Node next;
for(int i=;i<;i++){
next.step = now.step+;
next.x = now.x +dir[i][];
next.y = now.y + dir[i][];
if(!check(next.x,next.y)||s.g[next.x][next.y]==||vis[next.x][next.y]||s.g[next.x][next.y]==) continue;
vis[next.x][next.y] = true;
q.push(next);
}
}
return false;
}
int bfs(){
memset(flag,false,sizeof(flag));
queue <Node> q;
q.push(s);
while(!q.empty()){
Node now = q.front();
q.pop();
if(now.x==t.x&&now.y==t.y) return now.step;
Node next,target;
for(int i=;i<;i++){
next = now;
next.x = now.x+dir[i][];
next.y = now.y+dir[i][];
next.step = now.step+;
if(!check(next.x,next.y)||flag[next.x][next.y][i]||next.g[next.x][next.y]==) continue; ///该方向已经走过了
///人的目标点(人以此点为目标点将箱子从now推向next)
target.x = now.x - dir[i][];
target.y = now.y - dir[i][];
if(!check(target.x,target.y)) continue;
if(bfs2(next,target)){
swap(next.g[now.x][now.y],next.g[next.x][next.y]);
swap(next.g[target.x][target.y],next.g[person.x][person.y]);
flag[next.x][next.y][i] = true;
q.push(next);
}
}
}
return -;
}
int main()
{
int tcase;
scanf("%d",&tcase);
while(tcase--)
{
scanf("%d%d",&n,&m);
for(int i=; i<n; i++)
for(int j=; j<m; j++){
scanf("%d",&s.g[i][j]);
if(s.g[i][j]==){
s.x = i;
s.y = j;
s.step = ;
}
if(s.g[i][j]==){
t.x = i;
t.y = j;
}
}
int res = bfs();
printf("%d\n",res);
}
}
hdu 1254(搜索题)的更多相关文章
- hdu&&poj搜索题题号
搜索 hdu1067 哈希 hdu1401 双向搜索 hdu1430 哈希 hdu1667 跌搜+启发式函数 hdu1685 启发式搜索 hdu1813 启发式搜索 hdu1885 状态压缩搜索 hd ...
- [HDU 2102] A计划(搜索题,典型dfs or bfs)
A计划 Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...
- Square HDU 1518 搜索
Square HDU 1518 搜索 题意 原题链接 给你一定若干个木棒,让你使用它们组成一个四边形,要求这些木棒必须全部使用. 解题思路 木棒有多种组合方式,使用搜索来进行寻找,这里需要进行优化,不 ...
- O - 推箱子 HDU - 1254(bfs_box + bfs_man)
O - 推箱子 HDU - 1254 推箱子是一个很经典的游戏.今天我们来玩一个简单版本.在一个M*N的房间里有一个箱子和一个搬运工,搬运工的工作就是把箱子推到指定的位置,注意,搬运工只能推箱子而不能 ...
- bnuoj 33656 J. C.S.I.: P15(图形搜索题)
http://www.bnuoj.com/bnuoj/problem_show.php?pid=33656 [题解]:暴力搜索题 [code]: #include <iostream> # ...
- HDU(搜索专题) 1000 N皇后问题(深度优先搜索DFS)解题报告
前几天一直在忙一些事情,所以一直没来得及开始这个搜索专题的训练,今天做了下这个专题的第一题,皇后问题在我没有开始接受Axie的算法低强度训练前,就早有耳闻了,但一直不知道是什么类型的题目,今天一看,原 ...
- 历年NOIP中的搜索题
什么题目都不会做于是开始做搜索题. 然而我搜索题也不会做了. 铁定没戏的蒟蒻. 1.NOIP2004 虫食算 “对于给定的N进制加法算式,求出N个不同的字母分别代表的数字,使得该加法算式成立.输入数据 ...
- hdu 5887 搜索+剪枝
Herbs Gathering Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)T ...
- hdu 5636 搜索 BestCoder Round #74 (div.2)
Shortest Path Accepts: 40 Submissions: 610 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: ...
随机推荐
- 201621123080《Java程序设计》第十一周学习总结
201621123080<Java程序设计>第十一周学习总结 1. 本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结多线程相关内容. 2. 书面作业 本次PTA作业题集多线程 ...
- salt常用模块
salt 常用命令整理 转载:https://www.cnblogs.com/aslongas/p/6964890.html salt 常用命令整理 ***********模块******** ...
- 【Maven】 (请使用 -source 8 或更高版本以启用 lambda 表达式)
在使用mvn install编译maven项目时,报了 “ (请使用 -source 8 或更高版本以启用 lambda 表达式)”错误,是因为设置的maven默认jdk编译版本太低的问题. 可使用两 ...
- 如何在 CentOS 7 上安装 Python 3
当前最新的 CentOS 7.5 默认安装的是 Python 2.7.5,并且默认的官方 yum 源中不提供 Python 3 的安装包.这里主要介绍两种在 CentOS 7 中安装 Python 3 ...
- shell-code-拷贝文件
#!/bin/bash while read F do cp ${F}"_pe_1.fastq.gz" /public/home/chenjy/usr/ZD/data/cleand ...
- (转).gitignore详解
本文转自http://sentsin.com/web/666.html 今天讲讲Git中非常重要的一个文件——.gitignore. 首先要强调一点,这个文件的完整文件名就是“.gitignore”, ...
- Linux学习-什么是进程 (process)
触发 任何一个事件时,系统都会将他定义成为一个进程,并且给予这个进程一个 ID ,称为 PID,同时依据启发这个进程的用户与相关属性关系,给予这个 PID 一组有效的权限设定.从此以后,这 个 PID ...
- layui的upload组件使用以及上传阻止测试
背景:页面上一个按钮,点击弹出上传框,从按钮的方法代码开始写:处理未选择文件阻止上传:通过判断选择文件的数量,显示或隐藏上传按钮: 在js中定义: function uploadFile(){ la ...
- luogu2114 [NOI2014]起床困难综合症
大约是第一次做近几年NOI题(尽管是签到题)? 制作一个真值表,要是有哪一位原本是0但是能变成1那真是太好啦,要是有哪一位原来是1能变成1并且算上它不会超过m那也不错. #include <io ...
- python - 函数的相互调用 及 变量的作用域
# -*- coding:utf-8 -*- '''@project: jiaxy@author: Jimmy@file: study_函数的相互调用及变量的作用域.py@ide: PyCharm C ...