思路: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 【回家】的更多相关文章

  1. 洛谷 题解 UVA572 【油田 Oil Deposits】

    这是我在洛谷上的第一篇题解!!!!!!!! 这个其实很简单的 我是一只卡在了结束条件这里所以一直听取WA声一片,详细解释代码里见 #include<iostream> #include&l ...

  2. 洛谷 题解 P1600 【天天爱跑步】 (NOIP2016)

    必须得说,这是一道难题(尤其对于我这样普及组205分的蒟蒻) 提交结果(NOIP2016 天天爱跑步): OJ名 编号 题目 状态 分数 总时间 内存 代码 / 答案文件 提交者 提交时间 Libre ...

  3. 洛谷题解P4314CPU监控--线段树

    题目链接 https://www.luogu.org/problemnew/show/P4314 https://www.lydsy.com/JudgeOnline/problem.php?id=30 ...

  4. 洛谷题解 CF777A 【Shell Game】

    同步题解 题目翻译(可能有童鞋没读懂题面上的翻译) 给你三张牌0,1,2. 最初选一张,然后依次进行n次交换,交换规则为:中间一张和左边的一张,中间一张和右边一张,中间一张和左边一张...... 最后 ...

  5. 洛谷题解 CF807A 【Is it rated?】

    同步题解 题目 好吧,来说说思路: 1.先读入啦~(≧▽≦)/~啦啦啦 2.判断a[i]赛前赛后是否同分数,如果分数不同,则输出,return 0 . 3.如果同分数,则判断a[i]赛前(或赛后)是否 ...

  6. 洛谷题解 P1138 【第k小整数】

    蒟蒻发题解了 说明:此题我用的方法为桶排(我翻了翻有人用了桶排只不过很难看出来,可能有些重复的,这个题只是作为一个专门的桶排来讲解吧) (不会算抄袭吧 ‘QWaWQ’) 简单来说(会的人跳过就行): ...

  7. 【洛谷题解】P2303 [SDOi2012]Longge的问题

    题目传送门:链接. 能自己推出正确的式子的感觉真的很好! 题意简述: 求\(\sum_{i=1}^{n}gcd(i,n)\).\(n\leq 2^{32}\). 题解: 我们开始化简式子: \(\su ...

  8. 洛谷题解 P2865 【[USACO06NOV]路障Roadblocks】

    链接:https://www.luogu.org/problemnew/show/P2865 题目描述 Bessie has moved to a small farm and sometimes e ...

  9. 洛谷题解:P1209 【[USACO1.3]修理牛棚 Barn Repair】

    原题传送门:https://www.luogu.org/problemnew/show/P1209 首先,这是一道贪心题.  我们先来分析它的贪心策略.  例如,样例:  4 50 18  3 4 6 ...

随机推荐

  1. java新建excel文件导出(HSSFWorkbook)

    public ActionForward exportExcel(ActionMapping mapping, ActionForm form, HttpServletRequest request, ...

  2. 014_编写批量修改扩展名脚本,如批量将 txt 文件修改为 doc 文件

    #!/bin/bash#执行脚本时,需要给脚本添加位置参数 带参for i in "ls *.$1"do     #去尾     mv $i ${i%.*}.$2done

  3. word如何选择图片粘贴

    自动导入Word图片,或者粘贴Word内容时自动上传所有的图片,并且最终保留Word样式,这应该是Web编辑器里面最基本的一个需求功能了.一般情况下我们将Word内容粘贴到Web编辑器(富文本编辑器) ...

  4. 配置apt源

    vim /etc/apt/source.list 配置完成后执行 apt update apt upgrade 配置完源之后,就可以在源中所有想要的包 apt search xxx 本地自带的源配目录 ...

  5. Bzoj 2134: [国家集训队2011]单选错位(期望)

    2134: 单选错位 Time Limit: 10 Sec Memory Limit: 259 MB Description Input n很大,为了避免读入耗时太多,输入文件只有5个整数参数n, A ...

  6. SDUT2176 -> 递归的函数

    递归的函数                                         Time Limit: 1000 msMemory Limit: 65536 KiB Problem Des ...

  7. keras 模型简介

    keras模型在keras中主要有两种模型,顺序模型,以及模型类(类的内部有函数) model.layers 是层的列表,他们组成了模型 model.inputs 是模型输入的张量 model.out ...

  8. flex的圣杯布局记录 (flex : 0 0 80px)

  9. Ajax简单异步上传图片并回显

    前台代码 上传图片按钮 <a href="javascript:void(0)" onclick="uploadPhoto()">选择图片</ ...

  10. 解决python 保存json到文件时 中文显示16进制编码的问题

    python 2.7 import codecs import json with codecs.open('Options.json', 'w', encoding='utf-8') as f: j ...