中级农民
- 积分
- 123
- 大米
- 颗
- 鳄梨
- 个
- 水井
- 尺
- 蓝莓
- 颗
- 萝卜
- 根
- 小米
- 粒
- 学分
- 个
- 注册时间
- 2016-5-31
- 最后登录
- 1970-1-1
|
扫地机器人挺好玩的。oc写的,我抛砖,
- - (BOOL) needClean:(int) i j:(int) j
- {
- NSString *k = [NSString stringWithFormat:@"%d %d", i, j];
- if([rh valueForKey:k] == nil)
- return true;
- return false;
- }
- - (void) markClean:(int) i j:(int)j value:(int) v
- {
- NSString *k = [NSString stringWithFormat:@"%d %d", i, j];
- NSNumber *n = [NSNumber numberWithInt:v];
- [rh setObject:n forKey:k];
- }
- - (BOOL) move
- {
- if([robot move]){
- if(bdir == 0){
- bi--;
- }else if(bdir == 1){
- bj++;
- }else if(bdir == 2){
- bi++;
- }else{
- bj--;
- }
- [self markClean:bi j:bj value:1];
- return true;
- }else{
- [self markClean:bi j:bj value:2];
- }
- return false;
- }
- - (BOOL) needCleanForNext
- {
- int i = bi, j = bj;
- if(bdir == 0){
- i--;
- }else if(bdir == 1){
- j++;
- }else if(bdir == 2){
- i++;
- }else{
- j--;
- }
- return [self needClean:i j:j];
- }
- - (void) dfs
- {
- if([self needCleanForNext] && [self move]){
- [self dfs];
- }
- [self turnLeft];
- if([self needCleanForNext] && [self move]){
- [self dfs];
- }
-
- [self turnLeft];
- [self turnLeft];
- if([self needCleanForNext] && [self move]){
- [self dfs];
- }
-
-
- [self turnLeft];
- [self turnLeft];
- [self turnLeft];
- [self move];
- [self turnLeft];
- [self turnLeft];
- }
- - (void) turnLeft
- {
- bdir--;
- if(bdir < 0)
- bdir = 3;
- [robot turnLeft:1];
- }
- - (void) cleanRoom
- {
- [self markClean:bi j:bj value:1];
- [self dfs];
- [robot print];
- }
- - (void) testRobot
- {
- bi = 0, bj = 0, bdir = 0;
- robot = [[Robot alloc] init];
- rh = [NSMutableDictionary dictionary];
- [self cleanRoom];
- }
复制代码 |
|