ZUST 程序设计算法竞赛基础【3】题解报告

本文是ZUST程序设计算法竞赛的基础题解,包括1001至1006共6个题目。内容涉及电梯问题、迷宫寻路、N皇后问题、数独、石油区块和旋转游戏等算法题目,给出了问题描述、分析和解题思路,并提供了部分题目的代码实现。
摘要由CSDN通过智能技术生成

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

题解

题目大意

电梯每层有一个不同的数字,例如第n层有个数字k,那么这一层只能上k层或下k层,但是不能低于一层或高于n层,给定起点与终点,要求出最少要按几次键

分析

一、搜索:搜索的话用一维的bfs就好,需判断楼层大于0的情况和<n的情况;
二、最短路:构图,将从i层到按动按钮后跳转的楼层,看作连通状态,赋值为1,这样就转换成单源最短路问题;要注意的是,这道题是单向边,我们只要让map里面的值全部为1就可以统计次数了

代码

#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
const int inf = 1<<30;
int n;
int map[205][205];
int a[205],cnt;
int vis[205],cast[205];
void Dijkstra(int s,int e){
    int i,j,min,pos;
    memset(vis,0,sizeof(vis));
    for(i = 0; i<n; i++)
        cast[i] = map[s][i];
    cast[s] = 0;
    vis[s] = 1;
    for(i = 1; i<n; i++){
        min = inf;
        for(j = 0; j<n; j++){
            if(cast[j]<min && !vis[j]){
                pos = j;
                min = cast[j];
            }
        }
        if(min == inf) break;
        vis[pos] = 1;
        for(j = 0; j<n; j++){
            if(cast[pos]+map[pos][j]<cast[j] && !vis[j])
                cast[j] = cast[pos]+map[pos][j];
        }
    }
}
int main(){
    int i,j,s,e,x,y;
    while(~scanf("%d",&n),n){
        scanf("%d%d",&s,&e);
        s--,e--;
        for(i = 0; i<n; i++)
            for(j = 0; j<n; j++)
                map[i][j] = inf;
        for(i = 0; i<n; i++){
            scanf("%d",&a[i]);
            if(i+a[i]<n)
                map[i][i+a[i]] = 1;
            if(i-a[i]>=0)
                map[i][i-a[i]] = 1;
        }
        Dijkstra(s,e);
        printf("%d\n",cast[e]==inf?-1:cast[e]);
    }
    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
#.

**
.
*.#…

Sampl

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值