UVA_Rotation Game<旋转游戏> UVA 1343
The rotation game uses a # shaped board, which can hold 24 pieces of square blocks (see Fig.1). The blocks
are marked with symbols 1, 2 and 3, with exactly 8 pieces of each kind.
Initially, the blocks are placed on the board randomly. Your task is to move the blocks so that the eight blocks
placed in the center square have the same symbol marked. There is only one type of valid move, which is to
rotate one of the four lines, each consisting of seven blocks. That is, six blocks in the line are moved towards
the head by one block and the head block is moved to the end of the line. The eight possible moves are
marked with capital letters A to H. Figure 1 illustrates two consecutive moves, move A and move C from
some initial configuration.
Input
The input consists of no more than 30 test cases. Each test case has only one line that contains 24 numbers,
which are the symbols of the blocks in the initial configuration. The rows of blocks are listed from top to
bottom. For each row the blocks are listed from left to right. The numbers are separated by spaces. For
example, the first test case in the sample input corresponds to the initial configuration in Fig.1. There are no
blank lines between cases. There is a line containing a single `0' after the last test case that ends the input.
Output
For each test case, you must output two lines. The first line contains all the moves needed to reach the final
configuration. Each move is a letter, ranging from `A' to `H', and there should not be any spaces between the
letters in the line. If no moves are needed, output `No moves needed' instead. In the second line, you must
output the symbol of the blocks in the center square after these moves. If there are several possible solutions,
you must output the one that uses the least number of moves. If there is still more than one possible solution,
you must output the solution that is smallest in dictionary order for the letters of the moves. There is no need
to output blank lines between cases.
Sample Input
1 1 1 1 3 2 3 2 3 1 3 2 2 3 1 2 2 2 3 1 2 1 3 3
1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 3 3 3 3 3 3 3 3
0
Sample Output
AC
2
DDHH
2
解题报告
比较基础的IDA*,启发函数就是每次最多只能恢复一个...
代码比较挫(旋转那完全是人工。。。)
- #include <iostream>
- #include <cstdio>
- #include <cstring>
- #include <algorithm>
- using namespace std;
- int s[];
- int maxd;
- char ans[];
- bool input(){
- scanf("%d",&s[]);
- if(s[] == ) return false;
- for(int i = ;i<=;++i)
- scanf("%d",&s[i]);
- return true;
- }
- void ope(int i,int * d){
- int ch;
- if (i == )
- {
- ch = d[];
- d[] = d[];
- d[] = d[];
- d[] = d[];
- d[] = d[];
- d[] = d[];
- d[] = d[];
- d[] = ch;
- }
- else if(i == )
- {
- ch = d[];
- d[] = d[];
- d[] = d[];
- d[] = d[];
- d[] = d[];
- d[] = d[];
- d[] = d[];
- d[] = ch;
- }
- else if(i == )
- {
- ch = d[];
- for(int i = ;i>=;--i)
- d[i] = d[i-];
- d[] = ch;
- }
- else if(i == )
- {
- ch = d[];
- for(int i = ;i>=;--i)
- d[i] = d[i-];
- d[] = ch;
- }
- else if(i == )
- {
- ch = d[];
- d[] = d[];
- d[] = d[];
- d[] = d[];
- d[] = d[];
- d[] = d[];
- d[] = d[];
- d[] = ch;
- }
- else if(i == )
- {
- ch = d[];
- d[] = d[];
- d[] = d[];
- d[] = d[];
- d[] = d[];
- d[]= d[];
- d[] = d[];
- d[] = ch;
- }
- else if(i == )
- {
- ch = d[];
- for(int i = ;i<=;++i)
- d[i] = d[i+];
- d[] = ch;
- }
- else if(i == )
- {
- ch = d[];
- for(int i = ;i<=;++i)
- d[i] = d[i+];
- d[] = ch;
- }
- }
- bool dfs(int d,int *t,int tid){
- if (d == maxd)
- {
- if (t[] != tid || t[] != tid) return false;
- for(int i = ;i<=;++i)
- if(t[i] != tid)
- return false;
- for(int i = ;i<=;++i)
- if(t[i] != tid)
- return false;
- return true;
- }
- int co = ;
- if (t[] != tid ) co ++;
- if (t[] != tid) co ++;
- for(int i = ;i<=;++i)
- if(t[i] != tid)
- co ++;
- for(int i = ;i<=;++i)
- if(t[i] != tid)
- co ++;
- if (co + d > maxd) return false;
- for(int i = ;i<;++i)
- {
- int nt[];
- memcpy(nt,t,sizeof(nt));
- ope(i,nt);
- ans[d] = i + 'A';
- if (dfs(d+,nt,tid)) return true;
- }
- return false;
- }
- int main(int argc,char * argv[]){
- freopen("in.txt","r",stdin);
- freopen("out.txt","w",stdout);
- int targetid,outid;
- char outans[];
- while(input())
- {
- for(int i = ;;++i)
- {
- int ok = ;
- maxd = i ;
- for(int j = ;j<=;++j)
- if (dfs(,s,j))
- {
- ok++;
- if (ok == )
- {
- outid = j;
- memcpy(outans,ans,sizeof(outans));
- }
- if (ok >= )
- {
- int check = ;
- for(int i = ;i<maxd;++i)
- if(outans[i] > ans[i])
- {
- check = ;
- break;
- }
- else if(outans[i] < ans[i])
- break;
- if(check)
- {
- memcpy(outans,ans,sizeof(outans));
- outid = j;
- }
- }
- }
- if(ok)
- break;
- }
- if (maxd == )
- cout << "No moves needed" << endl << outid << endl;
- else
- {
- outans[maxd] = '\0';
- cout << outans << endl << outid << endl;
- }
- }
- return ;
- }
UVA_Rotation Game<旋转游戏> UVA 1343的更多相关文章
- UVa 1343 旋转游戏(dfs+IDA*)
https://vjudge.net/problem/UVA-1343 题意:如图所示,一共有8个1,8个2和8个3,如何以最少的移动来使得中间8个格子都为同一个数. 思路:状态空间搜索问题. 用ID ...
- 09_Sum游戏(UVa 10891 Game of Sum)
问题来源:刘汝佳<算法竞赛入门经典--训练指南> P67 例题28: 问题描述:有一个长度为n的整数序列,两个游戏者A和B轮流取数,A先取,每次可以从左端或者右端取一个或多个数,但不能两端 ...
- UVA 1343 The Rotation Game
题意: 给出图,往A-H方向旋转,使中间8个格子数字相同.要求旋转次数最少,操作序列字典序尽量小. 分析: 用一维数组存24个方格.二维数组代表每个方向对应的7个方格.IDA*剪枝是当8-8个方格中重 ...
- POJ 3752 字母旋转游戏
问题描述: 给定两个整数M,N,生成一个M*N的矩阵,矩阵中元素取值为A至Z的26个字母中的一个,A在左上角,其余各数按顺时针方向旋转前进,依次递增放置,当超过26时又从A开始填充.例如,当M=5,N ...
- uva 1343 非原创
uva1343 原作者 题目题意是:给你的棋盘,在A-H方向上可以拨动,问你最少拨动几次可以是中心图案的数字一致 解题思路:回溯法,剪枝 其中要把每次拨动的字母所代表的位置提前用数组表示: 然后在如果 ...
- UVA - 1343 The Rotation Game (BFS/IDA*)
题目链接 紫书例题. 首先附上我第一次bfs+剪枝TLE的版本: #include<bits/stdc++.h> using namespace std; typedef long lon ...
- 【例 7-12 UVA - 1343】The Rotation Game
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 迭代加深搜索. 每次抽动操作最多只会让中间那一块的区域离目标的"距离"减少1. 以这个作为剪枝. 枚举最大深度. ...
- UVa 1343 The Rotation Game (状态空间搜索 && IDA*)
题意:有个#字型的棋盘,2行2列,一共24个格. 如图:每个格子是1或2或3,一共8个1,8个2,8个3. 有A~H一共8种合法操作,比如A代表把A这一列向上移动一个,最上面的格会补到最下面. 求:使 ...
- UVA 1343 - The Rotation Game-[IDA*迭代加深搜索]
解题思路: 这是紫书上的一道题,一开始笔者按照书上的思路采用状态空间搜索,想了很多办法优化可是仍然超时,时间消耗大的原因是主要是: 1)状态转移代价很大,一次需要向八个方向寻找: 2)哈希表更新频繁: ...
随机推荐
- mybatis和ibatis区别
ibatis本是apache的一个开源项目,2010年这个项目由apache software foundation 迁移到了google code,并且改名为mybatis. 1.Mybat ...
- Spring HTTP invoker 入门
一.简介 Spring开发团队意识到RMI服务和基于HTTP的服务(如,Hessian)之间的空白.一方面,RMI使用JAVA标准的对象序列化机制,很难穿透防火墙.另一方面,Hessian/Burla ...
- Ajax 获取数据代码
无刷新获取字符串: Html网页中: <script> //定义异步对象 var xmlHttp; //封装方法 function CreateXMLHTTP() { try { xmlH ...
- Lua Interface基础使用
Lua是一种可爱的脚本语言,由Roberto Ierusalimschy.Waldemar Celes 和 Luiz Henrique de Figueiredo所组成并于1993年开发. 其设计目的 ...
- [HeadFist-HTMLCSS学习笔记][第五章认识媒体]
图像格式 PNG 多种颜色透明 无损压缩 PNG-8,PNG-16,PNG-32 多用于logo GIF 动画 256色 无损 JPEG 不能透明 多用于照片 img URL能插入 alt属性 = 如 ...
- ASP.NET开发学习视频教程大全(共800集)
ASP.NET是微软.NET平台的支柱之一,被广泛应用在WEB等互联网开发领域,因此它的强大性和适应性,可以使它运行在Web应用软件开发者的几乎全部的平台上.这里整理了最全的ASP.NET开发学习视频 ...
- javascript 切换动画
function startMove(obj, json, fn) { clearInterval(obj.timer); obj.timer = setInterval(function() { v ...
- Python学习笔记2(控制语句)
1.if条件语句 if(表达式): 语句1 else: 语句2 2.if...elif...else判断语句 if(表达式1):语句1 elif(表达式2):语句2 ... elif(表达式n):语句 ...
- (原)ubuntu16中安装moses
转载请注明出处: http://www.cnblogs.com/darkknightzh/p/5653186.html 在ubuntu14中,可以使用下面的语句安装moses: luarocks in ...
- XML为数据库进行增删改查(实例)
//增删改查文件 package xml2; import java.io.BufferedReader;import java.io.BufferedWriter;import java.io.Fi ...