WAJUEJI which home strong!nyoj
WAJUEJI which home strong!
- 描述
-
在一个山沟里,姐弟俩同时考上了大学。但由于家里拮据,所以这并不是什么好消息。父亲对孩子说:我就是砸锅卖铁也要把你们姐俩供出来。 当时的姐姐已经决定放弃上学的机会。 没想到第二天天还没亮,弟弟就偷偷带著几件破衣服和几个乾巴馒头走了,在姐姐枕边留下一个纸条: 姐,你别愁了,考上大学不容易,我出去打工供你。弟。 姐姐握著那张字条,趴在炕上,失声痛哭。 那一年,弟弟17岁,姐姐20岁。 姐姐用父亲满村子借的钱和弟弟在工地裏搬水泥挣的钱终於读到了大三。 一天姐姐正在寝室里看书,同学跑进来对姐姐说,有个老乡在找你。姐姐很纳闷,走出去后,远远地看见弟弟,穿著满身是水泥和沙子的工作服。姐姐说,你怎么和我同学说你是我老乡啊? 他笑著说,你看我穿的这样,说是你弟,你同学还不笑话你? 姐姐鼻子一酸,眼泪就落了下来。弟弟赶忙为姐姐擦掉眼泪,说:姐,你别哭,我这次来是想让你帮我打听一下,学挖掘机哪家强?
在你的帮助下,弟弟踏上了去蓝翔的路。
那么问题就来了。

