题意:给你一个骰子的初始状态和可以进行的四种操作,求从初始状态到目标状态的最少操作次数

题目本身很简单,bfs即可。但是因为骰子有六个面,搜索判重和记录状态比较麻烦。这时候就需要神器STL了。

 #include <iostream>
#include <map>
#include <queue>
#include <vector>
#include <algorithm>
#include <cstdio>
#include <cstring>
using namespace std; struct node
{
vector<int> seq;
int step;
node(vector<int> x,int m):seq(x),step(m)
{}
}; int a[],b[];
vector<int> A;
vector<int> y;
//queue<node> Q;
map<vector<int>,int> M;
bool ok; void writeln(vector<int> x,node y)
{
for (int i=;i<;i++)
cout<<x[i]<<" ";
cout<<" - "<<y.step<<endl;
} bool satisfy()
{
for (int i=;i<;i++)
if (y[i]!=b[i]) return false;
ok=true;
return true;
} void lft()
{
vector<int> t;
t=y;
y[]=t[]; y[]=t[]; y[]=t[];
y[]=t[]; y[]=t[]; y[]=t[];
}
void rht()
{
vector<int> t;
t=y;
y[]=t[]; y[]=t[]; y[]=t[];
y[]=t[]; y[]=t[]; y[]=t[];
}
void fnt()
{
vector<int> t;
t=y;
y[]=t[]; y[]=t[]; y[]=t[];
y[]=t[]; y[]=t[]; y[]=t[];
}
void bak()
{
vector<int> t;
t=y;
y[]=t[]; y[]=t[]; y[]=t[];
y[]=t[]; y[]=t[]; y[]=t[];
} bool same()
{
for (int i=;i<;i++)
if (a[i]!=b[i]) return false;
return true;
} int main()
{
//freopen("in.txt","r",stdin); while (cin>>a[])
{
A.clear();
M.clear();
//Q.clear();
queue <node> Q;
ok=false; A.push_back(a[]);
for (int i=;i<;i++)
{
cin>>a[i];
A.push_back(a[i]);
}
for (int i=;i<;i++)
cin>>b[i];
if (same())
{
cout<<<<endl;
continue;
} Q.push(node(A,));
//int nm=1;
M.insert(pair<vector<int>,int>(A,)); while (!Q.empty())
{
node tmp=Q.front();
Q.pop();
int st=tmp.step;
st++;
y=tmp.seq;
//writeln(y,tmp); ////////
if (satisfy())
{
cout<<st-<<endl;
break;
}
/*
if (st>55)
{
cout<<-1<<endl;
break;
}*/
for (int tm=;tm<=;tm++)
{
if (tm==)
{
y=tmp.seq;
lft();
if (!M.count(y))
{
M.insert(pair<vector<int>,int>(y,));
Q.push(node(y,st));
}
}
if (tm==)
{
y=tmp.seq;
rht();
if (!M.count(y))
{
M.insert(pair<vector<int>,int>(y,));
Q.push(node(y,st));
}
}
if (tm==)
{
y=tmp.seq;
fnt();
if (!M.count(y))
{
M.insert(pair<vector<int>,int>(y,));
Q.push(node(y,st));
}
}
if (tm==)
{
y=tmp.seq;
bak();
if (!M.count(y))
{
M.insert(pair<vector<int>,int>(y,));
Q.push(node(y,st));
}
}
}
}
if (!ok) cout<<-<<endl;
} }

本题中用到的操作:

用map作为哈希表判重:

map<vector<int>,int>,这样就把一个vector容器和一个整数关联起来了。

map对象的.count(x)函数:返回x在哈希表中的出现次数,未出现则返回0。用这个函数就可以判重了。

queue:队列

注意queue和stack没有.clear()函数,所以用完之后没法清空,只能建一个新的(三次都WA到这里了,对拍时才发现T^T)

还有结构体声明里面的node(vector<int> x,int m)那个东西是结构体构造函数,neopenx大神教的,可以避免代码太屎

附上neopenx大神的AC代码,Orz

 #include "cstdio"
