编程介的小学生 2019-09-07 21:57 采纳率: 20.5%
浏览 203

C语言转弯的问题,怎么用代码实现的,No Left Turns

Problem Description

ALL HEADS: You're a Knight of the Round Table?

ROBIN: I am.

LEFT HEAD: In that case I shall have to kill you.

MIDDLE HEAD: Shall I?

RIGHT HEAD: Oh, I don't think so.

MIDDLE HEAD: Well, what do I think?

LEFT HEAD: I think kill him.

RIGHT HEAD: Well let's be nice to him.

MIDDLE HEAD: Oh shut up.

As the story goes, the Knight scarpers off. Right Head has taken it upon himself to search the grounds for the knight so he, Left, and Middle can go extinguish him (and then have tea and biscuits.)

Consider the following 8 by 12 maze, where shaded squares are walls that can’t be entered.

The shortest path between the Right Head (denoted by the S, for start) and the knight (denoted by the F, for finish) is of length 3, as illustrated above. But! Right Head can’t turn left or make UTurns. He can only move forward and turn right. That means the shortest path that Right Head can find is significantly longer: at 29!

Input
The input file will consist of a single integer N (N > 0) specifying the number of mazes in the file. Following this, on a maze by maze basis will be the number of rows, r (3 < r <= 20), a space, then the number of columns, c (3 < c <= 20). After this will follow r lines of c characters, representing a map of the maze:

XXXXXXXXXXXXXX
X XXX
X XFXXXXX X
XXX XX XX X
X S X
XX XXXXXX X X
X X X X
X X X X X
XXX XX X
XXXXXXXXXXXXXX

X’s mark those locations that are walls and can’t be occupied. S marks the start location, and F marks the Knight. Blanks are locations that can be freely traveled.

Output
The output is the length of the shortest path between the start and finish locations. Based on the above maze, your program would output the minimum no-left-turns path length of 29.

Hint

Additional Constraints/Information:
Right Head is capable of moving from the start position in any of the four primary compass directions. After that, he’s constrained to either step forward or right.

The start and end locations will never be the same.

The maze is always surrounded by four walls.

You can assume that a path between the start and final locations always exists.

Sample Input
1
10 14
XXXXXXXXXXXXXX
X XXX
X XFXXXXX X
XXX XX XX X
X S X
XX XXXXXX X X
X X X X
X X X X X
XXX XX X
XXXXXXXXXXXXXX

Sample Output
29

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥100 Jenkins自动化部署—悬赏100元
    • ¥15 关于#python#的问题:求帮写python代码
    • ¥20 MATLAB画图图形出现上下震荡的线条
    • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
    • ¥15 perl MISA分析p3_in脚本出错
    • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
    • ¥15 ubuntu虚拟机打包apk错误
    • ¥199 rust编程架构设计的方案 有偿
    • ¥15 回答4f系统的像差计算
    • ¥15 java如何提取出pdf里的文字?