广度搜索BFS,要用Queue。还不是很熟,这道题帮助理清一些思绪了。其实这道题是求最短路径,所以BFS遇到第一个就可以返回了,所以后面有些现有大小和历史大小的判断可以省却。

过程中拿数组存step还是很有用的。其他的解法中我看到有把四位char编码返回整数的a*26*26*26+b*26*26+c*26+d,很不错,本质就是26进制的数。

import java.util.*;

public class SmartWordToy
{
public int minPresses(String start, String finish, String[] forbid) {
int[][][][] step = new int[26][26][26][26];
int[][][][] forb = new int[26][26][26][26];
for (int a = 0; a < 26; a++)
for (int b = 0; b < 26; b++)
for (int c = 0; c < 26; c++)
for (int d = 0; d < 26; d++)
{
step[a][b][c][d] = -1;
forb[a][b][c][d] = 0;
} for (int i = 0; i < forbid.length; i++) {
String[] lines = forbid[i].split(" ");
for (int a = 0; a < lines[0].length(); a++) {
for (int b = 0; b < lines[1].length(); b++) {
for (int c = 0; c < lines[2].length(); c++) {
for (int d = 0; d < lines[3].length(); d++) {
forb[lines[0].charAt(a)-'a'][lines[1].charAt(b)-'a'][lines[2].charAt(c)-'a'][lines[3].charAt(d)-'a'] = 1;
}
}
}
}
} LinkedList<Word> queue = new LinkedList<Word>();
Word sw = new Word(start.charAt(0), start.charAt(1), start.charAt(2), start.charAt(3));
step[sw.a-'a'][sw.b-'a'][sw.c-'a'][sw.d-'a'] = 0;
queue.offer(sw); Word fw = new Word(finish.charAt(0), finish.charAt(1), finish.charAt(2), finish.charAt(3)); while (queue.size() != 0) {
Word w = queue.poll();
int cur_step = step[w.a-'a'][w.b-'a'][w.c-'a'][w.d-'a'];
if (w.a == fw.a && w.b == fw.b && w.c == fw.c && w.d == fw.d) return cur_step; Word tmp = new Word();
tmp.a = next(w.a); tmp.b = w.b; tmp.c = w.c; tmp.d = w.d;
int tmp_step = step[tmp.a-'a'][tmp.b-'a'][tmp.c-'a'][tmp.d-'a'];
if (forb[tmp.a-'a'][tmp.b-'a'][tmp.c-'a'][tmp.d-'a'] == 0 && (tmp_step == -1 || tmp_step > cur_step+1)) {
step[tmp.a-'a'][tmp.b-'a'][tmp.c-'a'][tmp.d-'a'] = cur_step + 1;
queue.offer(tmp);
}
tmp = new Word();
tmp.a = prev(w.a); tmp.b = w.b; tmp.c = w.c; tmp.d = w.d;
tmp_step = step[tmp.a-'a'][tmp.b-'a'][tmp.c-'a'][tmp.d-'a'];
if (forb[tmp.a-'a'][tmp.b-'a'][tmp.c-'a'][tmp.d-'a'] == 0 && (tmp_step == -1 || tmp_step > cur_step+1)) {
step[tmp.a-'a'][tmp.b-'a'][tmp.c-'a'][tmp.d-'a'] = cur_step + 1;
queue.offer(tmp);
}
tmp = new Word();
tmp.a = w.a; tmp.b = next(w.b); tmp.c = w.c; tmp.d = w.d;
tmp_step = step[tmp.a-'a'][tmp.b-'a'][tmp.c-'a'][tmp.d-'a'];
if (forb[tmp.a-'a'][tmp.b-'a'][tmp.c-'a'][tmp.d-'a'] == 0 && (tmp_step == -1 || tmp_step > cur_step+1)) {
step[tmp.a-'a'][tmp.b-'a'][tmp.c-'a'][tmp.d-'a'] = cur_step + 1;
queue.offer(tmp);
}
tmp = new Word();
tmp.a = w.a; tmp.b = prev(w.b); tmp.c = w.c; tmp.d = w.d;
tmp_step = step[tmp.a-'a'][tmp.b-'a'][tmp.c-'a'][tmp.d-'a'];
if (forb[tmp.a-'a'][tmp.b-'a'][tmp.c-'a'][tmp.d-'a'] == 0 && (tmp_step == -1 || tmp_step > cur_step+1)) {
step[tmp.a-'a'][tmp.b-'a'][tmp.c-'a'][tmp.d-'a'] = cur_step + 1;
queue.offer(tmp);
}
tmp = new Word();
tmp.a = w.a; tmp.b = w.b; tmp.c = next(w.c); tmp.d = w.d;
tmp_step = step[tmp.a-'a'][tmp.b-'a'][tmp.c-'a'][tmp.d-'a'];
if (forb[tmp.a-'a'][tmp.b-'a'][tmp.c-'a'][tmp.d-'a'] == 0 && (tmp_step == -1 || tmp_step > cur_step+1)) {
step[tmp.a-'a'][tmp.b-'a'][tmp.c-'a'][tmp.d-'a'] = cur_step + 1;
queue.offer(tmp);
}
tmp = new Word();
tmp.a = w.a; tmp.b = w.b; tmp.c = prev(w.c); tmp.d = w.d;
tmp_step = step[tmp.a-'a'][tmp.b-'a'][tmp.c-'a'][tmp.d-'a'];
if (forb[tmp.a-'a'][tmp.b-'a'][tmp.c-'a'][tmp.d-'a'] == 0 && (tmp_step == -1 || tmp_step > cur_step+1)) {
step[tmp.a-'a'][tmp.b-'a'][tmp.c-'a'][tmp.d-'a'] = cur_step + 1;
queue.offer(tmp);
}
tmp = new Word();
tmp.a = w.a; tmp.b = w.b; tmp.c = w.c; tmp.d = next(w.d);
tmp_step = step[tmp.a-'a'][tmp.b-'a'][tmp.c-'a'][tmp.d-'a'];
if (forb[tmp.a-'a'][tmp.b-'a'][tmp.c-'a'][tmp.d-'a'] == 0 && (tmp_step == -1 || tmp_step > cur_step+1)) {
step[tmp.a-'a'][tmp.b-'a'][tmp.c-'a'][tmp.d-'a'] = cur_step + 1;
queue.offer(tmp);
}
tmp = new Word();
tmp.a = w.a; tmp.b = w.b; tmp.c = w.c; tmp.d = prev(w.d);
tmp_step = step[tmp.a-'a'][tmp.b-'a'][tmp.c-'a'][tmp.d-'a'];
if (forb[tmp.a-'a'][tmp.b-'a'][tmp.c-'a'][tmp.d-'a'] == 0 && (tmp_step == -1 || tmp_step > cur_step+1)) {
step[tmp.a-'a'][tmp.b-'a'][tmp.c-'a'][tmp.d-'a'] = cur_step + 1;
queue.offer(tmp);
}
}
return step[finish.charAt(0)-'a'][finish.charAt(1)-'a'][finish.charAt(2)-'a'][finish.charAt(3)-'a'];
} private char next(char c) {
if (c == 'z') return 'a';
else return (char)(c+1);
} private char prev(char c) {
if (c == 'a') return 'z';
else return (char)(c-1);
}
} class Word
{
char a;
char b;
char c;
char d; public Word(char _a, char _b, char _c, char _d) {
a = _a; b = _b; c = _c; d = _d;
} public Word() {}
}

  

