博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[usaco3.2.5]msquare
阅读量:4561 次
发布时间:2019-06-08

本文共 1560 字,大约阅读时间需要 5 分钟。

  题目传送门:

  这道题bfs+hash,但想到要判重的数字不多,就直接用了map,数组传递有些麻烦,所以直接写在了一起,会有点乱

  

/*ID:abc31261LANG:C++TASK:msquare*/#include
#include
#include
#include
#include
using namespace std;const int n=20;int numend,a[n];map
hash;string anss="";int sum(int num[n]){ int i,l=0; for (i=1;i<=8;i++)l+=num[i]*a[i]; return l;}void init(){ int i,j,x=1,end[n]; for (i=1;i<=8;i++)scanf("%d",&end[i]); swap(end[5],end[8]); swap(end[6],end[7]); for (i=8;i>=1;i--) { a[i]=x; x=x*10; } numend=sum(end);}void bfs(){ queue
q; queue
q1; hash.clear(); q.push(12348765); q1.push(""); hash[12348765]=1; while (!q.empty()) { int i,j,j1,x=q.front(),num1[n],num2[n]; string s=q1.front(); q.pop(); q1.pop(); if (s=="BCABCCB") { i=1; } if (x==numend) { anss=s; return; } for (i=1;i<=8;i++) { num1[i]=x/a[i]; x=x%a[i]; } memcpy(num2,num1,sizeof(num1)); //第一种操作 for (i=1;i<=4;i++)swap(num2[i],num2[i+4]); j=sum(num2); if (hash[j]==0) { hash[j]=1; q.push(j); q1.push(s+"A"); } memcpy(num2,num1,sizeof(num1)); //第二种操作 for (i=4;i>=2;i--) { swap(num2[i],num2[i-1]); swap(num2[i+4],num2[i+3]); } j=sum(num2); if (hash[j]==0) { hash[j]=1; q.push(j); q1.push(s+"B"); } memcpy(num2,num1,sizeof(num1)); //第三种操作 swap(num2[2],num2[3]); swap(num2[6],num2[7]); swap(num2[2],num2[7]); j=sum(num2); if (hash[j]==0) { hash[j]=1; q.push(j); q1.push(s+"C"); } }}int main(){ freopen("msquare.in","r",stdin); freopen("msquare.out","w",stdout); init(); bfs(); cout<
<
<
<

 

转载于:https://www.cnblogs.com/Sun-Sea/p/5143775.html

你可能感兴趣的文章
2016级算法第一次练习赛-A.群鸦的盛宴
查看>>
浅谈深度学习和本体间的关系
查看>>
js下载文件
查看>>
python 中的高级函数filter()
查看>>
vim配置
查看>>
python创建系统时间字符串
查看>>
服务器上产看报错的日志的方法
查看>>
软件安装
查看>>
黑盒测试实践—第四天
查看>>
luogu P4448 [AHOI2018初中组]球球的排列
查看>>
win7每天出现taskeng.exe进程的解决方案
查看>>
c++:资源管理(RAII)、new/delete的使用、接口设计与声明、swap函数
查看>>
React Children
查看>>
大数据等最核心的关键技术:32个算法
查看>>
Maven多模块项目搭建
查看>>
redis列表list
查看>>
雷林鹏分享: C# 简介
查看>>
ORA-12505: TNS: 监听程序当前无法识别连接描述符中所给出的SID等错误解决方法
查看>>
实用类-<Math类常用>
查看>>
构建之法阅读笔记之四
查看>>