斯坦福大学开放课程——编程方法 作业1-2(附作业中文翻译及源码)【修复支撑拱门】

Problem 2 

Karel has been hired to repair the damage done to the Quad in the 1989 earthquake. In particular, Karel is to repair a  set of arches where some of the stones (represented by beepers, of course) are missing from the columns supporting the arches, as follows:

Your program should work on the world shown above, but it should be general enough to handle any world that meets certain basic conditions as outlined at the end of this problem. There are several example worlds in the starter folder, and your program should work correctly with all of them. 

When Karel is done, the missing stones in the columns should be replaced by beepers, so that the final picture resulting from the world shown above would look like this: 

Karel may count on the following facts about the world, list on the next page: 

  • Karel starts at 1st Avenue and 1st Street, facing east, with  an infinite number of beepers. 
  • The columns are exactly four units apart, on 1st, 5th, 9th Avenue, and so forth. 
  • The end of the columns is marked by a wall immediately after the final column. This wall section appears after 13th Avenue in the example, but your program should work for any number of columns. 
  • The top of the column is marked by a wall, but Karel cannot assume that columns are always five units high, or even that all columns are the same height. 
  • Some of the corners in the column may already contain beepers representing stones that are still in place. Your program should not put a second beeper on these corners.

****************翻译分割线************

习题二:
卡雷尔被雇去修复1989 年大地震对斯坦福大方院造成的损坏。补全支撑拱门石头(当然还
是用菱形方块表示),如下图所示:


你编写的程序应当能解决如上情况,但凡是满足本题基本规则的情况都应该能解决,规则见
题末。本题文件夹中有各种不同情况的界面,你的程序应当能在这些界面中正确运行。
卡雷尔完工后,所有空缺应当被菱形填满,效果图如下:

卡雷尔需遵循如下规则:
1、卡雷尔的初始位置位于第一列、第一行,面向东,携带无限的支撑石(灰色方块);
2、每隔三列有一个支撑柱,分别位于第一、五、九和十三列,依此类推;
3、 最后一个支撑柱右侧紧贴一堵墙,本题中,墙壁紧贴第十三列右侧,但无论有多少组支撑柱,你的程序都应该能正确运行;
4、 每组支撑柱顶端为墙体,但卡雷尔不知道每个支撑住是否只有5 块石头,也不知道是否所有的支撑柱都等高。
5、有些支撑柱中的石头并未完全缺损,你的程序不能在已有石头的位置再次置放。

 

*************我的程序*********************

 

 

 

和一个网友的同一道题对比了下,发现我的程序中28行while改为if应该更舒服一些。

那网友的源码地址:

 

http://blog.csdn.net/shmming/archive/2010/12/13/6073769.aspx

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值