[topcoder]SmartWordToy的更多相关文章

  1. TopCoder kawigiEdit插件配置

    kawigiEdit插件可以提高 TopCoder编译,提交效率,可以管理保存每次SRM的代码. kawigiEdit下载地址:http://code.google.com/p/kawigiedit/ ...

  2. 记第一次TopCoder, 练习SRM 583 div2 250

    今天第一次做topcoder,没有比赛,所以找的最新一期的SRM练习,做了第一道题. 题目大意是说 给一个数字字符串,任意交换两位,使数字变为最小,不能有前导0. 看到题目以后,先想到的找规律,发现要 ...

  3. TopCoder比赛总结表

    TopCoder                        250                              500                                 ...

  4. Topcoder几例C++字符串应用

    本文写于9月初,是利用Topcoder准备应聘时的机试环节临时补习的C++的一部分内容.签约之后,没有再进行练习,此文暂告一段落. 换句话说,就是本文太监了,一直做草稿看着别扭,删掉又觉得可惜,索性发 ...

  5. TopCoder

    在TopCoder下载好luncher,网址:https://www.topcoder.com/community/competitive%20programming/ 选择launch web ar ...

  6. TopCoder SRM 596 DIV 1 250

    body { font-family: Monospaced; font-size: 12pt } pre { font-family: Monospaced; font-size: 12pt } P ...

  7. 求拓扑排序的数量,例题 topcoder srm 654 div2 500

    周赛时遇到的一道比较有意思的题目: Problem Statement      There are N rooms in Maki's new house. The rooms are number ...

  8. TopCoder SRM 590

     第一次做TC,不太习惯,各种调试,只做了一题...... Problem Statement     Fox Ciel is going to play Gomoku with her friend ...

  9. Topcoder Arena插件配置和训练指南

    一. Arena插件配置 1. 下载Arena 指针:http://community.topcoder.com/tc?module=MyHome 左边Competitions->Algorit ...

