POJ P3009 Curling 2.0 题解
深搜,向四个方向,在不越界的情况下一直闷头走,直到撞墙。到达终点就输出,没到就回溯。
#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
using namespace std;
int n,m;
int map[][];
int xxx,yyy;
int ans=;
int tx[]={,-,,};
int ty[]={,,,-};
void dfs(int x,int y,int step)
{
if(step>=ans||step>=)
{
return;
}
for(int i=;i<;i++)
{
int sx=x+tx[i];
int sy=y+ty[i];
if(map[sx][sy]==)continue;
while(sx>=&&sy>=&&sx<n&&sy<m&&map[sx][sy]!=&&map[sx][sy]!=)
{
sx+=tx[i];
sy+=ty[i];
}
if(sx<||sy<||sx==n||sy==m)continue;
else
{
if(map[sx][sy]==)
{
ans=step+;
return;
}
else
{
map[sx][sy]=;
dfs(sx-tx[i],sy-ty[i],step+);
map[sx][sy]=;
}
}
}
}
int main()
{
while(cin>>m>>n&&n!=&&m!=)
{
ans=;
for(int i=;i<n;i++)
{
for(int j=;j<m;j++)
{
cin>>map[i][j];
if(map[i][j]==)
{
xxx=i;
yyy=j;
}
}
}
dfs(xxx,yyy,);
if(ans==) cout<<-<<endl;
else cout<<ans<<endl;
}
}
请各位大佬斧正(反正我不认识斧正是什么意思)
POJ P3009 Curling 2.0 题解的更多相关文章
- poj 3009 Curling 2.0 (dfs )
Curling 2.0 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 11879 Accepted: 5028 Desc ...
- POJ 3009 Curling 2.0 {深度优先搜索}
原题 $On Planet MM-21, after their Olympic games this year, curling is getting popular. But the rules ...
- POJ 3009 Curling 2.0【带回溯DFS】
POJ 3009 题意: 给出一个w*h的地图,其中0代表空地,1代表障碍物,2代表起点,3代表终点,每次行动可以走多个方格,每次只能向附近一格不是障碍物的方向行动,直到碰到障碍物才停下来,此时障碍物 ...
- poj 3009 Curling 2.0
题目来源:http://poj.org/problem?id=3009 一道深搜题目,与一般搜索不同的是,目标得一直往一个方向走,直到出界或者遇到阻碍才换方向. 1 #include<iostr ...
- POJ 3009 Curling 2.0(DFS + 模拟)
题目链接:http://poj.org/problem?id=3009 题意: 题目很复杂,直接抽象化解释了.给你一个w * h的矩形格子,其中有包含一个数字“2”和一个数字“3”,剩下的格子由“0” ...
- POJ 3009 Curling 2.0 回溯,dfs 难度:0
http://poj.org/problem?id=3009 如果目前起点紧挨着终点,可以直接向终点滚(终点不算障碍) #include <cstdio> #include <cst ...
- poj 3009 Curling 2.0( dfs )
题目:http://poj.org/problem?id=3009 参考博客:http://www.cnblogs.com/LK1994/ #include <iostream> #inc ...
- 【POJ】3009 Curling 2.0 ——DFS
Curling 2.0 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 11432 Accepted: 4831 Desc ...
- POJ 3009:Curling 2.0 推箱子
Curling 2.0 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 14090 Accepted: 5887 Desc ...
随机推荐
- Jupyter notebook 安装
一.建议从官网下载最新版anaconda https://www.anaconda.com/ 进入网址找到下载位置,并找到对应的版本,下载python3.7,根据电脑系统自行选择32/64位进行下载, ...
- Django-12-auth认证组件
1. 介绍 我们在开发一个网站的时候,无可避免的需要设计实现网站的用户系统.此时我们需要实现包括用户注册.用户登录.用户认证.注销.修改密码等功能. Django作为一个完美主义者的终极框架,当然也会 ...
- 使用码云或GitHub搭建简单的个人网站
视频链接:https://www.bilibili.com/video/av64294697 码云: 1.新建一个仓库 路径名会影响你的个人网站的网址(自行修改),开源(公开),其他默认 ...
- coco2dx--Permission denied
在终端输入./cocos.py....创建项目时,出现Permission denied,是权限问题,可以先使用chmod命令获得权限,输入chmod u+x ./cocos.py 回车,接着再使用c ...
- window 包管理器--Chocolatey
Chocolatey 介绍 在 Linux 下,大家喜欢用 apt-get 来安装应用程序,如今在 windows 下,大家可以使用 Chocolatey 来快速下载搭建一个开发环境. Chocola ...
- 浮动IP地址(Float IP)与 ARP欺骗技术
浮动IP地址: 一个网卡是可以添加多个IP的. 就是多个主机工作在 同一个集群中,即两台主机以上.每台机器除了自己的实IP外,会设置一个浮动IP,浮动IP与主机的服务(HTTP服务/邮箱服务)绑在一起 ...
- int转换为String,常用的四种方法。
int i = 100; 方法一:String s1 = i + " "; 方法二:String s2 = String.valueof(i); 方法三(先转换为Integer类型 ...
- iOS开发,导入c文件引发的Could not build module 'UIKit'问题
导致出现异常的原因是是因为工程中添加了某(第三方开源库) 一般情况下出现“Unknown type name”是头文件互相引用出现的,这里可以排除,由于源码使用是c\c++与oc混编, 考虑新的XCo ...
- 纯css实现省略号,兼容火狐,IE9,chrome
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- session有效期设置的两种方式
/**session有效期设置的两种方式: * 1.代码设置:session.setMaxInactiveInterval(30);//单位:秒.30秒有效期,默认30分钟. * 2.web.xml中 ...