【问题标题】:Find String Path in Matrix using objective-c使用objective-c在矩阵中查找字符串路径
【发布时间】:2015-08-15 22:49:53
【问题描述】:

我是一名刚毕业的学生,​​我得到了一家大公司的面试。 在面试的过程中,我遇到了这个问题:

"如何检查字符矩阵中的字符串是否有路径?在矩阵中向左、向右、向上和向下移动,移动一个单元格。路径可以从字符矩阵中的任何条目开始一个矩阵。如果一个单元格被路径上的字符串的一个字符占据,它就不能再被另一个字符占据。"

例如,下面的三行四列矩阵有一个字符串“BCCED”的路径。它没有字符串“ABCB”的路径,因为字符串中的第一个“B”占据了矩阵中的“B”单元格,字符串中的第二个“B”不能再次进入同一个单元格。

A B C E
S F C S
A D E E

我使用了一种糟糕的方法来解决这个问题,因为我不知道如何在 Objective-C 中解决这样的矩阵。

我已经在其他网站上发现了很多类似的问题,主要是使用java而不是OC...

谁能告诉我一些在 Objective-C 中解决这个问题的好方法?

非常感谢。

【问题讨论】:

  • 我喜欢这些问题,因为你在编写 ObjC 时遇到的真正问题是这样的“我如何从导航栏中删除这 1px 行”,采访你的人都是白痴,我没有适合你的解决方案,但下次我建议向他们询问 1px 线的问题,看看需要多少矩阵代数才能得出答案
  • @Larcerax,非常感谢。
  • 好吧,我的意思是我可以帮助你,但是如果要在一两个月内编写 50K+ 行代码并从非技术人员的 UX 角度解决 UI 问题,这完全没有价值。所以,要回答你的问题,首先你需要知道如何制作一个数组,float matrix[4][4]; ...然后显然使用一些嵌套在另一个 for 循环中的 For 循环逻辑,然后可能是另一个,然后做一些 if/thens,可能会强制递归,然后 DONE 你有你的答案,抱歉这么否定,它完全不适用IOS 中的问题是如何解决的。
  • 我想说的是,任何了解计算机逻辑的人都可以花时间为此创建一个可行的解决方案,但他们能否处理一个爱猫但讨厌字体的女人的反击?你用你的导航栏,她开了一个博客,告诉她的朋友你的应用程序很糟糕,所以现在你必须深入研究字体理论,以确保你找到对猫爱好者有吸引力的东西,是的,我是认真的。

标签: ios string matrix data-structures path


【解决方案1】:

这是使用回溯解决的经典案例,通常的情况是您面临许多选项,您必须从中选择一个。做出选择后,您将获得一组新选项;您获得的选项集取决于您做出的选择。一遍又一遍地重复此过程,直到您达到最终状态。这里有一个很好的回溯解释http://www.cis.upenn.edu/~matuszek/cit596-2012/NewPages/backtracking.html