#include "queue"
#include "vector"
#include "map"
using namespace std;
int a[],b[];
vector<int> B;
map<vector<int>,int> HASH;
bool ok=false;
struct node
{
vector<int> X;
int num;
node(vector<int> x,int n): X(x),num(n) {}
};
void bfs()
{
vector<int> A;
for(int i=;i<=;i++) A.push_back(a[i]);
if(A==B) {printf("%d\n",);ok=true;return;}
queue<node> Q;Q.push(node(A,));
while(!Q.empty())
{
node x=Q.front();Q.pop();
//if(x.num>=8) continue; //没必要对dep剪枝了,目测数据在dep=4的时候,就能全部被hash掉
vector<int> tt=x.X;
for(int s=;s<;s++)
{
int flag=x.num;
if(s==)
{
int a=tt[],b=tt[],c=tt[],d=tt[];
vector<int> C;
C.push_back(c);C.push_back(d);C.push_back(b);C.push_back(a);
C.push_back(tt[]);C.push_back(tt[]);
if(C==B)
{
printf("%d\n",++flag);
ok=true;
return;
}
else
{
if(HASH.count(C)) continue;
else
{
HASH[C]=;
Q.push(node(C,++flag));
}
}
}
if(s==)
{
int a=tt[],b=tt[],c=tt[],d=tt[];
vector<int> C;
C.push_back(d);C.push_back(c);C.push_back(a);C.push_back(b);
C.push_back(tt[]);C.push_back(tt[]);
if(C==B)
{
printf("%d\n",++flag);
ok=true;
return;
}
else
{
if(HASH.count(C)) continue;
else
{
HASH[C]=;
Q.push(node(C,++flag));
}
}
}
if(s==)
{
int a=tt[],b=tt[],c=tt[],d=tt[];
vector<int> C;
C.push_back(c);C.push_back(d);
C.push_back(tt[]);C.push_back(tt[]);
C.push_back(b);C.push_back(a);
if(C==B)
{
printf("%d\n",++flag);
ok=true;
return;
}
else
{
if(HASH.count(C)) continue;
else
{
HASH[C]=;
Q.push(node(C,++flag));
}
}
}
if(s==)
{
int a=tt[],b=tt[],c=tt[],d=tt[];
vector<int> C;
C.push_back(d);C.push_back(c);
C.push_back(tt[]);C.push_back(tt[]);
C.push_back(a);C.push_back(b);
if(C==B)
{
printf("%d\n",++flag);
ok=true;
return;
}
else
{
if(HASH.count(C)) continue;
else
{
HASH[C]=;
Q.push(node(C,++flag));
}
}
}
}
}
}
int main()
{
//freopen("in.txt","r",stdin);
while(~scanf("%d",&a[]))
{
for(int i=;i<=;i++)
scanf("%d",&a[i]);
for(int i=;i<=;i++)
{
scanf("%d",&b[i]);
B.push_back(b[i]);
}
bfs();
if(!ok) printf("-1\n");
B.clear();
HASH.clear();
ok=false;
}
}

