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

题目本身很简单,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. java 16-2 ArrayList的练习2

      需求:去除集合中自定义对象的重复值(对象的成员变量值都相同 注意: 我们按照和字符串一样的操作,发现出问题了. 为什么呢? 我们必须思考哪里会出问题? 通过简单的分析,我们知道问题出现在了判断上. ...

  2. SQL语句统计每天、每月、每年的数据

    1.每年select year(ordertime) 年,sum(Total) 销售合计from 订单表group by year(ordertime) 2.每月select year(orderti ...

  3. POJ 1002 487-3279

    A - 487-3279 Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit ...

  4. 超详细图解:自己架设NuGet服务器

    原文:http://diaosbook.com/Post/2012/12/15/setup-private-nuget-server NuGet 是.NET程序员熟知的给.NET项目自动配置安装lib ...

  5. ie6,ie7兼容性总结(转)

    其实浏览器的不兼容,我们往往是各个浏览器对于一些标准的定义不一致导致的,因此,我们可以进行一些初始化,很多问题都很轻松解决. 下面是14条特殊情况仅供参考: 1. 文字本身的大小不兼容.同样是font ...

  6. usb驱动开发1之学习准备

    此系列是http://blog.csdn.net/fudan_abc/博文的整理,同时加入了自己的理解.很敬佩fudan_abc的文章,仔细学习和分析受益很多.注:fundan_abc所分析linux ...

  7. [tools]QuickPing

    一款神器 quickping 能够很快的探测出该网断分出去哪些地址.   在线的会显示绿色 在线的+有主机名的显示为亮绿色

  8. HashMap 中的 entrySet()使用方法 2016.12.28

    package map; import java.util.HashMap; import java.util.Iterator; import java.util.Map.Entry; import ...

  9. PHP基础01:环境搭建

    1.只会前端的只是有时候让我感到很苦恼,所以决定从今天开始学习后端,看了一些关于后端语言的比较帖子,决定选择php作为我的第一门后端语言.这个是我自己的学习笔记.方便自己复习,不写下来会太无聊了. 第 ...

  10. BibTex参考文献制作

    &1 制作ref.bib文件 在必应学术或者谷歌学术中搜索文章,这里以我硕士的老板和师兄的一篇文章为例: 两种搜索引擎几乎都差不多,一丢丢区别在下面会讲,点击上图中被引数的旁边那个“引用”按钮 ...