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. MVC小系列(八)【改变Areas的FindView顺序】

    MVC小系列(八)[改变Areas的FindView顺序] 一般项目比较大的话,会根据模块建立Areas,这样结构清晰,也有利于路由的部署, 1 Areas下有自己的_LayOut模板,而如果希望所有 ...

  2. ACM/ICPC ZOJ1006-Do the Untwist 解题代码

    #include <iostream> #include <string> #include <stdlib.h> using namespace std; int ...

  3. 自动生成get,set方法

    引发的问题: Action中有一个属性名字叫private boolean isHideNumber 用struts2的<s:if test ="isHideNumber"& ...

  4. swift 函数返回值

    函数的定义及调用 func开头作为前缀,->返回的类型 add输出结果是11 函数参数也可以有多个参数,写在括号里用逗号隔开. func introduce(name: String,age: ...

  5. (CodeForces )540B School Marks 贪心 (中位数)

    Little Vova studies programming to p. Vova is very smart and he can write every test for any mark, b ...

  6. STL中map与hash_map的比较

    1. map : C++的STL中map是使用树来做查找算法; 时间复杂度:O(log2N) 2. hash_map : 使用hash表来排列配对,hash表是使用关键字来计算表位置; 时间复杂度:O ...

  7. java中的JSON对象的使用

    申明:没工作之前都没听过JSON,可能是自己太菜了.可能在前台AJAX接触到JSON,这几天要求在纯java的编程中,返回JSON字符串形式. 网上有两种解析JSON对象的jar包:JSON-lib. ...

  8. cmake,gtest单元测试程序

    参考:http://blog.csdn.net/stdcoutzyx/article/details/8284183 PROJECT (HELLO) SET(SRC_LIST main.c) MESS ...

  9. RM-Linux驱动--Watch Dog Timer(看门狗)驱动分析

    from:http://blog.csdn.net/geekcome/article/details/6595265 硬件平台:FL2440 内核版本:2.6.28 主机平台:Ubuntu 11,04 ...

  10. jquery api 笔记(2) 事件 事件对象

    事件 #1.resize()     缩放窗体:window.resizeTo(width, height); 并不是兼容做法.   #2 .scroll() ->获取滚动条的位置: .scro ...