POJ3009-Curling 2.0(WA)
POJ3009-Curling 2.0
题意:
要求把一个冰壶从起点“2”用最少的步数移动到终点“3”
其中0为移动区域,1为石头区域,冰壶一旦想着某个方向运动就不会停止,也不会改变方向(想想冰壶在冰上滑动),除非冰壶撞到石头1 或者 到达终点 3
如果能在10步以内到达终点,输出到达终点所需的步数,否则输出-1
注意的是:
冰壶撞到石头后,冰壶会停在石头前面,此时(静止状态)才允许改变冰壶的运动方向,而该块石头会破裂,石头所在的区域由1变为0. 也就是说,冰壶撞到石头后,并不会取代石头的位置。
终点是一个摩擦力很大的区域,冰壶若到达终点3,就会停止在终点的位置不再移动。
解题思路:
DFS,回溯,利用条件进行优良的剪枝来控制时间
以下代码会WA……想不破哎:-( 暂留
#include<iostream>
using namespace std;
const int maxn = ;
int square[maxn][maxn];
int w,h;
int r0,c0,r2,c2;
int bestp;///最优解
const int dh[] = {-,,,};///向上0,右1,下2,左3
const int dw[] = { ,,,-}; bool inside(int x,int y)
{
return x>= && x <= h && y>= && y<=w;
} bool walk(int x,int y,int dir,int &xx,int &yy)
{///保证不滑到外面去
///遇到墙面,需要改变square
///到达终点,就停止
///如果可行,需要改变当前的位置x,y
int tempx = x+dh[dir];
int tempy = y+dw[dir];
///首先检测当前方位是否可以滑动
if(inside(tempx,tempy) && square[tempx][tempy] == ) return false;///第一步是墙面,不能滑动 while(true)
{
if(!inside(tempx,tempy)) return false;//滑倒了场外
if(square[tempx][tempy] == )//到达了终点
{
xx = tempx;yy = tempy;
return true;
}
if(square[tempx+dh[dir]][tempy+dw[dir]] == )///下一步滑到墙面
{
xx = tempx;yy = tempy;
square[tempx+dh[dir]][tempy+dw[dir]] = ;///改变场地状态
return true;
}
tempx+=dh[dir];tempy+=dw[dir];///划动一格
}
}
void dfs(int x,int y,int deep)
{ ///临界条件
if(x==r2 && y==c2)
{///寻找最优解
if(deep < bestp)
bestp = deep;
return;
}
for(int i=;i<=;i++)
{
int xx,yy;
bool flag = walk(x,y,i,xx,yy);
if(flag && deep<)
dfs(xx,yy,deep+);
///回溯
if(flag && square[xx][yy] != )
{
square[xx+dh[i]][yy+dw[i]] = ;///改变场地状态
} }
return;
} int main()
{
while(cin>>w>>h && w && h)
{
for(int i=;i<=h;i++)///行
for(int j=;j<=w;j++)///列
{
cin>>square[i][j];
if(square[i][j]==)
{
r0=i;c0=j;
square[i][j]=;
}
if(square[i][j]==)
{
r2=i;c2=j;
}
}
bestp = ;
dfs(r0,c0,);
if(bestp>)
{
cout<<-<<endl;
}
else
cout<<bestp<<endl;
}
return ;
}
POJ3009-Curling 2.0(WA)的更多相关文章
- POJ-3009 Curling 2.0 (DFS)
Description On Planet MM-21, after their Olympic games this year, curling is getting popular. But th ...
- POJ3009 Curling 2.0(DFS)
题目链接. 分析: 本题BFS A不了. 00100 00001 01020 00000 00010 00010 00010 00010 00030 对于这样的数据,本来应当是 5 步,但bfs却 4 ...
- POJ3009:Curling 2.0(dfs)
http://poj.org/problem?id=3009 Description On Planet MM-21, after their Olympic games this year, cur ...
- poj3009 Curling 2.0 (DFS按直线算步骤)
Curling 2.0 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 14563 Accepted: 6080 Desc ...
- poj 3009 Curling 2.0 (dfs )
Curling 2.0 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 11879 Accepted: 5028 Desc ...
- poj3009 Curling 2.0(很好的题 DFS)
https://vjudge.net/problem/POJ-3009 做完这道题,感觉自己对dfs的理解应该又深刻了. 1.一般来说最小步数都用bfs求,但是这题因为状态记录很麻烦,所以可以用dfs ...
- ****Curling 2.0(深搜+回溯)
Curling 2.0 Time Limit : 2000/1000ms (Java/Other) Memory Limit : 131072/65536K (Java/Other) Total ...
- PHP Yii2.0(一):环境搭建 & 问题集锦
第一节 简单认识版本的异同 (1)版本说明 在安装和使用之前,我们需要知道 PHP Yii 有两个不同的版本(Yii 1.*或者Yii 2.*),这两个版本的目录结构不一样,其具体使用方式差异较大,因 ...
- ArcGIS API for JavaScript 4.0(一)
原文:ArcGIS API for JavaScript 4.0(一) 最近ArcGIS推出了ArcGIS API for JavaScript 4.0,支持无插件3D显示,而且比较Unity和Sky ...
随机推荐
- 在vue中引用echarts导致Cannot read property getAttribute of null ?
报错信息如下: 之前一直用echarts没有出现过这个问题,所以当这个问题出现时我就开始了各种查,试了几种方法依旧报错,比如: 1.在mounted() {},写成如下形式:(依旧报错) this.$ ...
- 使用openSSL构造一个支持https的nodejs服务器
首先通过下面的链接下载openSSL https://slproweb.com/products/Win32OpenSSL.html 下载完毕后,执行openssl进入交互式界面: 使用命令生成pri ...
- Maven 依赖的作用域
Maven的一个哲学是惯例优于配置(Convention Over Configuration), Maven默认的依赖配置项中,scope的默认值是compile,项目中经常傻傻的分不清,直接默认了 ...
- RHEL6进入救援模式
1.救援模式 救援模式作用: 更改root密码: 恢复硬盘.文件系统操作 系统无法启动时,通过救援模式启动 2.放入系统光盘,重启从光盘启动: 4.选择语言,默认English就行 5.保持默 ...
- 开启aix SFTP日志 是否和链接SFTP有关呢
1.修改SSH配置 vi /etc/ssh/sshd_config 在sftp配置处添加-l INFO -f AUTH Subsystem sftp /usr/lib64/ssh/sftp-serve ...
- gitlab常用的命令
git checkout . #本地所有修改的.没有的提交的,都返回到原来的状态git stash #把所有没有提交的修改暂存到stash里面.可用git stash pop回复.git reset ...
- mysql基础_操作文件中的内容
1.插入数据: insert into t1(id,name) values(1,'alex'); #向t1表中插入id为1,name为'alex'的一条数据 2.删除: delete from t1 ...
- C# 数组(5) 持续更新
同一类型和不同类型的多个对象 使用同一类型的多个对象,使用集合和数组. 使用不同类型的多个对象,使用Tuple(元组). 初始化数组 ]; myArray 存放在栈中,而 myArray 的内容 放在 ...
- C# ado.net 操作(一)
简单的增删改查 class Program { private static string constr = "server=.;database=northwnd;integrated s ...
- macOS上更顺手的终端
安装iTerm2.下载地址 https://iterm2.com/downloads/stable/latest 安装Nerd Fonts.下载地址 https://github.com/ryanoa ...