洛谷 题解 P2802 【回家】
思路:DFS+剪枝
本题可以用一个字符二维数组来存整个地图,然后在往四个方向进行搜索。注意:当走到家门前要先判断血量!(本人就被坑了)
代码:
#include<bits/stdc++.h>
using namespace std;
const int N=110;
int n,m;
int Begin_x,Begin_y,End_x,End_y;
int ans=0x7ffffff;
char Map[N][N];
bool t[N][N];
int dx[5]={0,1,-1,0,0},dy[5]={0,0,0,1,-1};
int sm=6;
inline void dfs(int x,int y,int tot)
{
bool four=false;
if(x==End_x&&y==End_y&&sm>0)
{
ans=min(ans,tot);
return;
}
for(int i=1;i<=4;i++)
{
int a=x+dx[i],b=y+dy[i];
if(a>0&&b>0&&a<=n&&b<=m&&(sm-1>0||Map[a][b]=='4')&&Map[a][b]!='0'&&!t[a][b])
{
sm--;
t[x][y]=true;
if(Map[x][y]=='4')sm=6,four=true,Map[x][y]='1';
dfs(a,b,tot+1);
sm++;
t[x][y]=false;
if(four)Map[x][y]='4';
}
}
}
inline int read()
{
int tot=0,f=1;
char c=getchar();
while(c<'0'||c>'9')
{
if(c=='-')f=-1;
c=getchar();
}
while(c>='0'&&c<='9')
{
tot=tot*10+c-'0';
c=getchar();
}
return tot*f;
}
int main()
{
n=read();
m=read();
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
cin>>Map[i][j];
if(Map[i][j]=='2')Begin_x=i,Begin_y=j;
else if(Map[i][j]=='3')End_x=i,End_y=j;
}
}
//cout<<Begin_x<<" "<<Begin_y<<endl<<End_x<<" "<<End_y<<endl;
dfs(Begin_x,Begin_y,0);
if(ans==0x7ffffff)cout<<-1<<endl;
else cout<<ans<<endl;
return 0;
}
信心满满地提交,什么?90分??!
下了第九个测试点,发现小H栽死在家门口了。
修改了半天,改成了这样:
#include<bits/stdc++.h>
using namespace std;
const int N=110;
int n,m;
int Begin_x,Begin_y,End_x,End_y;
int ans=0x7ffffff;
char Map[N][N];
bool t[N][N];
int dx[5]={0,1,-1,0,0},dy[5]={0,0,0,1,-1};
int sm=6;
inline void dfs(int x,int y,int tot)
{
bool four=false;
if(sm<=0||tot>=ans)return;
if(x==End_x&&y==End_y)
{
//cout<<sm<<" "<<tot<<endl;
ans=min(ans,tot);
return;
}
for(int i=1;i<=4;i++)
{
int a=x+dx[i],b=y+dy[i];
if(a>0&&b>0&&a<=n&&b<=m&&Map[a][b]!='0'&&!t[a][b])
{
sm--;
t[x][y]=true;
if(Map[x][y]=='4')sm=6,four=true,Map[x][y]='1';
dfs(a,b,tot+1);
sm++;
t[x][y]=false;
if(four)Map[x][y]='4',sm-=6;
}
}
}
inline int read()
{
int tot=0,f=1;
char c=getchar();
while(c<'0'||c>'9')
{
if(c=='-')f=-1;
c=getchar();
}
while(c>='0'&&c<='9')
{
tot=tot*10+c-'0';
c=getchar();
}
return tot*f;
}
int main()
{
n=read();
m=read();
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
cin>>Map[i][j];
if(Map[i][j]=='2')Begin_x=i,Begin_y=j;
else if(Map[i][j]=='3')End_x=i,End_y=j;
}
}
//cout<<Begin_x<<" "<<Begin_y<<endl<<End_x<<" "<<End_y<<endl;
dfs(Begin_x,Begin_y,0);
if(ans==0x7ffffff)cout<<-1<<endl;
else cout<<ans<<endl;
return 0;
}
这回没问题了吧......
然而只有80分
没有处理好特殊的‘4’
AC代码:
#include<bits/stdc++.h>
using namespace std;
const int N=110;
int n,m;
int Begin_x,Begin_y,End_x,End_y;//存起始坐标与终止坐标
int ans=0x7ffffff;//答案
char Map[N][N];//存地图
bool t[N][N];//判断有没有走过
int dx[5]={0,1,-1,0,0},dy[5]={0,0,0,1,-1};//四个方向
int sm=6;//存生命
inline void dfs(int x,int y,int tot)
{
bool four=false;//判断这格是不是4
if(sm<=0||tot>=ans)return;//剪枝
if(x==End_x&&y==End_y)//更新答案
{
ans=min(ans,tot);
return;
}
for(int i=1;i<=4;i++)//四个方向搜索
{
int a=x+dx[i],b=y+dy[i];
if(a>0&&b>0&&a<=n&&b<=m&&Map[a][b]!='0'&&!t[a][b])
{
sm--;
t[x][y]=true;//设置为已走
int lssm=sm+1;//存一下当前生命+1(以便在回溯时候用)
if(Map[x][y]=='4')sm=6,four=true,Map[x][y]='1';//判断4的情况
dfs(a,b,tot+1);//搜索
sm++;
t[x][y]=false;
if(four)Map[x][y]='4',sm=lssm;
}
}
}
inline int read()//然而加不加都一样
{
int tot=0,f=1;
char c=getchar();
while(c<'0'||c>'9')
{
if(c=='-')f=-1;
c=getchar();
}
while(c>='0'&&c<='9')
{
tot=tot*10+c-'0';
c=getchar();
}
return tot*f;
}
int main()
{
n=read();
m=read();
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
cin>>Map[i][j];
if(Map[i][j]=='2')Begin_x=i,Begin_y=j;
else if(Map[i][j]=='3')End_x=i,End_y=j;
}
}
dfs(Begin_x,Begin_y,0);//DFS
if(ans==0x7ffffff)cout<<-1<<endl;//特判
else cout<<ans<<endl;
return 0;
}
洛谷 题解 P2802 【回家】的更多相关文章
- 洛谷 题解 UVA572 【油田 Oil Deposits】
这是我在洛谷上的第一篇题解!!!!!!!! 这个其实很简单的 我是一只卡在了结束条件这里所以一直听取WA声一片,详细解释代码里见 #include<iostream> #include&l ...
- 洛谷 题解 P1600 【天天爱跑步】 (NOIP2016)
必须得说,这是一道难题(尤其对于我这样普及组205分的蒟蒻) 提交结果(NOIP2016 天天爱跑步): OJ名 编号 题目 状态 分数 总时间 内存 代码 / 答案文件 提交者 提交时间 Libre ...
- 洛谷题解P4314CPU监控--线段树
题目链接 https://www.luogu.org/problemnew/show/P4314 https://www.lydsy.com/JudgeOnline/problem.php?id=30 ...
- 洛谷题解 CF777A 【Shell Game】
同步题解 题目翻译(可能有童鞋没读懂题面上的翻译) 给你三张牌0,1,2. 最初选一张,然后依次进行n次交换,交换规则为:中间一张和左边的一张,中间一张和右边一张,中间一张和左边一张...... 最后 ...
- 洛谷题解 CF807A 【Is it rated?】
同步题解 题目 好吧,来说说思路: 1.先读入啦~(≧▽≦)/~啦啦啦 2.判断a[i]赛前赛后是否同分数,如果分数不同,则输出,return 0 . 3.如果同分数,则判断a[i]赛前(或赛后)是否 ...
- 洛谷题解 P1138 【第k小整数】
蒟蒻发题解了 说明:此题我用的方法为桶排(我翻了翻有人用了桶排只不过很难看出来,可能有些重复的,这个题只是作为一个专门的桶排来讲解吧) (不会算抄袭吧 ‘QWaWQ’) 简单来说(会的人跳过就行): ...
- 【洛谷题解】P2303 [SDOi2012]Longge的问题
题目传送门:链接. 能自己推出正确的式子的感觉真的很好! 题意简述: 求\(\sum_{i=1}^{n}gcd(i,n)\).\(n\leq 2^{32}\). 题解: 我们开始化简式子: \(\su ...
- 洛谷题解 P2865 【[USACO06NOV]路障Roadblocks】
链接:https://www.luogu.org/problemnew/show/P2865 题目描述 Bessie has moved to a small farm and sometimes e ...
- 洛谷题解:P1209 【[USACO1.3]修理牛棚 Barn Repair】
原题传送门:https://www.luogu.org/problemnew/show/P1209 首先,这是一道贪心题. 我们先来分析它的贪心策略. 例如,样例: 4 50 18 3 4 6 ...
随机推荐
- Girls and Boys POJ - 1466 【(二分图最大独立集)】
Problem DescriptionIn the second year of the university somebody started a study on the romantic rel ...
- SQL审核 Inception 中小团队快速构建SQL自动审核系统
SQL审核与执行,作为DBA日常工作中相当重要的一环,一直以来我们都是通过人工的方式来处理,效率低且质量没办法保证.为了规范操作,提高效率,我们决定引入目前市面上非常流行的SQL自动审核工具Incep ...
- 转:关于C++ const 的全面总结
转自:https://www.cnblogs.com/xkfz007/articles/2419518.html 如有侵权请联系博主,立即删除. C++中的const关键字的用法非常灵活,而使用 ...
- 内存管理2-set方法的内存管理-程序解析
创建class Book .h 有@ property float price; //@synthesize 自动 ------------ 创建class Student #import &quo ...
- Node解析之----模块机制篇
开篇前,我们先来看张图, 看node与W3C组织.CommonJS组织.ECMAScript之间的关系. Node借鉴来CommonJS的Modules规范实现了一套非常易用的模块系统,NPM对Pac ...
- HDU 2243 考研路茫茫――单词情结 ——(AC自动机+矩阵快速幂)
和前几天做的AC自动机类似. 思路简单但是代码200余行.. 假设solve_sub(i)表示长度为i的不含危险单词的总数. 最终答案为用总数(26^1+26^2+...+26^n)减去(solve_ ...
- docker 容器连接 host的sql server失败
报错内容::“A network-related or instance-specific error occurred while establishing a connection to SQL ...
- DSSM算法-计算文本相似度
转载请注明出处: http://blog.csdn.net/u013074302/article/details/76422551 导语 在NLP领域,语义相似度的计算一直是个难题:搜索场景下quer ...
- 图的深度优先遍历(DFS)和广度优先遍历(BFS)算法分析
1. 深度优先遍历 深度优先遍历(Depth First Search)的主要思想是: 1.首先以一个未被访问过的顶点作为起始顶点,沿当前顶点的边走到未访问过的顶点: 2.当没有未访问过的顶点时,则回 ...
- 第11组 Beta冲刺(5/5)
第11组 Beta冲刺(5/5) 队名 不知道叫什么团队 组长博客 https://www.cnblogs.com/xxylac/p/12031050.html 作业博客 https://edu. ...