uva1343

原作者

题目题意是:给你的棋盘,在A-H方向上可以拨动,问你最少拨动几次可以是中心图案的数字一致

解题思路:回溯法,剪枝

其中要把每次拨动的字母所代表的位置提前用数组表示;

然后在如果step+h()>maxd表示剪枝。

总之,用数组那里表示真的好棒,

自己太残了……!!!

#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <cstdlib>
#include <stack>
#include <cctype>
#include <string>
#include <malloc.h>
#include <queue>
#include <map> using namespace std; const int INF = 0xffffff;
const double esp = 10e-;
const double Pi = * atan(1.0);
const int Maxn = +;
const long long mod = ;
const int dr[] = {,,-,,-,,-,};
const int dc[] = {,,,-,,-,-,};
typedef long long LL; LL gac(LL a,LL b){
return b?gac(b,a%b):a;
}
int shift[][] = {
{,,,,,,},
{,,,,,,},
{,,,,,,},
{,,,,,,},
{,,,,,,},
{,,,,,,},
{,,,,,,},
{,,,,,,}
};
int center[] = {,,,,,,,};
int re[] = {,,,,,,,};
int d[];
char ans[];
int maxd; bool is_good(){
for(int i = ;i < ;i++){
if(d[ center[] ] != d[ center[i] ])
return ;
}
return ;
} void _move(int x){
int tmp = d[ shift[x][] ];
for(int i = ;i < ;i++)
d[ shift[x][i] ] = d[shift[x][i+] ];
d[shift[x][]] = tmp;
} int differ(int x){
int cnt = ;
for(int i = ;i < ;i++){
if(d[center[i]] != x){
cnt++;
}
}
return cnt;
} int h(){
return min(differ(),min(differ(),differ()));
} bool dfs(int step){
if(step == maxd){
if(is_good()){
ans[step] = '\0';
return ;
}
return ;
}
if(step + h() > maxd)
return ;
for(int i = ;i < ;i++){
ans[step] = i + 'A';
_move(i);
if(dfs(step+))
return ;
_move(re[i]);
}
return ;
} int main()
{
#ifndef ONLINE_JUDGE
freopen("inpt.txt","r",stdin);
#endif
int goal[];
char str[] = "No moves needed";
while(~scanf("%d",&goal[]) && goal[]){
for(int i = ;i < ;i++){
scanf("%d",&goal[i]);
}
for(maxd = ;;maxd++){
memcpy(d,goal,sizeof(d));
if(dfs())
break;
}
printf("%s\n%d\n",(maxd?ans:str),d[center[]]);
}
return ;
}

uva 1343 非原创的更多相关文章

  1. Linux下high CPU分析心得【非原创】

    非原创,搬运至此以作笔记, 原地址:http://www.cnitblog.com/houcy/archive/2012/11/28/86801.html 1.用top命令查看哪个进程占用CPU高ga ...

  2. CSS样式命名整理(非原创)

    非原创,具体出自哪里忘了,如果侵害您的利益,请联系我. CSS样式命名整理 页面结构 容器: container/wrap 整体宽度:wrapper 页头:header 内容:content 页面主体 ...

  3. 非原创。使用ajax加载控件

    非原创.来自博客园老赵. public class ViewManager<T> where T : System.Web.UI.UserControl { private System. ...

  4. Java 表达式解析(非原创)

    因项目需要,在网上找来一套表达式解析方法,由于原来的方法太过于零散,不利于移植,现在整理在同一文件内: 文件中包含5个内部类,源码如下: import java.util.ArrayList; imp ...

  5. Java Interface 是常量存放的最佳地点吗?(转帖学习,非原创)

    Java Interface 是常量存放的最佳地点吗?(转帖学习,非原创) 由于java interface中声明的字段在编译时会自动加上static final的修饰符,即声明为常量.因而inter ...

  6. 用RD,GR,BL三个方法内代码生成一张图片(非原创,我只是完整了代码)

    我公开以下图片的源代码,,是ppm格式的,,自己找到能打开的工具.. (非原创,我加工的代码,可直接执行运行输出,缩略图能看到效果)  这是原博客 http://news.cnblogs.com/n/ ...

  7. tp5.1 phpspreadsheet- 工具类 导入导出(整合优化,非原创,抄一抄,加了一些自己的东西,)

    phpspreadsheet-工具类 导入导出(整合优化,非原创,抄一抄,加了一些自己的东西)1. composer require phpoffice/phpspreadsheet2. 看最下面的两 ...

  8. Vue 仿QQ左滑删除功能(非原创)

    非原创,摘选来源:http://www.jb51.net/article/136221.htm. 废话不多说,相当实用,先记录. Html代码: <div class="contain ...

  9. 老男孩Django笔记(非原创)

    .WEB框架 MVC Model View Controller 数据库 模板文件 业务处理 MTV Model Template View 数据库 模板文件 业务处理 ############## ...

随机推荐

  1. 利用SolrJ操作solr API完成index操作

    使用SolrJ操作Solr会比利用httpClient来操作Solr要简单.SolrJ是封装了httpClient方法,来操作solr的API的.SolrJ底层还是通过使用httpClient中的方法 ...

  2. 基于visual Studio2013解决算法导论之028散列表开放寻址

     题目 散列表 解决代码及点评 #include <iostream> #include <time.h> using namespace std; template & ...

  3. 基于visual Studio2013解决算法导论之017查找第n小元素

     题目 查找第n小元素 解决代码及点评 #include <stdio.h> #include <stdlib.h> #include <malloc.h> ...

  4. 180China丨the Agency for Brand Engagement and Experience

    180China丨the Agency for Brand Engagement and Experience Welcome to 180. Welcome to Creativity. Thank ...

  5. 详解iOS7升级细节:引领视觉革命

    下星期我们将看到的正式版将和WWDC上看到的大不相同.苹果六月份发布了全新版本的iOS操作系统——这是从2007年首次发布以来的最大的一次调整和改进.这次的改变招致许多批评.许多设计师在网站上晒出了他 ...

  6. 第1章 Lua基础

    1.1 全局变量 全局变量不需要声明,给一个变量赋值后即创建了这个全局变量,访问一个没有初始化的全局变量也不会出错,只不过得到的结果是:nil. 如果你想删除一个全局变量,只需要将变量负值为 nil ...

  7. 一个开发原则:永远不要返回NULL

    看一篇文章:10个经典的java开发原则,里面一个原则:永远不要返回NULL. 说实在的,我对这个原则体会不是很深,平时在使用对象前,检查是否为null已经成了习惯,也是我要求开发人员的一个标准动作. ...

  8. xp对opengl的支持问题

    我在项目中遇到的xp显示问题是因为xp对opengl的支持问题,是通过void QCoreApplication::setAttribute(Qt::ApplicationAttribute attr ...

  9. android wifi讲解 wifi列表显示

    1.怎样获取wifi对象并进行操作 要操作WIFI设备,需要先获取Context.getSystemService(Context.WIFI_SERVICE)来获取WifiManager对象,并通过这 ...

  10. Android ListView 常见问题与使用总结

    一.机制 ListView机制 - 先运行getCount.然后运行getView. 假设getCount返回0,不运行getView Gallery convertView不会复用.每次都返回NUL ...