推箱子

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 4593 Accepted Submission(s):
1298

Problem Description
推箱子是一个很经典的游戏.今天我们来玩一个简单版本.在一个M*N的房间里有一个箱子和一个搬运工,搬运工的工作就是把箱子推到指定的位置,注意,搬运工只能推箱子而不能拉箱子,因此如果箱子被推到一个角上(如图2)那么箱子就不能再被移动了,如果箱子被推到一面墙上,那么箱子只能沿着墙移动.

现在给定房间的结构,箱子的位置,搬运工的位置和箱子要被推去的位置,请你计算出搬运工至少要推动箱子多少格.

 
Input
输入数据的第一行是一个整数T(1<=T<=20),代表测试数据的数量.然后是T组测试数据,每组测试数据的第一行是两个正整数M,N(2<=M,N<=7),代表房间的大小,然后是一个M行N列的矩阵,代表房间的布局,其中0代表空的地板,1代表墙,2代表箱子的起始位置,3代表箱子要被推去的位置,4代表搬运工的起始位置.
 
Output
对于每组测试数据,输出搬运工最少需要推动箱子多少格才能帮箱子推到指定位置,如果不能推到指定位置则输出-1.
 
Sample Input
1
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
 
Sample Output
4
 
 
一开始没考虑到人的初始情况,就是能不能到箱子的后面所以代码很快就写好了。。。
后来发现的,写起来好挺多的,貌似有128行。
 
思路是:先移动箱子,然后考虑能否退一步的位置,人能不能到,能到,则,进行下一步同时更新地图,不能到,就重新移动箱子。
 
双重搜索!!!
 
详见代码
#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue> using namespace std; int n,m,T;
int dir[][]={{,},{,-},{,},{-,}};
int str[][];
struct Node
{
int x,y,step;
int mmap[][];
bool check()
{
if(x>=&&x<n&&y>=&&y<m)
return true;
return false;
}
}s,e,u,v,ss,ee,uu,vv;
bool bfs_people(Node n1)
{ //搜索人是否能到达指定位置
int i;
queue<Node>que;
ss=n1;
bool flag[][];
memset(flag,false,sizeof(flag));
for(i=;i<n;i++)
{ //找到人的起点
for(int j=;j<m;j++)
{
if(n1.mmap[i][j]==)
{
ss.x=i;
ss.y=j;
ss.step=;
}
}
}
if(ss.x==ee.x&&ss.y==ee.y)
return true;
que.push(ss);
flag[ss.x][ss.y]=true;
while(!que.empty())
{
uu=que.front();
que.pop();
for(i=;i<;i++)
{
vv=uu;
vv.step++;
vv.x+=dir[i][];
vv.y+=dir[i][];
if(vv.check()&&flag[vv.x][vv.y]==false&&(n1.mmap[vv.x][vv.y]!=&&n1.mmap[vv.x][vv.y]!=))
{ //目标点不是墙也不是箱子
flag[vv.x][vv.y]=true;
if(vv.x==ee.x&&vv.y==ee.y)
return true;
que.push(vv);
}
}
}
return false;
}
int bfs_box()
{ //搜索箱子
int flag[][][];
queue<Node>Q;
Q.push(s);
memset(flag,false,sizeof(flag));
while(!Q.empty())
{
u=Q.front();
Q.pop();
for(int i=;i<;i++)
{
v=u;
v.x+=dir[i][];
v.y+=dir[i][];
v.step++;
if(v.check()&&str[v.x][v.y]!=&&flag[v.x][v.y][i]==false)
{
//人的目标位置
ee.x=u.x-dir[i][];
ee.y=u.y-dir[i][];
if(ee.check()==false)
continue;
if(bfs_people(v))
{
//更新地图,箱子和人的位置
swap(v.mmap[v.x][v.y],v.mmap[u.x][u.y]);
swap(v.mmap[ee.x][ee.y],v.mmap[ss.x][ss.y]);
flag[v.x][v.y][i]=true;
if(str[v.x][v.y]==)
return v.step;
Q.push(v);
}
}
}
}
return -;
}
int main()
{
int T,i,j;
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&n,&m);
for(i=;i<n;i++)
{
for(j=;j<m;j++)
{
scanf("%d",&str[i][j]);
s.mmap[i][j]=str[i][j];
if(str[i][j]==)
{ //标注箱子起点
s.x=i;
s.y=j;
s.step=;
}
}
}
printf("%d\n",bfs_box());
}
return ;
} /*
60
3 1
3
2
4
3 3
3 0 0
2 0 0
4 0 0
3 3
3 0 1
1 2 1
4 0 1
3 6
1 1 1 1 1 1
0 0 0 1 0 0
0 0 2 4 0 3
5 7
1 1 1 0 1 1 1
1 1 1 0 1 1 1
3 0 0 2 0 0 0
1 1 1 0 1 1 1
1 1 1 4 1 1 1
5 7
1 1 1 0 1 1 1
1 1 1 0 1 1 1
3 0 0 2 0 0 0
1 1 1 0 0 1 1
1 1 1 4 1 1 1
5 7
1 1 1 0 1 1 1
1 1 1 0 1 1 1
4 0 0 2 0 0 0
1 1 1 0 0 1 1
1 1 1 3 1 1 1
5 7
1 1 1 0 1 1 1
1 1 1 0 1 1 1
4 0 0 2 0 0 0
1 1 1 0 0 0 1
1 1 1 3 1 1 1
5 7
1 1 1 0 1 1 1
1 1 1 0 1 1 1
4 3 0 2 0 0 0
1 1 1 0 0 0 1
1 1 1 0 1 1 1
5 7
1 1 1 0 1 1 1
1 1 1 0 0 1 1
4 0 0 2 0 0 0
1 1 1 0 0 0 1
1 1 1 3 1 1 1 Ans:1
Ans:1
Ans:-1
Ans:5
Ans:-1
Ans:3
Ans:-1
Ans:-1
Ans:4
Ans:4
*/