2014ACMICPC西安网赛1006的更多相关文章

  1. 2017 ACM-ICPC西安网赛B-Coin

    B-Coin Bob has a not even coin, every time he tosses the coin, the probability that the coin's front ...

  2. HDU 5047 Sawtooth(大数模拟)上海赛区网赛1006

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5047 解题报告:问一个“M”型可以把一个矩形的平面最多分割成多少块. 输入是有n个“M",现 ...

  3. 2013长春网赛 1006 hdu 4764 Stone(巴什博弈)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4764 题意:Tang 和 Jiang 玩一个游戏,轮流写下一个数,Tang先手,第一次Tang只能写[ ...

  4. hdu 5011 nim博弈 (2014西安网赛E题)

    n堆石子,每次可以选一堆取走至少一个,之后你可以不操作或者把该堆石子分成两堆,每堆至少一个,和还是原来(取完石子后)的石子个数. Sample Input1121 131 2 3 Sample Out ...

  5. hdu 5007 水题 (2014西安网赛A题)

    题意:出现Apple.iPod.iPhone.iPad时输出MAI MAI MAI!,出现Sony,输出SONY DAFA IS GOOD! Sample InputApple bananaiPad ...

  6. ACM学习历程—HDU 5012 Dice(ACM西安网赛)(bfs)

    Problem Description There are 2 special dices on the table. On each face of the dice, a distinct num ...

  7. ACM学习历程——HDU 5014 Number Sequence (贪心)(2014西安网赛)

    Description There is a special number sequence which has n+1 integers. For each number in sequence, ...

  8. ACM学习历程——HDU5017 Ellipsoid(模拟退火)(2014西安网赛K题)

    ---恢复内容开始--- Description Given a 3-dimension ellipsoid(椭球面) your task is to find the minimal distanc ...

  9. 大连网络赛 1006 Football Games

    //大连网络赛 1006 // 吐槽:数据比较水.下面代码可以AC // 但是正解好像是:排序后,前i项的和大于等于i*(i-1) #include <bits/stdc++.h> usi ...

随机推荐

  1. 关于phpmyadmin #1045无法登陆服务器的问题

    修改/home/wwwroot/phpmyadmin/libraries/config.default.php 文件,找到下面两行 $cfg['Servers'][$i]['nopassword'] ...

  2. Nginx反向代理+负载均衡简单实现(http方式)

    1)nginx的反向代理:proxy_pass2)nginx的负载均衡:upstream 下面是nginx的反向代理和负载均衡的实例: 负载机:A机器:103.110.186.8/192.168.1. ...

  3. 802.1x协议&eap类型

    EAP: 0,扩展认证协议 1,一个灵活的传输协议,用来承载任意的认证信息(不包括认证方式) 2,直接运行在数据链路层,如ppp或以太网 3,支持多种类型认证 注:EAP 客户端---服务器之间一个协 ...

  4. iBatis.Net(C#)数据库查询

    引用请注明http://www.cnblogs.com/13590/archive/2013/03/14/2958735.html  摘要:查询是数据库SQL语言的核心,本文介绍了通过iBatis.N ...

  5. python数字图像处理(11):图像自动阈值分割

    图像阈值分割是一种广泛应用的分割技术,利用图像中要提取的目标区域与其背景在灰度特性上的差异,把图像看作具有不同灰度级的两类区域(目标区域和背景区域)的组合,选取一个比较合理的阈值,以确定图像中每个像素 ...

  6. js copy

    Javascript 实现复制(Copy)动作方法大全 投稿:hebedich 字体:[增加 减小] 类型:转载 时间:2014-06-20我要评论 现在浏览器种类也越来越多,诸如 IE.Firefo ...

  7. [CareerCup] 3.5 Implement Queue using Two Stacks 使用两个栈来实现队列

    3.5 Implement a MyQueue class which implements a queue using two stacks. LeetCode上的原题,请参见我之前的博客Imple ...

  8. HoloLens开发手记 - Unity之Keyboard input 键盘输入

    虽然HoloLens支持很多种输入方式,包括蓝牙键盘在内.但是大部分应用还是不能断定用户有物理键盘可以输入,所以虚拟键盘输入还是必须要提供的. Unity提供了一个TouchScreenKeyboar ...

  9. 疯狂的Java算法——插入排序,归并排序以及并行归并排序

    从古至今的难题 在IT届有一道百算不厌其烦的题,俗称排序.不管是你参加BAT等高端笔试,亦或是藏匿于街头小巷的草根笔试,都会经常见到这样一道百年难得一解的问题. 今天LZ有幸与各位分享一下算法届的草根 ...

  10. 喝咖啡写脚本,顺便再加一点点CSS语法糖 1.选择环境

    经过对前端开发的初步了解,大体上发现了以下几点,前端开发需要使用脚本语言,主要是JavaScript,需要Html,需要CSS,这些东西相信很多人已经很熟了.但是仅仅只是学习一点简单的JS,配合Htm ...