程序设计算法竞赛基础——练习3解题报告

程序设计算法竞赛基础——练习3解题报告1001 A strange liftProblem DescriptionThere is a strange lift.The lift can stop can at every floor as you want, and there is a number Ki(0 <= Ki <= N) on every floor.The li...
摘要由CSDN通过智能技术生成

程序设计算法竞赛基础——练习3解题报告

1001 A strange lift

Problem Description

There is a strange lift.The lift can stop can at every floor as you want, and there is a number Ki(0 <= Ki <= N) on every floor.The lift have just two buttons: up and down.When you at floor i,if you press the button “UP” , you will go up Ki floor,i.e,you will go to the i+Ki th floor,as the same, if you press the button “DOWN” , you will go down Ki floor,i.e,you will go to the i-Ki th floor. Of course, the lift can’t go up high than N,and can’t go down lower than 1. For example, there is a buliding with 5 floors, and k1 = 3, k2 = 3,k3 = 1,k4 = 2, k5 = 5.Begining from the 1 st floor,you can press the button “UP”, and you’ll go up to the 4 th floor,and if you press the button “DOWN”, the lift can’t do it, because it can’t go down to the -2 th floor,as you know ,the -2 th floor isn’t exist.
Here comes the problem: when you are on floor A,and you want to go to floor B,how many times at least he has to press the button “UP” or “DOWN”?

Input

The input consists of several test cases.,Each test case contains two lines.
The first line contains three integers N ,A,B( 1 <= N,A,B <= 200) which describe above,The second line consist N integers k1,k2,…kn.
A single 0 indicate the end of the input.

Output

For each case of the input output a interger, the least times you have to press the button when you on floor A,and you want to go to floor B.If you can’t reach floor B,printf “-1”.

Sample Input

5 1 5
3 3 1 2 5
0

Sample Output

3

解题思路

电梯上升不能超过最高层,下降不能低于1楼,每楼最多只能前往一次以保证最优解

代码实现

#include<iostream>
using namespace std;

const int maxn = 1e6 + 5;

//fl表示该层按钮情况 time存放到达该层所需最小次数
struct INF {
	int fl;
	int time;
};

INF flor[205];
int n;

void search(const int a, const int b) {
	if(a != b) {
		int up = flor[a].fl + a;
		int down = a - flor[a].fl;
		int newtime = flor[a].time + 1;
        //上升层存在且第一次到达
		if(newtime < flor[up].time && up <= n) {
			flor[up].time = newtime;
			search(up, b);
		}
        //下降层存在且第一次到达
		if(newtime < flor[down].time && down >= 1) {
			flor[down].time = newtime;
			search(down, b);
		}
	}
}

int main() {
	int a, b;
	while(cin >> n && n != 0) {
		cin >> a >> b;
		for(int i = 1; i <= n; i++) {
			cin >> flor[i].fl;
			flor[i].time = maxn;
		}
		flor[a].time = 0;
		search(a, b);
		if(flor[b].time == maxn) cout << -1 << endl;
		else cout << flor[b].time << endl;
	}
	return 0;
}

1002 A计划

Problem Description

可怜的公主在一次次被魔王掳走一次次被骑士们救回来之后,而今,不幸的她再一次面临生命的考验。魔王已经发出消息说将在T时刻吃掉公主,因为他听信谣言说吃公主的肉也能长生不老。年迈的国王正是心急如焚,告招天下勇士来拯救公主。不过公主早已习以为常,她深信智勇的骑士LJ肯定能将她救出。
现据密探所报,公主被关在一个两层的迷宫里,迷宫的入口是S(0,0,0),公主的位置用P表示,时空传输机用#表示,墙用*表示,平地用.表示。骑士们一进入时空传输机就会被转到另一层的相对位置,但如果被转到的位置是墙的话,那骑士们就会被撞死。骑士们在一层中只能前后左右移动,每移动一格花1时刻。层间的移动只能通过时空传输机,且不需要任何时间。

Input

输入的第一行C表示共有C个测试数据,每个测试数据的前一行有三个整数N,M,T。 N,M迷宫的大小NM(1 <= N,M <=10)。T如上所意。接下去的前NM表示迷宫的第一层的布置情况,后N*M表示迷宫第二层的布置情况。

Output

如果骑士们能够在T时刻能找到公主就输出“YES”,否则输出“NO”。

Sample Input

1
5 5 14
S*#*.
.#...
.....
****.
...#.

..*.P
#.*..
***..
...*.
*.#..

Sample Output

YES

Source

HDU 2007-6 Programming Contest

解题思路

用一个三维数组fl2 [12] **[12]**储存迷宫地图,**fl[0] [ ] [ ]**存放第一层迷宫地图, **fl1 [ ] [ ]**存放第二层迷宫地图

每次经过一个点就将该点变成墙,保证每次到达某个点都是最优情况

如果传送门对面也是传送门或是墙则不能使用该传送门

代码实现

#include<queue>
#include<iostream>
using namespace std;

//kase为迷宫地面状况,times表示到达该点所用时间,x,y,z存放改格迷宫所在位置(为que队列服务)
struct INF {
	char kase;
	int times;
	int x, y, z;
};

//用于读入迷宫地图,防止读入空格换行等
char mygetchar() {
	char ch;
	while(true) {
		cin >> ch;
		if(ch == 'S' || ch == '*' || ch == '.' || ch == 'P' || ch == '#') return ch;
	}
}

//清空队列
void clearqueue(queue&l
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值