- 输入
- 第一个数T,T组测试数据。
两个数 n, m; ( 0< n , m <= 100 ) 表示一个h行m列的二维地图。
接下来n行每行m 个字符。
‘s’ 表示弟弟目前所在位置。
‘# ’表示此处为一座山。为了节省体力,不从此处通行。
从‘A’-‘Z’表示各地的经济水平,对应1-26,路过对应字符的地区需要交对应的生活费。
‘l’表示蓝翔技校的所在地。
s 与 l 均为小写字母。
弟弟只能走四个方向。 - 输出
- 输出一个数表示弟弟到达蓝翔需要的生活费最小是多少。
如果不能到达,输出 -1。 - 样例输入
-
3 3 5 #sVGF A##ZA lCDBC 3 3 sAB ABS ABl 3 3 s#B ### ABl
- 样例输出
-
48 4 -1
#include <iostream>
#include <cstring>
#include <queue>
#include <cstdio>
using namespace std;int n,m;
char map[105][105];
bool visit[105][105];
int fx[4]= {-1,0,0,1};
int fy[4]= {0,-1,1,0}; //四个方向struct road
{
int x,y,count;//横纵坐标和花费的费用
friend bool operator<(road a,road b)//定义结构体的优先级比较方式
{
return a.count>b.count;
}
}st,tp;void bfs(int X,int Y)
{
priority_queue<road> q;
st.x=X;
st.y=Y;
st.count=0;
q.push(st);
visit[X][Y]=true;
while(!q.empty())
{
st=q.top();
q.pop();
if(map[st.x][st.y]=='s')
{
cout<<st.count-'s'+'A'-1<<endl;
return ;
}for(int i=0; i<4; ++i)
{
int tx=st.x+fx[i];
int ty=st.y+fy[i];
if(tx<0||tx>n-1||ty<0||ty>m-1||map[tx][ty]=='#')
continue;
if(!visit[tx][ty])
{
visit[tx][ty]=true;
tp.x=tx;
tp.y=ty;
tp.count=st.count+map[tx][ty]-'A'+1;
q.push(tp);
}
}
}
cout<<"-1"<<endl;
}int main()
{
int t,X,Y;
cin>>t;
while(t--)
{
cin>>n>>m;
int i,j;
for(i=0; i<n; ++i)
{
getchar();//吸收回车符
for(j=0; j<m; ++j)
{
cin>>map[i][j];if(map[i][j]=='l')//记录终点的坐标
{
X=i;
Y=j;
}
}
}
memset(visit,false,sizeof(visit));
bfs(X,Y);
}
return 0;
}#include<stdio.h>
#include<string.h>
#include<algorithm>
#define Inf 0x3f3f3f3f
using namespace std;int mp[105][105];
int vis[105][105];
int n,m,mint,sum;
int sx,sy,ex,ey;
int next[4][2]={1,0,-1,0,0,1,0,-1};void Getmap()
{
int i,j;
char s[105];
for(i=0;i<n;i++)
{
scanf("%s",s);
for(j=0;j<m;j++)
{
if(s[j]=='#')
mp[i][j]=Inf;
else if(s[j]=='l')
mp[i][j]=0;
else
mp[i][j]=s[j]-'A'+1;if(s[j]=='s')//起点
{
sx=i;
sy=j;
}
else if(s[j]=='l')//终点
{
ex=i;
ey=j;
}
}
}
}//判断是否越界,是否是墙,是否访问过此点
int Is(int x,int y)
{
if(x<0||y<0||x>=n||y>=m)
return 1;
if(vis[x][y]||mp[x][y]==Inf)
return 1;
return 0;
}void Dfs(int x,int y)
{
if(x==ex&&y==ey)
{
mint=min(mint,sum);
return;
}
int i;
for(i=0;i<4;i++)
{
int tx=x+next[i][0];
int ty=y+next[i][1];
if(Is(tx,ty))
continue;
vis[tx][ty]=1;
sum+=mp[tx][ty];
Dfs(tx,ty);
sum-=mp[tx][ty];
vis[tx][ty]=0;
}
}int main()
{
int T;
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&n,&m);
Getmap();
mint=Inf;
vis[sx][sy]=1;
sum=0;
Dfs(sx,sy);
if(mint<Inf)
printf("%d\n",mint);
else
printf("-1\n");
}
return 0;
}
WAJUEJI which home strong!nyoj的更多相关文章
- nyoj 1100 WAJUEJI which home strong!
WAJUEJI which home strong! 时间限制:1000 ms | 内存限制:65535 KB 难度:2 描述 在一个山沟里,姐弟俩同时考上了大学.但由于家里拮据,所以这并不是 ...
- NYoj WAJUEJI which home strong!(简单搜索)
题目链接:http://acm.nyist.edu.cn/JudgeOnline/problem.php?pid=1100 这道题,自己初写搜索,给学长气的只打我,Orz....... 搜索的思路要理 ...
- WAJUEJI which home strong!
算法:搜索+优先队列 描述 在一个山沟里,姐弟俩同时考上了大学.但由于家里拮据,所以这并不是什么好消息.父亲对孩子说:我就是砸锅卖铁也要把你们姐俩供出来. 当时的姐姐已经决定放弃上学的机会. 没想到第 ...
- nyoj--1100--WAJUEJI which home strong!(bfs)
WAJUEJI which home strong! 时间限制:1000 ms | 内存限制:65535 KB 难度:2 描述 在一个山沟里,姐弟俩同时考上了大学.但由于家里拮据,所以这并不是什么 ...
- Weak is not weak,Strong is not strong
问题 今天做浏览器Controller的时候,碰到了一个奇怪的问题:每次pop浏览器controller之后,等几秒,总会碰到类似下面的错误(其中的xxxController就是浏览器或继承他的子类C ...
- datatables使用总结篇
<!doctype html> <html> <head> <meta charset="gbk"/> <meta name= ...
- php学习,一个简单的Calendar(2) 一个简单的活动页面
有了前面的基础,后面就是将页面展示出来. 预览图如下:1号和31号分别有活动,会一并显示出来 这里需要搞定几个问题,一个就是数据库的连接,我们用\sys\class\class.db_connec ...
- Html学习之十一(CSS选择器的应用一)
<!doctype html> <html> <head> <meta charset="utf-8"> <title> ...
- Java调用WebService方法总结(1)--准备工作
WebService是一种跨编程语言.跨操作系统平台的远程调用技术,已存在很多年了,很多接口也都是通过WebService方式来发布的:本系列文章主要介绍Java调用WebService的各种方法,使 ...
随机推荐
- ASP.NET MVC 习惯
- Data Guard Wait Events
This note describes the wait events that monitor the performance of the log transport modes that wer ...
- 015PHP文件处理——文件处理flock 文件锁定 pathinfo realpath tmpfile tempname
<?php /**文件处理flock 文件锁定 pathinfo realpath tmpfile tempname */ /*$arr=pathinfo('ab.txt');//获取文件路径的 ...
- httpclient 连接路由
http路由 httpclient能够直接或通过路由建立连接到目标主机,这会涉及多个中间连接,也被称为跳. Httpclient区分路由和普通连接,通道和分层. 通道连接到目标主机的多个中间代理的使用 ...
- Python 数据库之间差异对比
参考资料: Python 集合(set) 此脚本用于两个数据库之间的表.列.栏位.索引的差异对比. cat oracle_diff.py #!/home/dba/.pyenv/versions/3 ...
- 注解配置定时任务——@Scheduled
Java中注解@Scheduled 的注解代码如下: @Target({ElementType.METHOD, ElementType.ANNOTATION_TYPE}) @Retention(Ret ...
- Installing MIB in Ubuntu and Solving the Error “SNMP Cannot Find Module …”
Has noticed an error after executing the command snmpwalk with the indication of MIB instead of OID: ...
- L235
China will launch the Chang'e-5 probe by the end of this year to bring moon samples back to Earth, a ...
- L233
Betty was offended because she felt that her friends had ignored her purposefully(deliberately) at t ...
- php 文件上传处理
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAS4AAACvCAIAAADMuaTdAAAcaklEQVR4nO2da3Abx33Az3H6JdNx2i