之前的代码先存着,可以当模板。。。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
const int maxn=;
bool vst[maxn][maxn],t;
int map[maxn][maxn];
int dir[][]={-,,,,,-,,} ;
int m,n,fx,fy; struct state
{
int x,y;
int step;
}; int check(int x, int y)
{
if(x< || x>=m || y< || y>=n)
return ;
else
return ;
} void bfs()
{
queue<state>Q;
state now,next;
now.x=fx;
now.y=fy;
now.step=;
t=;
Q.push(now);
vst[now.x][now.y]= ;
while(!Q.empty())
{
now=Q.front();
Q.pop();
if(map[now.x][now.y]==)
{
printf("%d\n",now.step);//找到第一个就输出了
t=;
return ;//结束bfs,返回。,不写的话,会等全部输出之后才停下来的
}
/* if(map[now.x][now.y]=='x')
{
map[now.x][now.y] = '.';
now.step+= 1;
Q.push(now);
continue;
}
*/
else
for(int i=;i<;i++)
{
next.x=now.x+dir[i][];
next.y=now.y+dir[i][]; if(vst[next.x][next.y] || map[next.x][next.y]== || !check(next.x,next.y)||map[now.x-dir[i][]][now.y-dir[i][]]==)
continue;
next.step=now.step+;
Q.push(next);
vst[next.x][next.y]=; } } } int main()
{
int i,j,T;
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&m,&n);
// getchar(); //用scanf,就要先吸收回车;用cin就没必要了
for(i=;i<m;i++)
{
for(j=;j<n;j++)
{
cin>>map[i][j];//scanf("%c",&map[i][j]);为什么最后一列输不进来?
if(map[i][j]==)
{fx=i;fy=j;}
}
// getchar();//用scanf,就要先吸收回车;用cin就没必要了
}
/* for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf("%d",map[i][j]);
}
puts("");
}
*/
memset(vst,,sizeof(vst));
t=;
bfs();
if(!t)
cout <<-<< endl;
}
return ;
}
/*
5
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
5 5
0 3 0 0 0
0 1 1 4 0
0 0 1 0 0
1 0 2 0 0
0 0 0 0 0
5 5
0 3 0 0 1
0 1 1 4 0
0 0 1 0 0
1 0 2 0 0
0 0 0 0 0
5 5
0 3 0 1 0
0 1 1 4 0
0 0 1 0 0
1 0 2 0 0
0 0 0 0 0
4 4
0 0 1 1
0 0 1 1
0 2 3 1
1 4 1 1
*/
 
 
 

