http://community.topcoder.com/stat?c=problem_statement&pm=11225&rd=14427

http://apps.topcoder.com/wiki/display/tc/SRM+498

此题的暴力解法是一个BFS。BFS的话,要有Queue,如果是图的话,还要有Set来记录走过的状态。后面答案中会看到其实可以用贪心,就是只要输入和输出的各种颜色数量一样,就一定能变换过来,这个下回再表。

import java.util.*;
public class NinePuzzle
{
public int getMinimumCost(String init, String goal)
{
int[][] arr = new int[10][];
arr[0] = new int[]{1, 2};
arr[1] = new int[]{0, 2, 3, 4};
arr[2] = new int[]{0, 1, 4, 5};
arr[3] = new int[]{1, 4, 6, 7};
arr[4] = new int[]{1, 2, 3, 5, 7, 8};
arr[5] = new int[]{2, 4, 8, 9};
arr[6] = new int[]{3, 7};
arr[7] = new int[]{3, 4, 6, 8};
arr[8] = new int[]{4, 5, 7, 9};
arr[9] = new int[]{5, 8};
Queue<String> queue = new LinkedList<String>();
HashSet<String> set = new HashSet<String>();
set.add(goal);
queue.offer(goal);
int initStar = -1;
for (int i = 0; i < init.length(); i++)
if (init.charAt(i) == '*')
initStar = i;
int ans = Integer.MAX_VALUE;
while (queue.size() != 0)
{
String s = queue.poll();
char[] ca = s.toCharArray();
int starPos = -1;
for (int i = 0; i < ca.length; i++)
if (ca[i] == '*') starPos = i; if (initStar == starPos)
{
int a = 0;
for (int i = 0; i < s.length(); i++)
{
if (initStar != i && ca[i] != init.charAt(i)) a++;
}
if (ans > a) ans = a;
}
for (int i = 0; i < arr[starPos].length; i++)
{
swap(ca, starPos, arr[starPos][i]);
String tmp = new String(ca);
if (!set.contains(tmp))
{
queue.offer(tmp);
set.add(tmp);
}
swap(ca, starPos, arr[starPos][i]);
}
}
return ans;
} private void swap(char[] ca, int i, int j)
{
char tmp = ca[i];
ca[i] = ca[j];
ca[j] = tmp;
}
}

  

[topcoder]NinePuzzle的更多相关文章

  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. RedHat6.1(64bit)安装JDK

    今天在服务器上装JDK1.5,费了不少力气,记录下来以供参考 服务器安装的操作系统为Red Hat 6.1(x86) [123@123 bin]$ cat /etc/redhat-release Re ...

  2. 在虚拟机安装64位系统提示,此主机支持Intel VT-x,但Intel VT-x处于禁用状态

    进入BIOS - Security - Virtualization - Intel (R) Virtualization Technology 将 Disabled 改为 Enabled 即可

  3. Redis操作字符串工具类封装,Redis工具类封装

    Redis操作字符串工具类封装,Redis工具类封装 >>>>>>>>>>>>>>>>>>& ...

  4. (转)我所理解的OOP——UML六种关系

    原文地址:http://www.cnblogs.com/dolphinX/p/3296681.html 最近由于经常给公司的小伙伴儿们讲一些OOP的基本东西,每次草纸都被我弄的很尴尬,画来画去自己都乱 ...

  5. Bootstrap教程

    Bootstrap 教程 Bootstrap 教程

  6. Git之路--1

    昨天下午到今天早上,终于搞定了github.过程很难过,不过看到自己的github上有代码了.还是小小的开心了一下.暂时没时间分享相关技术,附带微博链接,有想试试上传上Github的小伙伴可以查看我的 ...

  7. 版本控制:集中式 vs 分布式

    集中式 CVCS的版本库集中存放在中央服务器,而工作时都是用自己的电脑,所以要先从中央服务器取得最新的版本,然后工作完后再将自己的代码推送给中央服务器. CVS:最早的.开源.免费.由于自身设计的问题 ...

  8. LevelDB windows vs2013 c++编译和测试

    引用: (src1) :http://download.csdn.net/detail/flyfish1986/8881263(这里有下载地址) (src2) :http://blog.csdn.ne ...

  9. [002] The Perks of Being a Wallflower - 读后记

    The Perks of Being a Wallflower 今天(2015年10月30日 18:26:17)读完"The Perks of Being a Wallflower" ...

  10. Cocos2d-x 3.0坐标系详解(转载)

    Cocos2d-x 3.0坐标系详解 Cocos2d-x坐标系和OpenGL坐标系相同,都是起源于笛卡尔坐标系. 笛卡尔坐标系 笛卡尔坐标系中定义右手系原点在左下角,x向右,y向上,z向外,OpenG ...