poj1475Pushing Boxes
Time Limit:2000MS Memory Limit:131072KB 64bit IO Format:%I64d & %I64u
Description
One of the empty cells contains a box which can be moved to an adjacent free cell by standing next to the box and then moving in the direction of the box. Such a move is called a push. The box cannot be moved in any other way than by pushing, which means that if you push it into a corner you can never get it out of the corner again.
One of the empty cells is marked as the target cell. Your job is to bring the box to the target cell by a sequence of walks and pushes. As the box is very heavy, you would like to minimize the number of pushes. Can you write a program that will work out the best such sequence?
Input
Following this are r lines each containing c characters. Each character describes one cell of the maze. A cell full of rock is indicated by a `#' and an empty cell is represented by a `.'. Your starting position is symbolized by `S', the starting position of the box by `B' and the target cell by `T'.
Input is terminated by two zeroes for r and c.
Output
Otherwise, output a sequence that minimizes the number of pushes. If there is more than one such sequence, choose the one that minimizes the number of total moves (walks and pushes). If there is still more than one such sequence, any one is acceptable.
Print the sequence as a string of the characters N, S, E, W, n, s, e and w where uppercase letters stand for pushes, lowercase letters stand for walks and the different letters stand for the directions north, south, east and west.
Output a single blank line after each test case.
Sample Input
- 1 7
- SB....T
- 1 7
- SB..#.T
- 7 11
- ###########
- #T##......#
- #.#.#..####
- #....B....#
- #.######..#
- #.....S...#
- ###########
- 8 4
- ....
- .##.
- .#..
- .#..
- .#.B
- .##S
- ....
- ###T
- 0 0
Sample Output
- Maze #1
- EEEEE
- Maze #2
- Impossible.
- Maze #3
- eennwwWWWWeeeeeesswwwwwwwnNN
- Maze #4
- swwwnnnnnneeesssSSS
- 很经典的双重搜索,要注意,当箱子的步数相等时,人走的步数要越少
- #include<iostream>
- #include<stdio.h>
- #include<string>
- #include<string.h>
- using namespace std;
- int n,bp,m,bx,by,add,maxbox,maxstep,allstep,ret,gx,gy,rx,ry,map[25][25],boxdir[4][2]={{0,-1},{1,0},{0,1},{-1,0}},visit[25][25],peoplevisit[25][25];
- struct tree2{int i,bp,add;}setqueue[4];
- struct tree{int pre,step,allstep,bx,by,rx,ry;string re;}boxq[60000],peopleq[60000];
- char peoplewesn(int i)
- {
- if(i==0)
- return 'w';
- else if(i==1)
- return 's';
- else if(i==2)
- return 'e';
- else if(i==3)
- return 'n';
- return 'a';
- }
- int peoplebfs(int sx,int sy,int ex,int ey)
- {
- memset(peoplevisit,0,sizeof(peoplevisit));
- int t,w,x,y,i,xx,yy;
- t=w=1;
- peopleq[1].step=0;
- peopleq[1].pre=-1;
- peopleq[1].allstep=0;
- peopleq[1].bx=ex;//在peopleq中用bx by存人要走的地方
- peopleq[1].by=ey;
- peopleq[1].rx=sx;
- peopleq[1].ry=sy;
- peopleq[1].re="";
- peoplevisit[sx][sy]=1;
- if(sx==ex&&sy==ey)
- {
- bp=1;
- add=2;
- return 2;
- }
- while(t<=w)
- {
- x=peopleq[t].rx;
- y=peopleq[t].ry;
- if(x==ex&&y==ey)
- {
- bp=t;
- //printf("true");
- add=1;
- return 1;
- }
- for(i=0;i<4;i++)
- {
- xx=x+boxdir[i][0];//人和箱子一个方向
- yy=y+boxdir[i][1];
- if((!peoplevisit[xx][yy])&&(map[xx][yy]==0)&&(xx>=1)&&xx<=n&&yy>=1&&yy<=m)
- {
- // printf("%d i\n",i);
- peoplevisit[xx][yy]=1;//标记为访问
- peopleq[++w].pre=t;
- peopleq[w].rx=xx;
- peopleq[w].ry=yy;
- peopleq[w].bx=ex;
- peopleq[w].by=ey;
- peopleq[w].re=peopleq[t].re+peoplewesn(i);
- peopleq[w].step=peopleq[t].step+1;
- }
- }
- t++;
- // printf("%d w%d\n",t,w);
- }
- // printf("%d %d false\n",t,w);
- add=0;
- return 0;
- }
- bool boxcan(int x,int y,int i,int tt)//判断是否能走,并搜索人
- {
- int xx,yy;
- xx=x+boxdir[i][0];
- yy=y+boxdir[i][1];
- map[x][y]=1;
- if((visit[xx][yy]==0)&&(map[xx][yy]==0)&&xx>=1&&xx<=n&&yy>=1&&yy<=m&&peoplebfs(boxq[tt].rx,boxq[tt].ry,x-boxdir[i][0],y-boxdir[i][1]))
- {
- // printf("%d %d tt%d",tt,boxq[tt].rx,boxq[tt].ry);
- visit[xx][yy]=1;//标记为已访问
- map[x][y]=0;
- return true;
- }
- map[x][y]=0;
- return false ;
- }
- char boxwesn(int i)//判断箱子走的方向
- {
- if(i==0)
- return 'W';
- else if(i==1)
- return 'S';
- else if(i==2)
- return 'E';
- else if(i==3)
- return 'N';
- return 'a';
- }
- int boxbfs(int sx,int sy,int ex,int ey)//找箱子的最短路
- {
- memset(visit,0,sizeof(visit));
- int t,w,x,y,i;
- t=w=1;
- boxq[1].pre=-1;
- boxq[1].step=0;
- boxq[1].bx=sx;
- boxq[1].by=sy;
- visit[sx][sy]=1;
- boxq[1].re="";
- boxq[1].rx=rx;
- boxq[1].ry=ry;
- boxq[1].allstep=0;
- maxbox=-1;
- if(sx==ex&&sy==ey)
- {
- return 2;
- }
- while(t<=w)
- {
- x=boxq[t].bx;
- y=boxq[t].by;
- if(ret==2&&boxq[t].step>maxstep)
- {
- return maxbox;
- }
- if(x==ex&&y==ey)//找到结果,但这里不能停,要把所有的是最小步数的搜出来,并把最小人走的数输出来
- {
- if(ret==1)
- {
- ret=2;
- maxstep=boxq[t].step;
- allstep=boxq[t].allstep;
- maxbox=t;
- }
- if(ret==2&&boxq[t].allstep<allstep)
- {
- maxbox=t;
- allstep=boxq[t].allstep;
- }
- }
- int j;
- for(i=0,j=0;i<4;i++)//4个方向搜
- {
- if(boxcan(x,y,i,t))
- {
- {
- w++;
- boxq[w].pre=t;
- boxq[w].bx=x+boxdir[i][0];
- boxq[w].by=y+boxdir[i][1];
- boxq[w].rx=x;
- boxq[w].ry=y;
- boxq[w].step=boxq[t].step+1;
- boxq[w].allstep=boxq[t].allstep+peopleq[bp].step;
- if(add==2)//判定是否跟着箱子走
- boxq[w].re=boxq[t].re+boxwesn(i);////结果保存下来
- else if(add==1)
- boxq[w].re=boxq[t].re+peopleq[bp].re+boxwesn(i);
- }
- }
- }
- t++;
- }
- if(ret==2)//如果有了最小值要标记下来
- return 1;
- return -1;//不可达到
- }
- int main ()
- {
- int i,j,tcase=1;
- char c,str[200];
- while(scanf("%d%d",&n,&m)!=EOF)
- {
- getchar();
- ret=1;maxbox=-1;maxstep=-1;allstep=-1;
- if(n==0&&m==0)
- break;
- for(i=1;i<=n;i++)
- {
- for(j=1;j<=m;j++)
- {
- c=getchar();
- if(c=='#')
- map[i][j]=1;
- else if(c=='.')
- map[i][j]=0;
- else if(c=='B')
- {
- map[i][j]=0;
- bx=i;
- by=j;
- }
- else if(c=='T')
- {
- map[i][j]=0;
- gx=i;
- gy=j;
- }
- else if(c=='S')
- {
- map[i][j]=0;
- rx=i;
- ry=j;
- }
- }
- gets(str);
- }
- printf("Maze #%d\n",tcase++);
- if(boxbfs(bx,by,gx,gy)==-1)
- {
- printf("Impossible.\n\n");
- }
- else
- {
- cout<<boxq[maxbox].re<<endl<<endl;
- }
- }
- return 0;
- }
poj1475Pushing Boxes的更多相关文章
- POJ-1475-Pushing Boxes(BFS)
Description Imagine you are standing inside a two-dimensional maze composed of square cells which ma ...
- Fedora 24 Gnome Boxes 无法ping通网络
安装Fedora 24在试用虚拟机时发现无法ping通外网. 我傻傻地以为是软件问题. 问题描述: 尝试ping程序来测试网络连通性: (我之前也是ping百度,后来在为了少打字百度了一些比较短的域名 ...
- Problem B Boxes in a Line
省赛B题....手写链表..其实很简单的.... 比赛时太急了,各种手残....没搞出来....要不然就有金了...注:对相邻的元素需要特判..... Problem B Boxes in a Li ...
- Codeforces Round #229 (Div. 2) C. Inna and Candy Boxes 树状数组s
C. Inna and Candy Boxes Inna loves sweets very much. She has n closed present boxes lines up in a ...
- boxes
boxes [英][bɒksɪz][美][bɑ:ksɪz] n.盒( box的名词复数 ); 一盒; 电视; 小亭; v.把…装入盒[箱,匣]中( box的第三人称单数 ); 拳击; 以上结果来自 ...
- Brute Force - B. Candy Boxes ( Codeforces Round #278 (Div. 2)
B. Candy Boxes Problem's Link: http://codeforces.com/contest/488/problem/B Mean: T题目意思很简单,不解释. ana ...
- UVa 103 - Stacking Boxes(dp求解)
题目来源:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=3&pa ...
- [CareerCup] 9.10 Stack Boxes 垒箱子问题
9.10 You have a stack of n boxes, with widths w., heights hir and depths drThe boxes cannot be rotat ...
- 北京网络赛G BOXES 状态压缩+有序BFS+高维数组判重
#include <bits/stdc++.h> using namespace std; ]; ][]; ][][]; ][][][]; ][][][][]; ][][][][][]; ...
随机推荐
- 使用SQL Server Driver for PHP解决PHP连接MSSQL乱码的问题
原文 使用SQL Server Driver for PHP解决PHP连接MSSQL乱码的问题 最近帮客户写了一个.net商城网站的发布接口,大家都知道.net一般都使用MSSQL数据库,但鱼丸不会. ...
- 谈谈那些年PHP中屌屌的验证码
验证码已经是现在网站中非常基础的知识点了,验证码的存在可以防止恶意破解密码.刷票.灌水,可以有效的防止暴力破解特定用户. 现在就来了解了解那些年PHP中屌屌的验证码吧. 首先,以四位验证码为例(多位验 ...
- MVC+Bootstrap设计
MVC+Bootstrap) 二 框架设计 文章目录: 一.前言 二.结构图 三.项目搭建 四.代码生成 五.实现接口 六.依赖倒置 七.登录实现 八.最后 一.前言 这个框架是从最近几年做过的项目中 ...
- leetcode第24题--Reverse Nodes in k-Group
problem: Given a linked list, reverse the nodes of a linked list k at a time and return its modified ...
- leetcode第三题--Longest Substring Without Repeating Characters
Problem:Given a string, find the length of the longest substring without repeating characters. For e ...
- leetcode[159] Longest Substring with At Most Two Distinct Characters
找到最多含有两个不同字符的子串的最长长度.例如:eoeabc,最长的是eoe为3,其他都为2. 思路: 用p1,p2表示两种字符串的最后一个出现的下标位置.初始p1为0. p2为-1.start初始化 ...
- C注意,使用的语言字符串
转载请注明出处! 在C语言没有具体的字符串数据类型,字符串的字符串常量和字符数组的形式. 实际上该字符串是零个或更多字符的字符串.并在整个位模式0NUL字节结束.因此,字符串所包括的字符内部不能出现N ...
- ASP.NET MVC项目
ASP.NET MVC项目里创建一个aspx视图 先从控制器里添加视图 视图引擎选"ASPX(C#)",使用布局或模板页不要选. 在Views\EAV目录里,生成的aspx是个单独 ...
- 个推A/B测试评测
A/B测试在各类网站设计中已经是比较常见的,本文着重讲讲A/B测试在应用推送领域的作用. 目前国外开通A/B测试的推送服务商只有swrve,而国内的个推也在前不久发布的smart push 2.0中集 ...
- windows服务安装启动报错误1053:服务没有及时响应启动或控制请求
<startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0&qu ...