andyfaces 2009-05-03 12:15
浏览 182
已采纳

关于JAVA算法的问题!谢谢!!

write a program to find words that are hidden in a square of
letters. Below is an example:

x z v g k u p
g s n i b p k
p i a n o d g
l w c e l l o
b q z h s u a
t b n s g w b
l k r t q f h

Starting from any character in the square, move to the left, to the right, up,
down, or on any of the four diagonals Your task is to find words
that can be spelled out with those characters on the moving path. For example,
you might have found the above square contains the word “viola” starting from
the ‘v’ in the first row to the last character in the fifth row. Similarly, you’ll
find “piano” and “cello”. For “piano”, you may wonder if words “a”, “a”, and
“no”, which are sub-strings of “piano”, count. The answer is yes and no. If a
word is part of another word, both words should be found, for example, “prose”
and “prosecution”. However, we just ignore a word if its length is less than or
equal to 4. In fact, you don’t need to consider the latter restriction, because we
shall provide you a dictionary containing no short words .

[img]/upload/attachment/99946/1104327c-66d0-3745-bc7a-5c104b0c508d.jpg[/img]

Thanks !!

(注:您只需提供具体算法或者分析的思路!谢谢~)

  • 写回答

3条回答 默认 最新

  • iteye_2914 2009-05-05 00:18
    关注

    [code="java"]
    package sny.ag;

    public class WordCross {
    private static char[][] charMap = {
    {'x', 'z', 'v', 'g', 'k', 'u', 'p'},
    {'g', 's', 'n', 'i', 'b', 'p', 'k'},
    {'p', 'i', 'a', 'n', 'o', 'd', 'g'},
    {'l', 'w', 'c', 'e', 'l', 'l', 'o'},
    {'b', 'q', 'z', 'h', 's', 'u', 'a'},
    {'t', 'b', 'n', 's', 'g', 'w', 'b'},
    {'l', 'k', 'r', 't', 'q', 'f', 'h'}
    };

    private static String[] wordSet = {
        "acz",
        "ce",
        "cel",
        "kpg",
        "krt"
    };
    
    public static void main(String[] args) {
        int wordLen = wordSet.length - 1;
        for (int x = 0; x < 7; x++) {
            for (int y = 0; y < 7; y++) {
                search(x, y, 0, 1, 0, 0, wordLen, new StringBuffer());
                search(x, y, 1, 1, 0, 0, wordLen, new StringBuffer());
                search(x, y, 1, 0, 0, 0, wordLen, new StringBuffer());
                search(x, y, -1, 1, 0, 0, wordLen, new StringBuffer());
                search(x, y, 0, -1, 0, 0, wordLen, new StringBuffer());
                search(x, y, -1, -1, 0, 0, wordLen, new StringBuffer());
                search(x, y, -1, 0, 0, 0, wordLen, new StringBuffer());
                search(x, y, -1, 1, 0, 0, wordLen, new StringBuffer());
            }
        }
    

    // search(0, 0, 0, 1, 0, 0, 1, new StringBuffer());
    }

    private static void search(int x, int y, int dx, int dy, int p, int s, int e, StringBuffer str) {
        char c = charMap[x][y];
        str.append(c);
        int s1 = -1, e1 = -1;
        for (int i = s; i <= e; i++) {
            if (wordSet[i].length() > p && wordSet[i].charAt(p) == c) {
                s1 = i;
                break;
            }
        }
        if (s1 == -1) {
            return;
        }
        for (int i = s1; i <= e; i++) {
            if (wordSet[i].length() > p) {
                if (wordSet[i].charAt(p) == c) {
                    e1 = i;
                    if (wordSet[i].length() == str.length()) {
                        System.out.println(str + "[" + x + "," + y + "][" + dx + "," + dy + "]");
                    }
                } else {
                    break;
                }
            }
        }
        x += dx;
        y += dy;
        if (x >= 0 && y >= 0 && x < 7 && y < 7) {
            search(x, y, dx, dy, p + 1, s1, e1, str);
        }
    }
    

    }
    [/code]

    不保证速度,应该使用更有效的搜索策略。另外字典也应该使用其它方式存储。
    只是演示一下想法而已。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 使用C#,asp.net读取Excel文件并保存到Oracle数据库
  • ¥15 C# datagridview 单元格显示进度及值
  • ¥15 thinkphp6配合social login单点登录问题
  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 虚心请教几个问题,小生先有礼了
  • ¥30 截图中的mathematics程序转换成matlab