USACO section 3.2 Magic Squares(STL+广搜)

用FMQL45T900+ADRV9009搭建你的第一套国产软件无线电(SDR)信号处理平台 本文详细介绍了如何利用FMQL45T900 ARM+FPGA异构处理器与ADRV9009射频模块搭建国产软件无线电(SDR)信号处理平台。从硬件架构解析、开发环境搭建到FM广播接收实现,提供了一套完整的全国产化解决方案,助力开发者轻松进入SDR领域。 阅读详情
Magic Squares
IOI'96

Following the success of the magic cube, Mr. Rubik invented its planar version, called magic squares. This is a sheet composed of 8 equal-sized squares:

1234
8765

In this task we consider the version where each square has a different color. Colors are denoted by the first 8 positive integers. A sheet configuration is given by the sequence of colors obtained by reading the colors of the squares starting at the upper left corner and going in clockwise direction. For instance, the configuration of Figure 3 is given by the sequence (1,2,3,4,5,6,7,8). This configuration is the initial configuration.

Three basic transformations, identified by the letters `A', `B' and `C', can be applied to a sheet:

  • 'A': exchange the top and bottom row,
  • 'B': single right circular shifting of the rectangle,
  • 'C': single clockwise rotation of the middle four squares.

Below is a demonstration of applying the transformations to the initial squares given above:

A:
8765
1234
B:
4123
5876
C:
1724
8635

All possible configurations are available using the three basic transformations.

You are to write a program that computes a minimal sequence of basic transformations that transforms the initial configuration above to a specific target configuration.

PROGRAM NAME: msquare

INPUT FORMAT

A single line with eight space-separated integers (a permutation of (1..8)) that are the target configuration.

SAMPLE INPUT (file msquare.in)

2 6 8 4 5 7 3 1 

OUTPUT FORMAT

 

Line 1:A single integer that is the length of the shortest transformation sequence.
Line 2:The lexically earliest string of transformations expressed as a string of characters, 60 per line except possibly the last line.

SAMPLE OUTPUT (file msquare.out)

7
BCABCCB

思路:就是开个MAP判重,然后广搜到答案输出即可。不会超的。

失误点:弄不对是因为题目按要求的字母序最小的方法。所以按顺序比时只要长度短的更新,长度一样的不用更新。

 

代码:

/*
ID:nealgav1
LANG:C++
PROG:msquare
*/
#include<fstream>
#include<string>
#include<cstring>
#include<queue>
#include<map>
using namespace std;
ifstream cin("msquare.in");
ofstream cout("msquare.out");
const string mm="12345678";
map<string,string>mp;
string taget;
string STR;
///A,B,C三种操作
bool A(string a)
{ string z=a;
  string b(z.rbegin(),z.rend());
  a=b;
  STR=a;
  if(mp[a]!="")///如果原先无此量,返回true,入队列
  { if(mp[a].length()>mp[z].length()+1)///当长度比其小时更新,一样长不用更新
    {mp[a].clear();mp[a]=mp[z]+"A";return true;}
    else return true;
  }
  else if(mp[a]==mm)return true;
  else {mp[a]=mp[z]+"A";return false;}
}
bool B(string a)
{
  string z=a;
  string b;
  string s(z[3]+z.substr(0,3)+z.substr(5)+z[4]);
   a=s;
  STR=a;
  if(mp[a]!="")
  { if(mp[a].length()>mp[z].length()+1)
    {mp[a].clear();mp[a]=mp[z]+"B";return true;}
    else return true;
  }
  else if(mp[a]==mm)return true;
  else {mp[a]=mp[z]+"B";return false;}
}
bool C(string a)
{
   string z=a;
   string b;
   swap(a[1],a[2]);swap(a[1],a[5]);swap(a[1],a[6]);
   STR=a;
   if(mp[a]!="")
  { if(mp[a].length()>mp[z].length()+1)
    {mp[a].clear();mp[a]=mp[z]+"C";return true;}
    else return true;
  }
  else if(mp[a]==mm)return true;
  else {mp[a]=mp[z]+"C";return false;}
}
void bfs()
{
  string sss="12345678";
  queue<string>que;
  que.push("12345678");
  while(!que.empty())
  {
    sss=que.front();///遇到目标输出即可
    if(sss==taget)return;
    que.pop();
    if(!A(sss))que.push(STR);
    if(!B(sss))que.push(STR);
    if(!C(sss))que.push(STR);
  }
}
int main()
{
  string a;
  for(int i=0;i<8;i++)
  {cin>>a;taget+=a;}
  bfs();
  cout<<mp[taget].length()<<"\n";
  cout<<mp[taget]<<"\n";
  cin.close();
  cout.close();
}


 

 

 

 