/*
This part needs to be in the caller program, which will pass the original array and input. I have taken input as an array, you can use input as a string for your question, in the end for the algorithm it doesn't matter. 

NSArray* a1 = @[
    @[@"A", @"B", @"C", @"E"],
    @[@"S", @"F", @"C", @"S"],
    @[@"A", @"D", @"E", @"E"],
];

NSArray* input = @[ @"A", @"B", @"C", @"B" ];
BOOL result = [self checkPath:a1 andInput:input];
*/
 -(BOOL) checkPath:(NSArray*)a1 andInput:(NSArray*)input
{
// Create a test array to keep track of what options you have already used.
NSMutableArray* testArray =
[[NSMutableArray alloc] init];
[testArray addObject:[[NSMutableArray alloc] init]];
[testArray addObject:[[NSMutableArray alloc] init]];
[testArray addObject:[[NSMutableArray alloc] init]];

// Initialize all the elements of the test array to zero, You should replace zero with 1 when you match the element at a certain index.
for(int i = 0; i < 3; i++) {
    for (int j = 0; j< 4; j++) {
        testArray[i][j]  = @0;
    }
}

for(int i = 0; i < 3; i++) {
    for (int j = 0; j< 4; j++) {
        NSLog(@"%@", a1[i][j]);
    }
}

BOOL res = NO;

int i = 0, j = 0;

while (!res && i <3) {
    while (!res && j <4) {
        if( [input[0] isEqualToString:a1[i][j]])
        {
            testArray[i][j] = @1;
            res = [self doesPathExist:input dictionaryArray:a1 startIndexX:i andStartIndexY:j compareIndex:1 andMarkerTable:testArray];
            // Reset the test option incase the chosen option wasn't success
            testArray[i][j] = [NSNumber numberWithBool:res];
        }
        j++;
    }
    i++;
}
return res;
}
/**
*  Check if the path exist in array a1 starting at index x and y for elements of array input, starting at element c
*
*  @param input       input array for which you want to check path in a1
*  @param a1          array in which we want to check the path
*  @param x           row number to start with
*  @param y           column number to start with
*  @param c           index of elemnet to check in input array
*  @param markerTable table that decides which elements have already been used.
*
*  @return yes if path exist for array input in a1 otherwise No
*/
-(BOOL)doesPathExist:(NSArray*)input dictionaryArray:(NSArray*)a1 startIndexX:(int) x andStartIndexY:(int) y compareIndex:(int)c andMarkerTable:(NSMutableArray*) markerTable
{
NSUInteger n = markerTable.count;
NSUInteger m = ((NSArray*)markerTable[0]).count;

NSUInteger N = input.count;
if (c == N)
{
    NSLog(@"YES, Path Exists");
    return YES;
}
else
{
    BOOL result = NO;
while (c < N) {
    int left = y -1;
    int right = y + 1;
    int top = x - 1;
    int bottom = x + 1;

    if (left>=0 && ![markerTable[x][left]  isEqualToNumber: @1]) {

        if( [a1[x][left] isEqualToString:input[c]]){
            // set marker table value as 1 to mark the match
            markerTable[x][left] = @1;
         result = [self doesPathExist:input dictionaryArray:a1 startIndexX:x andStartIndexY:left compareIndex:c+1 andMarkerTable:markerTable];
        // reset the marker table based on result, we do a reset of marker
        // table at the index to make sure if this combination fails and there
        // is another combination involving this element it should be able to use this element.
        markerTable[x][left] = [NSNumber numberWithBool:result];
            return result;
        }
    }
    if (right<m && ![markerTable[x][right] isEqualToNumber:@1]) {
        if([a1[x][right] isEqualToString:input[c]]) {
            markerTable[x][right] = @1;
            result = [self doesPathExist:input dictionaryArray:a1 startIndexX:x andStartIndexY:right compareIndex:c+1 andMarkerTable:markerTable];
            markerTable[x][right] = [NSNumber numberWithBool:result];
            return result;
        }
    }
    if(top >=0 && ![markerTable[top][y] isEqualToNumber:@1]) {
        if([a1[top][y] isEqualToString:input[c]]) {
            markerTable[top][y] = @1;
            result = [self doesPathExist:input dictionaryArray:a1 startIndexX:top andStartIndexY:y compareIndex:c+1 andMarkerTable:markerTable];
            markerTable[top][y] = [NSNumber numberWithBool:result];
            return result;
        }
    }
    if(bottom < n && ![markerTable[bottom][y] isEqualToNumber:@1]) {
        if([a1[bottom][y] isEqualToString:input[c]]) {
            markerTable[bottom][y] = @1;
            result = [self doesPathExist:input dictionaryArray:a1 startIndexX:bottom andStartIndexY:y compareIndex:c+1 andMarkerTable:markerTable];
            markerTable[bottom][y] = [NSNumber numberWithBool:result];
            return result;
        }
    }
    c++;
}
    NSLog(@"Returning false");
     return NO;
}
}

【讨论】:

  • 天哪!太酷了!!!我的意思是代码。我不敢相信。我不认为我可以在面试中解决这样的问题......
猜你喜欢
  • 1970-01-01
  • 2014-02-07
  • 1970-01-01
  • 1970-01-01
  • 2023-03-25
  • 1970-01-01
  • 1970-01-01
  • 2015-05-09
  • 1970-01-01
相关资源
最近更新 更多