推箱子 (hdu1254)(bfs双重广搜)的更多相关文章

  1. HDU1254 推箱子(BFS) 2016-07-24 14:24 86人阅读 评论(0) 收藏

    推箱子 Problem Description 推箱子是一个很经典的游戏.今天我们来玩一个简单版本.在一个M*N的房间里有一个箱子和一个搬运工,搬运工的工作就是把箱子推到指定的位置,注意,搬运工只能推 ...

  2. 推箱子 hdu1254

    推箱子 1  http://acm.hdu.edu.cn/showproblem.php?pid=1254 推箱子 2  http://acm.hzau.edu.cn/problem.php?id=1 ...

  3. HDU 1254 推箱子(BFS加优先队列)

    传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1254 推箱子 Time Limit: 2000/1000 MS (Java/Others)    Me ...

  4. HDU 1254 推箱子(BFS)

    Problem Description 推箱子是一个很经典的游戏.今天我们来玩一个简单版本.在一个M*N的房间里有一个箱子和一个搬运工,搬运工的工作就是把箱子推到指定的位置,注意,搬运工只能推箱子而不 ...

  5. suseoj 1212: 推箱子问题(bfs)

    1212: 推箱子问题 时间限制: 1 Sec  内存限制: 128 MB提交: 60  解决: 13[提交][状态][讨论版][命题人:liyuansong] 题目描述 码头仓库是划分为n×m个格子 ...

  6. AcWing:172. 立体推箱子(bfs)

    立体推箱子是一个风靡世界的小游戏. 游戏地图是一个N行M列的矩阵,每个位置可能是硬地(用”.”表示).易碎地面(用”E”表示).禁地(用”#”表示).起点(用”X”表示)或终点(用”O”表示). 你的 ...

  7. 推箱子 HDU1254 (bfs)

    较难的bfs 有两种方法做 一种双重bfs: 主bfs是箱子   还要通过dfs判断人是否能到箱子后面 用inmap函数的好处.. 箱子要用三位数组来标记  因为箱子可以回到原来到过的地方  因为推的 ...

  8. hdu 2612:Find a way(经典BFS广搜题)

    Find a way Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  9. luoguP4112 [HEOI2015]最短不公共子串 SAM,序列自动机,广搜BFS

    luoguP4112 [HEOI2015]最短不公共子串 链接 luogu loj 思路 子串可以用后缀自动机,子序列可以用序列自动机. 序列自动机是啥,就是能访问到所有子序列的自动机. 每个点记录下 ...

随机推荐

  1. SpringBoot2 配置

    一.Properties与Yaml SpringBoot支持properties与yaml两种配置文件application.properties/application.yml yaml简单使用 1 ...

  2. js判断是否手机自动跳转移动端

    写法一: {literal} <script> //判断是否手机自动跳转 var browser={versions:function(){var u=navigator.userAgen ...

  3. Mac上搭建rtmp流媒体服务器(结合FFmpeg的使用)

    1.确保安装homebrew ---安装则跳到第二步 ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/ins ...

  4. 一步步Cobol 400上手自学入门教程05 - 表

    在COBOL中有几类典型结构的表.这几类典型结构的表在大体上可分为下标表和索引表两大类.另外,根据表的重复次数定义又有定长表和变长表.此外,表还允许嵌套,因此还有嵌套表.这几类表均符合表的基本定义,都 ...

  5. python使用selector模块编写FTP

    server import os import socket import time import selectors BASE_DIR = os.path.dirname(os.path.abspa ...

  6. POJ 2593

    #include <iostream> #include <stdio.h> using namespace std; int cmp ( const void *a , co ...

  7. centos 7 Mysql5.7 主从复制配置

    1.环境 Centos 7 Mysql 5.7 Master  192.168.1.71 Slave01 192.168.1.72 2.分别配置master,slave01 # vi /etc/my. ...

  8. git自己用得着的命令

    -----------随笔记记,给自己备份------------ 1.查看分支 查看当前分支:git branch 查看远程所有分支:git branch -r/git branch -a 2.切换 ...

  9. Spring Boot 基础概述与相关约定配置

    今天打算整理一下 Spring Boot 的基础篇,这系列的文章是我业余时间来写的,起源于之前对微服务比较感兴趣,微服务的范畴比较广包括服务治理.负载均衡.断路器.配置中心.API网关等,还需要结合 ...

  10. 手把手教你封装 Vue 组件并使用 NPM 发布

    Vue 开发插件 我们可以先查看Vue的插件的开发规范 我们开发的之后期望的结果是支持 import.require 或者直接使用 script 标签的形式引入,就像这样: ps: 这里注意一下包的名 ...