USER: Neal Gavin Gavin [nealgav1]
TASK: msquare
LANG: C++

Compiling...
Compile: OK

Executing...
   Test 1: TEST OK [0.000 secs, 3408 KB]
   Test 2: TEST OK [0.000 secs, 3408 KB]
   Test 3: TEST OK [0.000 secs, 3408 KB]
   Test 4: TEST OK [0.000 secs, 3408 KB]
   Test 5: TEST OK [0.097 secs, 4464 KB]
   Test 6: TEST OK [0.205 secs, 5652 KB]
   Test 7: TEST OK [0.410 secs, 6972 KB]
   Test 8: TEST OK [0.616 secs, 7632 KB]

All tests OK.

YOUR PROGRAM ('msquare') WORKED FIRST TIME! That's fantastic -- and a rare thing. Please accept these special automated congratulations.

Here are the test data inputs:

------- test 1 ----
2 6 8 4 5 7 3 1
------- test 2 ----
1 2 3 4 5 6 7 8
------- test 3 ----
6 7 4 1 8 5 2 3
------- test 4 ----
5 1 2 4 3 7 8 6
------- test 5 ----
6 1 5 4 3 2 7 8
------- test 6 ----
4 1 2 3 5 8 7 6
------- test 7 ----
3 4 2 1 5 6 7 8
------- test 8 ----
4 3 1 2 5 6 7 8
Keep up the good work!

 

Magic Squares
Hal Burch

