广度搜索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. JAXB - XML Schema Types, Defining Subtypes

    Although object orientation isn't a key feature of XML or the XML Schema language, it's still possib ...

  2. C#+ArcEngine 序列化和反序列化AE对象

    http://www.cnblogs.com/jindin/archive/2009/07/23/1529695.html 在AE开发过程,总是要将某些对象暂时存储起来,像element,layer, ...

  3. Maven3.0 服务器配置搭建

    搭建nexus私服,原因很简单,不必多说,本文重点说下最新版的Maven 3.0.x系列的安装步骤. 最新版的网上中文资料很少,参考后都没成功.最后在官网的英文资料中得到答案,成功搞定. 1.确定我们 ...

  4. 【html】【20】高级篇--轮播图[聚焦]

    下载: http://sc.chinaz.com/jiaoben/151204445580.htm 效果: html <!doctype html> <html> <he ...

  5. JavaScript 学习笔记: 扩充类型的功能

    JavaScript 是允许给基本类型扩充功能的.例如,可以通过对Object.prototype增加方法,可以让该方法对所有的对象都可用. 这样的方式对函数,数组,字符串,数字,正则表达式和布尔值同 ...

  6. Mysql的联合查询

    联合查询 union联合查询 语法:     select 语句 union [all|distinct] select 语句; 示例: 特点:     条件:两表的查询出来的字段数目必须相同     ...

  7. HDOJ 1024 Max Sum Plus Plus -- 动态规划

    题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=1024 Problem Description Now I think you have got an ...

  8. 第32条:用EnumSet代替位域

    如果一个枚举类型的元素主要用在集合中,一般使用int枚举模式,将2的不同倍数赋予每个常量: public class Text { public static final int STYLE_BOLD ...

  9. OS X平台上MySQL环境搭建

    参考资料: http://www.cnblogs.com/macro-cheng/archive/2011/10/25/mysql-001.html http://blog.csdn.net/just ...

  10. RX学习笔记:Bootstrap

    Bootstrap https://getbootstrap.com 2016-07-01 在学习FreeCodeCamp课程中了解到Bootstrap,并于课程第一个实战题卡在响应式部分,于是先对B ...