zoukankan      html  css  js  c++  java
  • 算法问题实战策略[串串字连环 ID:BOGGLE]

    对地图中每个位置进行搜索,如果当前位置的字母等于当前深度,则继续进行搜索。进行搜索的方式是新构造九个位置,如果这个位置在范围内并且地图内容等于当前深度...当深度+1(深度从0开始)达到要找的字符串的长度,说明已经搜索到了最后一个字符并且字符相等,所以就可以结束搜索了。

    int n=5, m=5;
    char board[5][5] = {
    	{'a', 'b', 'c', 'd', 'e'},
    	{'b', 'c', 'd', 'e', 'f'}, 
    	{'c', 'd', 'e', 'f', 'g'}, 
    	{'d', 'e', 'f', 'g', 'h'}, 
    	{'e', 'f', 'g', 'h', 'i'}
    };
    const int dx[8] = {-1, -1, -1, 1, 1, 1, 0, 0};
    const int dy[8] = {-1, 0, 1, -1, 0, 1, -1, 1};
    bool inRange(int x, int y) {
    	if (x >= 0 && x < n && y >= 0 && y < m) return true;
    	return false;
    }
    bool hasWord(int x, int y, const string &word, int nth) {
    	if (!inRange(x, y)) return false;
    	if (board[x][y] != word[nth]) return false;
    	if (word.size() == nth+1) return true;
    	for (int direction = 0; direction < 8; ++direction) {
    		int nx = x + dx[direction];
    		int ny = y + dy[direction];
    		if (hasWord(nx, ny, word, nth+1)) return true;
    	}
    	return false;
    }
    int main() {
    #ifdef LOCAL
    	freopen("input.txt", "r", stdin);
    #endif
    	string word = "abcde";
    	
    	for (int i = 0; i < 5; ++i) for (int j = 0; j < 5; ++j) if (hasWord(i, j, word, 0)) {
    		printf("Yes
    "); return 0;
    	}
    	printf("No
    ");
    	
    	return 0;
    }
    
  • 相关阅读:
    Tapestry AppModule中的方法
    Tapestry Grid
    Tapestry5之AutoLoading Module
    Tapestry Submits
    Tapestry SubmitLink
    再读Struts2之一:总括
    Java war包取之外的properties文件
    用ORACLE的高级复制实现内外网数据同步【转】
    在Oracle中实现数据库的复制
    解决ORA12560: TNS: 协议适配器错误
  • 原文地址:https://www.cnblogs.com/jeffy-chang/p/6994415.html
Copyright © 2011-2022 走看看