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

题目本身很简单,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. man命令中的文本操作

    转自:http://www.cnblogs.com/chengmo/archive/2010/10/26/1861809.html 下面说下less命令操作: 光标移动操作: e  ^E  j  ^N ...

  2. 使用CuteSlider做网站炫酷的幻灯片

    cuteSlider 1.预览 官网:http://www.cuteslider.com/ 应用:http://www.dutphonelab.org/ 2.资料 文档:http://pan.baid ...

  3. mybatis-config.xml详解

    <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE configuration PUBLIC ...

  4. MVC3 使用 FusionCharts 做报表

    环境 VS2010+SQL2008 +MVC3 报表思路 1.新建报表需要的数据表,FusionCharts将直接获取数据表的数据进行展示 2.使用SQL代理,通过存储过程定时生成数据表的数据,只添加 ...

  5. Flash相关知识

    <object id="FlashID" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" wid ...

  6. 在C#代码中应用Log4Net 中配置文件的解释

    一个完整的配置文件的例子如下所示,这个是”在C#代码中应用Log4Net(二)”中使用的配置文件. <log4net> <!-- 错误日志类--> <logger nam ...

  7. 用postgreSQL做基于地理位置的app(zz)

    前言:项目中用到了postgreSQL中的earthdistance()函数功能计算地球上两点之间的距离,中文的资料太少了,我找到了一篇英文的.讲的很好的文章,特此翻译,希望能够帮助到以后用到eart ...

  8. 第10章 系统级I/O

    第10章 系统级I/O 10.1 Unix I/O 一个Unix文件就是一个m个字节的序列:B0,B1,…,BK,…,Bm-1 Unix I/O:一种将设备优雅地映射为文件的方式,允许Unix内核引出 ...

  9. CUDA编程学习(一)

    /****c code****/ #include<stdio.h> int main() { printf("Hello world!\n); ; } /****CUDA co ...

  10. 浅谈JS面向对象之创建对象

    hello,everybody,今天要探讨的问题是JS面向对象,其实面向对象呢呢,一般是在大型项目上会采用,不过了解它对我们理解JS语言有很大的意义. 首先什么是面向对象编程(oop),就是用对象的思 ...