洛谷 题解 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 ...
随机推荐
- HDU 6154 - CaoHaha's staff | 2017 中国大学生程序设计竞赛 - 网络选拔赛
/* HDU 6154 - CaoHaha's staff [ 构造,贪心 ] | 2017 中国大学生程序设计竞赛 - 网络选拔赛 题意: 整点图,每条线只能连每个方格的边或者对角线 问面积大于n的 ...
- 自制tomcat镜像
1.编写dockerfile FROM centos MAINTAINER raygift@qq.com # 安装vim RUN yum -y install vim net-tools ENV BA ...
- 连续攻击游戏【P1640洛谷】二分图匹配变形【好题】【每次memset太慢了,用时间戳id。】
lxhgww最近迷上了一款游戏,在游戏里,他拥有很多的装备,每种装备都有2个属性,这些属性的值用[1,10000]之间的数表示.当他使用某种装备时,他只能使用该装备的某一个属性.并且每种装备最多只能使 ...
- 安装包设计-------安装(QT)---------知识总结
1.判断文件是否存在 QFile file(path): file.exists(); 2.复制文件 bool copy(const QString &fileName, const QStr ...
- C到汇编[1]
这篇文字解释这个问题:C语言函数调用在汇编语言是怎么实现的?栈模型很好的满足了函数调用的需求,以最简单的函数调用说明. : int add2(int a, int b){return a+b;} 00 ...
- I Hate It (HDU 1754)
Problem 很多学校流行一种比较的习惯.老师们很喜欢询问,从某某到某某当中,分数最高的是多少. 这让很多学生很反感. 不管你喜不喜欢,现在需要你做的是,就是按照老师的要求,写一个程序,模拟老师 ...
- SpringMVC配置 事务管理
1.确保持久层配置完毕 2.pom.xml里追加spring-tx 3.application-context.xml追加 <bean id="transactionManager&q ...
- Python 不覆盖输入txt 读取txt
不覆盖输入: with open('1.txt','rt')as f: data=f.read() print(data+"\n") 读取txt: with open('1.txt ...
- 如何通过Thread查看一个方法被调用的顺序
Test1 package com.dwz.concurrency.chapter11; public class Test1 { private Test2 test2 = new Test2(); ...
- JavaWeb_(Spring框架)Spring中的aop事务
1.事务相关知识 a)什么是事务:把多条数据库操作捆绑到一起执行,要么都成功,要么都失败: b)事务的原则ACID: i.原子性:事务包含的所有操作,要么全部成功,要么全部失败回滚,成功全部应用到数据 ...