http://poj.org/problem?id=2286

题目大意:如图所示有一种玩具,每次可以拉动A-H的开关使得整个行(或列)向字母方向移动一位(如果移动到头的话则到行(列)尾部)

求使得中间八个方格内数字相同的移动顺序(移动次数最小且字典序最小)和最终中间八个方格内的数的个数。

————————————————————————

说好的学完IDA*之后独立做了一道题,全程没查题解一遍AC。

对于IDA*是什么不了解的,可以看我的置顶骑士精神那道题。

首先估价函数简单点处理就是8-中间相同的数的最大个数。

然后枚举最终答案跑dfs即可,没什么太大难度(就是代码实现吗!)

#include<cstdio>
#include<cstring>
#include<cctype>
#include<iostream>
#include<map>
using namespace std;
int a[];
char dir[]={'A','B','C','D','E','F','G','H'};
int go[][]={{,,,,,,},
{,,,,,,},
{,,,,,,},
{,,,,,,},
{,,,,,,},
{,,,,,,},
{,,,,,,},
{,,,,,,}};
int arc[]={,,,,,,,};
int ans;
inline int h(){
int t[]={},res=;
for(int i=;i<=;i++)t[a[i]]++;
for(int i=;i<=;i++)t[a[i]]++;
for(int i=;i<=;i++)t[a[i]]++;
for(int i=;i<=;i++)res=max(res,t[i]);
return -res;
}
inline void turn(int k){
for(int i=;i<;i++){
swap(a[go[k][i]],a[go[k][i-]]);
}
return;
}
char realdir[];
inline bool dfs(int step){
if(step==ans){
if(!h())return ;
return ;
}
if(h()+step>ans)return ;
for(int i=;i<;i++){
turn(i);
if(dfs(step+)){
realdir[step]=i;
return ;
}
return ;
}
int main(){
while(scanf("%d",&a[])!=EOF&&a[]){
for(int i=;i<=;i++)scanf("%d",&a[i]);
for(ans=;;ans++){
if(dfs())break;
}
if(!ans)printf("No moves needed\n%d\n",a[]);
else{
for(int i=;i<ans;i++)printf("%c",dir[realdir[i]]);
printf("\n%d\n",a[]);
}
}
return ;
}

POJ2286:The Rotation Game——题解的更多相关文章

  1. POJ2286 The Rotation Game

    Description The rotation game uses a # shaped board, which can hold 24 pieces of square blocks (see ...

  2. [poj2286]The Rotation Game (IDA*)

    //第一次在新博客里发文章好紧张怎么办 //MD巨神早已在一个小时前做完了 The Rotation Game Time Limit: 15000MS Memory Limit: 150000K To ...

  3. POJ2286 The Rotation Game(IDA*)

    The Rotation Game Time Limit: 15000MS   Memory Limit: 150000K Total Submissions: 5691   Accepted: 19 ...

  4. POJ2286 The Rotation Game[IDA*迭代加深搜索]

    The Rotation Game Time Limit: 15000MS   Memory Limit: 150000K Total Submissions: 6325   Accepted: 21 ...

  5. A*专题训练

    POJ2449 Remmarguts' Date UDF's capital consists of N stations. The hall is numbered S, while the sta ...

  6. The Rotation Game (POJ 2286) 题解

    [问题描述] (由于是英文的,看不懂,这里就把大意给大家说一下吧……都是中国人,相信大家也不愿意看英文……) 如图,一个井字形的棋盘,中间有着1-3任意的数,有ABCDEFGH八个操作,每个操作意味着 ...

  7. Hackerrank - Game Of Rotation 题解

    旋转一个数组以得到最大值. 陷阱就是:不能排序.须要模拟操作旋转,并设计公式计算旋转后的和. 要求是O(n)时间完毕. 原题: https://www.hackerrank.com/challenge ...

  8. 【题解】【数组】【查找】【Leetcode】Search in Rotated Sorted Array

    Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 migh ...

  9. UVA1434-The Rotation Game(迭代加深搜索)

    Problem UVA1434-The Rotation Game Accept:2209  Submit:203 Time Limit: 3000 mSec  Problem Description ...

随机推荐

  1. 利用反射获取Model值

    public ActionResult Base(UserModel Model) { Model.Tel = string.Format("{0}-{1}", Model.Are ...

  2. android 学习四 ContentProvider

    1.系统自带的许多数据(联系人,本地信息等)保存在sqllite数据库,然后封装成许多ContentProvider来供其他程序访问. 2.对sqllite数据库的操作,可以在命令行通过adb工具登录 ...

  3. Selenium(Python)等待元素出现

    1.显式等待 from selenium import webdriverfrom selenium.webdriver.common.by import Byfrom selenium.webdri ...

  4. 用IDEA编写spark的WordCount

    我习惯用Maven项目 所以用IDEA新建一个Maven项目 下面是pom文件 我粘上来吧 <?xml version="1.0" encoding="UTF-8& ...

  5. 初学Direct X(3)

    初学Direct X(3) 1.获取外设输入--键盘以及鼠标 无论是获取鼠标还是键盘的设备,首先得初始化DirectInput,不过先把必要的环境先配置好: 所要用到的头文件以及库文件是(相比于前两次 ...

  6. leetcode-对称二叉树

    对称二叉树     给定一个二叉树,检查它是否是镜像对称的. 例如,二叉树 [1,2,2,3,4,4,3] 是对称的. 1 / \ 2 2 / \ / \ 3 4 4 3 但是下面这个 [1,2,2, ...

  7. Java并发简介

    年轻的时候学会了“使用”Servlet后,感觉自己什么都会做了,之后就不停的写所谓的业务逻辑,框架(这里说的不是structs,spring等,就是说servlet)给人们屏蔽了很多复杂性(更别说构建 ...

  8. 五:Edits Viewer离线日志查看器

    离线日志查看器可以将二进制日志翻译成可读的文件(如XML),只有当hadoop集群停止时才能使用.输入文件支持的类型:XML和二进制.输出文件支持类型:XML 二进制 Stats(标准输出?)     ...

  9. Javascript闭包演示【转】

    文章出自http://www.cnblogs.com/snandy/archive/2011/03/01/1967628.html 有个网友问了个问题,如下的html,为什么点击所有的段落p输出都是5 ...

  10. vue.js学习之 如何在better-scroll加载完成后,自动滚动到最底部

    首先我们需要使用scrollTo这个方法: scrollTo(x, y, time, easing) 参数: {Number} x 横轴坐标(单位 px) {Number} y 纵轴坐标(单位 px) ...