This is a shortest path problem, where the nodes of the graph are board arrangements, and edges are transformations. There are 8! = 40,320 possible board arrangements, so the problem can be solved using breadth-first search (since all edges are of unit length.

Number the boards in increasing order lexicographically, so that indexing is simpler.

In order to simplify the calculations, walk the path backward (start at the ending arrangement given, find the minimum path to the initial configuration following reverse transformations). Then, walk backwards in the resulting tree to determine the path.

#include <stdio.h>
#include <assert.h>

/* the distance from the initial configuration (+1) */
/* dist == 0 => no know path */
int dist[40320];

/* calculate the index of a board */
int encode(int *board)
 {
  static int mult[8] = 
    {1, 8, 8*7, 8*7*6, 8*7*6*5,
     8*7*6*5*4, 8*7*6*5*4*3, 8*7*6*5*4*3*2};
  
  /* used to calculate the position of a number within the
     remaining ones */
  int look[8] = {0, 1, 2, 3, 4, 5, 6, 7};
  int rlook[8] = {0, 1, 2, 3, 4, 5, 6, 7};
  /* rlook[look[p]] = p and look[rlook[p]] = p */

  int lv, rv;
  int t;

  rv = 0;
  for (lv = 0; lv < 8; lv++)
   {
    t = look[board[lv]]; /* the rank of the board position */
    assert(t < 8-lv); /* sanity check */
    rv += t * mult[lv]; 

    assert(look[rlook[7-lv]] == 7-lv); /* sanity check */

    /* delete t */
    look[rlook[7-lv]] = t;
    rlook[t] = rlook[7-lv];
   }
  return rv;
 }

/* the set of transformations, in order */
static int tforms[3][8] = { {8, 7, 6, 5, 4, 3, 2, 1},
     {4, 1, 2, 3, 6, 7, 8, 5}, {1, 7, 2, 4, 5, 3, 6, 8} };

void do_trans(int *inboard, int *outboard, int t)
 { /* calculate the board (into outboard) that results from doing
      the t'th transformation to inboard */
  int lv;
  int *tform = tforms[t];

  assert(t >= 0 && t < 3);

  for (lv = 0; lv < 8; lv++)
    outboard[lv] = inboard[tform[lv]-1];
 }

void do_rtrans(int *inboard, int *outboard, int t)
 { /* calculate the board (into outboard) that which would result
      in inboard if the t'th transformation was applied to it */
  int lv;
  int *tform = tforms[t];

  assert(t >= 0 && t < 3);

  for (lv = 0; lv < 8; lv++)
    outboard[tform[lv]-1] = inboard[lv];
 }

/* queue for breadth-first search */
int queue[40325][8];
int qhead, qtail;

/* calculate the distance from each board to the ending board */
void do_dist(int *board)
 {
  int lv;
  int t1;
  int d, t;

  qhead = 0;
  qtail = 1;

  /* the ending board is 0 steps away from itself */
  for (lv = 0; lv < 8; lv++) queue[0][lv] = board[lv];
  dist[encode(queue[0])] = 1; /* 0 steps (+ 1 offset for dist array) */

  while (qhead < qtail)
   {
    t1 = encode(queue[qhead]);
    d = dist[t1];

    /* for each transformation */
    for (lv = 0; lv < 3; lv++)
     {
      /* apply the reverse transformation */
      do_rtrans(queue[qhead], queue[qtail], lv);

      t = encode(queue[qtail]);
      if (dist[t] == 0) 
       { /* found a new board position!  add it to queue */
        qtail++;
        dist[t] = d+1;
       }
     }

    qhead++;
   }
 }

/* find the path from the initial configuration to the ending board */
void walk(FILE *fout)
 {
  int newboard[8];
  int cboard[8];
  int lv, lv2;
  int t, d;

  for (lv = 0; lv < 8; lv++) cboard[lv] = lv;
  d = dist[encode(cboard)];
  /* start at the ending board */
  while (d > 1)
   {
    for (lv = 0; lv < 3; lv++)
     {
      do_trans(cboard, newboard, lv);
      t = encode(newboard);
      if (dist[t] == d-1) /* we found the previous board! */
       {
        /* output transformatino */
        fprintf (fout, "%c", lv+'A');

	/* find the rest of the path */
        for (lv2 = 0; lv2 < 8; lv2++) cboard[lv2] = newboard[lv2];
	break;
       }
     }
    assert(lv < 3);
    d--;
   }
  fprintf (fout, "\n");
 }

int main(int argc, char **argv)
 {
  FILE *fout, *fin;
  int board[8];
  int lv;

  if ((fin = fopen("msquare.in", "r")) == NULL)
   {
    perror ("fopen fin");
    exit(1);
   }
  if ((fout = fopen("msquare.out", "w")) == NULL)
   {
    perror ("fopen fout");
    exit(1);
   }

  for (lv = 0; lv < 8; lv++) 
   {
    fscanf (fin, "%d", &board[lv]);
    board[lv]--; /* use 0-based instead of 1-based */
   }

  /* calculate the distance from each board to the ending board */
  do_dist(board);

  for (lv = 0; lv < 8; lv++) board[lv] = lv;

  /* output the distance from and the path from the initial configuration */
  fprintf (fout, "%d\n", dist[encode(board)]-1);
  walk(fout);

  return 0;
 }

 

 


Thanks for your submission!

USACO section 3.4 Closed Fences(计算几何+叉积+二分) Closed Fences A closed fence in the plane is a set of non-crossing, connected line segments with N corners (3 i, yi}, i in (1..N). Every pair of adjacent vertices defines a side of the fence. 阅读详情

相关推荐

手机抽奖页面代码html,html5大转盘抽奖支持手机转盘抽奖代码

特效描述:html5 大转盘抽奖 支持手机 转盘抽奖代码。html5转盘,html5抽奖,手机转盘,手机抽奖,手机转盘抽奖,可配置奖品抽奖。代码结构1. 引入CSS2. 引入JS3. HTML代码大转盘活动var turnplate={restaraunts:[],//大转盘奖品名称colors:[],//大转盘奖品区块对应背景颜色outsideRadius:192,//大转盘外圆的半径textR...

weixin_39628551的博客 1866

弱口令字典1500

123456789 1234567890 a123456789 123456 qq123456 abc123456 123456789a WOAINI1314 12345678 11111111 123123123 88888888 111111111 147258369 987654321 1111111111 1qaz2wsx 789456123 ilove

卧龙居 5万+

卫星载荷应用方案

本文将从技术和应用层面探讨卫星载荷的应用方案,分析各个领域的特点和挑战,以期为卫星载荷的应用提供一定的指导和参考。卫星载荷应用方案涵盖了通信、地球观测、气象预报、导航定位等诸多领域在每一个领域中,都存在着不同的技术挑战和应用需求。随着技术的进步和需求的变化,卫星载荷应用方案将不断发展和完善,为人类社会的进步和发展做出更大的贡献。用户可以通过接收多颗卫星的信号,计算自身的位置和速度信息。在覆盖扩展方面,可以采用多星座、MEO、GEO 等不同轨道高度的卫星布局,结合卫星的特点和应用需求,实现覆盖范围的扩展。

muyangjun的专栏 904

【USACO题库】3.2.5 Magic Squares魔板

题目描述 在成功地发明了魔方之后,拉比克先生发明了它的二维版本,称作魔板。这是一张有8个大小相同的格子的魔板: 1 2 3 4 8 7 6 5 我们知道魔板的每一个方格都有一种颜色。这8种颜色用前8个正整数来表示。可以用颜色的序列来表示一种魔板状态,规定从魔板的左上角开始,沿顺时针方向依次取出整数,构成一个颜色序列。对于上图的魔板状态,我们用序列(1,2,3,4,5,6,7,8)来

Hello,I'm Proking. 1328

二阶FLL辅助三阶PLL环路的代码解读

今天对GNSS接收机里面的载波跟踪环路代码进行了解读 以下是个人的一点见解 % 载波环第二步:环路滤波器 二阶FLL辅助三阶PLL环路 carrLoopS0 = lastCarrLoopS0 + (phaseErr*tau0PLL + freqErr*tau1FLL) * PDIcarr; carrLoopS1 = lastCarrLoopS1 + (tau1PLL*phaseErr + carrLoopS0 + tau2FLL*freqErr) * PD

LWN7321的博客 2629

Anomalib实战指南:从零开始构建自定义异常检测数据集

本文提供了一份详细的Anomalib实战指南,指导用户从零开始构建自定义异常检测数据集。文章深入解析了数据准备的核心步骤、目录结构规范,并对比了PatchCore与FastFlow等关键模型的适用场景。通过清晰的代码示例,手把手演示了环境配置、模型训练与推理评估的全流程,旨在帮助开发者快速掌握这一工业视觉异常检测利器,有效解决实际生产中的缺陷识别难题。

weixin_29054245的博客 643

USACO 3.2 Magic Squares 魔板 (BFS-HASH)

USACO 3.2 Magic Squares 魔板 (BFS-HASH) Time Limit:10000MS  Memory Limit:65536K Total Submit:188 Accepted:60  Case Time Limit:1000MS Description   在成功地发明了魔方之后,拉比克先生发明了它的二维版本,称作魔板。这是一张有8个大小相同的格子的魔板:

闲人请进 405

51单片机IAP在线升级

51单片机IAP在线升级 爱矽半导体E85F3325单片机IAP在线升级教程,此处可查看更新及demo下载 文章目录前言一、ROM资源二、KEIL有关知识1.BL51连接器:2.LX51连接器:三、Keil其他相关项说明:四、MCU启动文件说明:五、例程BOOTLOADER使用说明六、例程使用可更改说明:七、程序使用通信方式:XMODEM八、APP程序说明九、APP代码升级说明十、代码合并烧写 前言 扩展-IAP 主要用于产品出厂后应用程序的更新作用 ,类似于其他引导装载升级程序的方法,在系统中必须始终

qq_42415953的博客 6667

USACO 魔板 Magic Squares

题意还是很简单的,但是做起来就不是那么简单了 第一步就是规划好存储方式,还有类似判断的数组 unordered_map<string,int> mp; unordered_map<string,pair<char,string>> pre; queue<string> q; 把这个定义出来,其实思路就出来了: First: 就是先把 三个操作所生成的字符串str搞出来,然后用bfs进行搜索,如果这个路径还没走过的话即mp.count(str) == 0,我们

cainiao的博客 288

CANoe诊断测试避坑指南:DTC故障注入与ECU刷写失败的N个常见原因排查

本文深入解析CANoe诊断测试中的常见问题,重点探讨DTC故障注入与ECU刷写失败的排查策略。从会话管理、安全访问到数据完整性校验,提供实战经验与CAPL脚本示例,帮助工程师规避诊断测试中的典型陷阱,提升车载网络通信与ECU刷写的可靠性。

weixin_29278805的博客 461

cursor AI代码编辑器IDE对接deepseek模型api;字节ai编辑器trae

参考: https://www.cursor.com/ https://platform.deepseek.com/https://www.cursor.com/下载安装后,注册账号登录即可 再高级里选择设置 再模型这注册模型: 模型名称一定要填正确,url地址填好,最后填上秘钥验证

weixin_42357472的博客 1万+

[U]3.2.5 Magic Squares 康托展开+BFS

这题和八数码十分相似,或许是我的遗留问题吧....敲完总是不过,各种错误。迟迟下不了手的原因就在于对空间的把握问题。要节约空间就必须设计一个好的hash函数,不然时空开销大。关键在于哈希怎么弄了?八位数--->87654321如果采用这么大的空间,空间消耗太大,许多内存都没采用,不行...  后来查了下资料,发现有康托展开恰好是结果全排列的哈希问题。有了康托展开之后,全排列的问题就可以采用最小的

Bysen32 2419

CVPR 2023 中的半监督学习: FixMatch 的升级版 UniMatch

我们首先回顾下发表在 NeurIPS 2020 上的 FixMatch: Simplifying Semi-Supervised Learning with Consistency and Confidence,FixMatch 是一种更轻量的半监督分类方法。如下图所示,FixMatch 首先使用模型(带标签数据训练后的)对弱增强的未标记图像进行预测,生成伪标签。对于给定的图像,只有在模型产生高置信度的预测时,伪标签才会被保留。然后,在输入同一图像的强增强版本时,训练模型预测伪标签。

NJU phd 在读。 2632

USACO 3.2 Magic Squares 魔板

题目大意: 在成功地发明了魔方之后,拉比克先生发明了它的二维版本,称作魔板。这是一张有8个大小相同的格子的魔板: 1 2 3 4 8 7 6 5 我们知道魔板的每一个方格都有一种颜色。这8种颜色用前8个正整数来表示。可以用颜色的序列来表示一种魔板状态,规定从魔板的左上角开始,沿顺时针方向依次取出整数,构成一个颜色序列。对于上图的魔板状态,我们用序列(1,2,3,4,5,6,7,8)来表

生如夏花之绚烂 1163

android 蓝牙通讯demo

android 下蓝牙通讯demo,实现了发现蓝牙,使能蓝牙,通过蓝牙发送消息。

pscad IEEE标准系统搭建说明

pscad IEEE标准系统搭建说明,包含典型的IEEE 9节点、14节点、30节点、39节点和118节点系统。内含标准测试系统的原始数据和潮流结果。

USACO3.2.5 Magic Squares (msquare)

标准的宽搜,应该没什么难度吧。/* ID:shijiey1 PROG:msquare LANG:C++ */ #include #include #include #include #include #include #include using namespace std; struct Node { int deep; string stat, trans; Node(

Shion的专栏 642

IDEA的使用指南

IDEA基于java的使用手册,可以最快速的上手,在使用中不断的丰富自己的使用技巧.

USACO section 3.1 Contact(AC自动机)

Contact IOI'98 The cows have developed a new interest in scanning the universe outside their farm with radiotelescopes. Recently, they noticed a very curious microwave pulsing emission sent right f

nealgavin的专栏 9384

霍尼韦尔SoftMaster使用说明

霍尼韦尔SoftMaster使用说明

USCAO section 1.3 Calf Flac

Calf Flac It is said that if you give an infinite number of cows an infinite number of heavy-duty laptops (with very large keys), that they will ultimately produce all the world's great palindromes

nealgavin的专栏 6656

USACO section 1.5 Checker Challenge(dfs深搜)

Checker ChallengeExamine the 6x6 checkerboard below and note that the six checkers are arranged on the board so that one and only one is placed in each row and each column, and there is never more tha

nealgavin的专栏 1923

USACO section 2.3 Longest Prefix(dp)

Longest Prefix IOI'96 The structure of some biological objects is represented by the sequence of their constituents, where each part is denote by an uppercase letter. Biologists are interested in

nealgavin的专栏 1486

USACO section 2.4 Cow Tours(并查集+最短路)

Cow Tours Farmer John has a number of pastures on his farm. Cow paths connect some pastures with certain other pastures, forming a field. But, at the present time, you can find at least two pasture

nealgavin的专栏 1405

USACO section3.1 Shaping Regions(漂浮法)

Shaping Regions N opaque rectangles (1 <= N <= 1000) of various colors are placed on a white sheet of paper whose size is A wide by B long. The rectangles are put with their sides parallel to the s

nealgavin的专栏 1052

USACO section 2.2 Runaround Numbers

Runaround Numbers Runaround numbers are integers with unique digits, none of which is zero (e.g., 81362) that also have an interesting property, exemplified by this demonstration: If you start

nealgavin的专栏 989

USACO section 3.1 Stamps(dp)

Stamps Given a set of N stamp values (e.g., {1 cent, 3 cents}) and an upper limit K to the number of stamps that can fit on an envelope, calculate the largest unbroken list of postages from 1 cent

nealgavin的专栏 980

USCAO section 4.1 Fence Loops(最短路,最小环,5级)

Fence Loops The fences that surround Farmer Brown's collection of pastures have gotten out of control. They are made up of straight segments from 1 through 200 feet long that join together only a

nealgavin的专栏 941

USACO section 1.5 Superprime Rib(深搜)

Superprime RibButchering Farmer John's cows always yields the best prime rib. You can tell prime ribs by looking at the digits lovingly stamped across them, one by one, by FJ and the USDA. Farmer John

nealgavin的专栏 937

USACO section 2.2 Party Lamps

Party Lamps IOI 98 To brighten up the gala dinner of the IOI'98 we have a set of N (10 <= N <= 100) colored lamps numbered from 1 to N. The lamps are connected to four buttons: Button 1: Wh

nealgavin的专栏 913
上一篇: USACO section 3.2 Feed Ratios(高斯定理)
下一篇: USACO section 3.2 Sweet Butter(SPFA)
剑不飞
博客等级 码龄15年 86粉丝 505原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值