上蓝翔
2015-08-18 12:47
465 查看
废话不多说,我要上蓝翔。
‘s’ 表示起始所在位置。
‘# ’表示此处为一座山。为了节省体力,不从此处通行。
从‘A’-‘Z’表示各地的经济水平,对应1-26,路过对应字符的地区需要交对应的生活费。
‘l’表示蓝翔技校的所在地。
s 与 l 均为小写字母。
问最少消费是多少
输入
输入包括多组测试数据,每组两个数 n, m; ( 0< n , m <= 100 ) 表示一个h行m列的二维地图。
接下来n行每行m 个字符。
输出
输出一个数表示我到达蓝翔需要的生活费最小是多少。
如果不能到达,输出 -1。
样例输入
3 5
#sVGF
A##ZA
lCDBC
样例输出
48
广搜用来求最有解,深搜求是否有解以及解有多少种方式
‘s’ 表示起始所在位置。
‘# ’表示此处为一座山。为了节省体力,不从此处通行。
从‘A’-‘Z’表示各地的经济水平,对应1-26,路过对应字符的地区需要交对应的生活费。
‘l’表示蓝翔技校的所在地。
s 与 l 均为小写字母。
问最少消费是多少
输入
输入包括多组测试数据,每组两个数 n, m; ( 0< n , m <= 100 ) 表示一个h行m列的二维地图。
接下来n行每行m 个字符。
输出
输出一个数表示我到达蓝翔需要的生活费最小是多少。
如果不能到达,输出 -1。
样例输入
3 5
#sVGF
A##ZA
lCDBC
样例输出
48
广搜用来求最有解,深搜求是否有解以及解有多少种方式
#include<cstdio> #include<queue> #include<cstring> #include<algorithm> using namespace std; int u[]={0,1,-1,0}; int v[]={1,0,0,-1}; char map[120][120]; int w[120][120]; struct stu { int x,y; int step; friend bool operator<(stu a,stu b)//不要忘记写friend与< { return a.step>b.step; } }now,next; int n,m,x1,y1,x2,y2,flag; int can(int x,int y) { if(x<0||x>=n||y<0||y>=m)//先写return 0的情况吧,写成别的形式return 1会出错 return 0; if(w[x][y]||map[x][y]=='#') return 0; return 1; } int bfs(int x,int y) { memset(w,0,sizeof(w)); priority_queue<stu>q; now.x=x; now.y=y; now.step=0; q.push(now);//不可以在这里加q.pop() w[x][y]=1; flag=0; while(!q.empty()) { now=q.top(); q.pop(); if(now.x==x2&&now.y==y2) { flag=1; return now.step; } for(int i=0;i<4;i++) { next.x=now.x+u[i]; next.y=now.y+v[i]; if(can(next.x,next.y)) { if(map[next.x][next.y]>='A'&&map[next.x][next.y]<='Z') next.step=now.step+map[next.x][next.y]-'A'+1; else next.step=now.step+0;//到终点时用 w[next.x][next.y]=1; q.push(next); } } } } int main() { while(~scanf("%d%d",&n,&m)) { int i,j,s; for(i=0;i<n;i++) { getchar(); for(j=0;j<m;j++) { scanf("%c",&map[i][j]); if(map[i][j]=='s') { x1=i; y1=j; } else if(map[i][j]=='l') { x2=i; y2=j; } } } s=bfs(x1,y1); if(flag) printf("%d\n",s); else printf("-1\n"); } return 0; }
相关文章推荐
- [LeetCode] Two Sum
- 使用grep实现精确过滤的五种方法
- 使用grep实现精确过滤的五种方法
- 基于Java的拼图游戏
- poj 2001
- 通过SSH连接linux服务器
- PHP 一致性哈希算法的一种简单实现
- 数组实现邻接表
- iOS之 Category 属性 的理解
- C#实现将类的内容写成JSON格式字符串的方法
- WCF和ASP.NET Web API
- PDF转换成txt文档的转换方法
- 补:小玩文件2--将文本文件中的所有行加上行号后写到新文件中
- Linux程序设计(二)shell程序设计
- 文件特殊属性:SUID/SGID/Sticky
- 关于对x5平台的总结(摘自X5论坛)
- centos编译内核:no space left on device 解
- 欢迎使用CSDN-markdown编辑器
- Fragment+viewpagerIndicator第三方控件实现APP主界面
- 用其他用户的身份执行命令:sudo