随机推荐

  1. asp搜索两个以上的词的原理

    通常会在许多网站上进行搜索一些内容,要输入两个或两个以上的词,它的原理是这样的: 假设在搜索框search中输入:“asp php” 先得到输入框中的内容:search=request("s ...

  2. Android屏幕保持唤醒状态

    我们程序偶尔会有需要屏幕一直或较长时间的保持唤醒状态,而用户的睡眠时间又设置的比较短.这时可能会对程序以及用户的使用造成一定的影响.在Android中有两种方法,可以让我们在我们需要保持唤醒的页面长时 ...

  3. linux系统文件属性

    1  硬链接概念 硬链接是指通过索引节点(Inode)来进行链接,在Linux(ext2,ext3)文件系统中,保存在磁盘分区中的文件不管是什么类型都会给它分配一个编号,这个编号被称为索引节点编号(I ...

  4. Requirejs开篇

    前言 随着页面的内容丰富,以及网站体验更好.性能优化等,原有的通过script标签引入JavaScript脚本的方式已经不能很好地解决,此时新的一种JavaScript加载方式产生了--延时加载.执行 ...

  5. Java多线程-线程的锁总结

    一.多线程-同步函数的锁是this /*同步函数用的是哪一个锁呢?函数需要被对象调用.那么函数都有一个所属对象引用.就是this.所以同步函数使用的锁是this. 通过该程序进行验证. 使用两个线程来 ...

  6. JAVA日历

    效果图如下: import java.awt.*; import java.awt.event.*; import java.util.*; import javax.swing.*; import ...

  7. 访问图像中的像素[OpenCV 笔记16]

    再更一发好久没更过的OpenCV,不过其实写到这个部分对计算机视觉算法有所了解的应该可以做到用什么查什么了,所以后面可能会更的慢一点吧,既然开了新坑,还是机器学习更有研究价值吧... 图像在内存中的存 ...

  8. bzoj1015:[JSOI2008]星球大战starwar

    思路:反着做用并查集维护连通块个数就好了. #include<iostream> #include<cstdio> #include<cstring> #inclu ...

  9. c++实现类似Common Lisp的多参数加法和比较

    在CL里我们可以这样: $ sbcl * (+ 1 2 3) 6 * (< 1 2 3) T * (< 2 3 1) NIL * 从简单的方面看, CL的+和<就是一个接收多参数的函 ...

  10. 九度OJ 1514 数值的整数次方【算法】

    题目地址:http://ac.jobdu.com/problem.php?pid=1514 题目描述: 给定一个double类型的浮点数base和int类型的整数exponent.求base的expo ...