2014ACMICPC西安网赛1006
题意:给你一个骰子的初始状态和可以进行的四种操作,求从初始状态到目标状态的最少操作次数
题目本身很简单,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的更多相关文章
- 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 ...
 - HDU 5047 Sawtooth(大数模拟)上海赛区网赛1006
		
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5047 解题报告:问一个“M”型可以把一个矩形的平面最多分割成多少块. 输入是有n个“M",现 ...
 - 2013长春网赛 1006 hdu 4764	 Stone(巴什博弈)
		
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4764 题意:Tang 和 Jiang 玩一个游戏,轮流写下一个数,Tang先手,第一次Tang只能写[ ...
 - hdu 5011 nim博弈 (2014西安网赛E题)
		
n堆石子,每次可以选一堆取走至少一个,之后你可以不操作或者把该堆石子分成两堆,每堆至少一个,和还是原来(取完石子后)的石子个数. Sample Input1121 131 2 3 Sample Out ...
 - hdu 5007  水题 (2014西安网赛A题)
		
题意:出现Apple.iPod.iPhone.iPad时输出MAI MAI MAI!,出现Sony,输出SONY DAFA IS GOOD! Sample InputApple bananaiPad ...
 - 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 ...
 - ACM学习历程——HDU 5014 Number Sequence (贪心)(2014西安网赛)
		
Description There is a special number sequence which has n+1 integers. For each number in sequence, ...
 - ACM学习历程——HDU5017 Ellipsoid(模拟退火)(2014西安网赛K题)
		
---恢复内容开始--- Description Given a 3-dimension ellipsoid(椭球面) your task is to find the minimal distanc ...
 - 大连网络赛 1006 Football Games
		
//大连网络赛 1006 // 吐槽:数据比较水.下面代码可以AC // 但是正解好像是:排序后,前i项的和大于等于i*(i-1) #include <bits/stdc++.h> usi ...
 
随机推荐
- maven总结1
			
环境:win7 maven版本:apache-maven-3.1.1-bin.zip maven安装 1.确定已经正确安装jdk,若未安装需要先安装jdk 2.http://maven.apac ...
 - 狮子和计算Java题
			
package cn.bdqn.com; import java.util.Scanner; public class Jisaunqi { int num1; int num2; int jiegu ...
 - 20Mybatis_订单商品数据模型_一对一查询——resultType和resultMap两种方式以及两种方式的总结
			
上一篇文章分析了数据模型,这篇文章就给出一个需求,这个需求是一对一查询,并完成这个需求. ------------------------------------------------------- ...
 - PHP版本VC6和VC9、Non Thread Safe和Thread Safe的区别
			
链接:http://www.cnblogs.com/neve/articles/1863853.html 想更新个PHP的版本,PHP的windows版本已经分离出来了,见http://windows ...
 - 最近火到不行的微信小程序的常识
			
满网都是微信小程序,技术dog们不关注都不行了.先别忙着去学怎么开发小程序,先纠正一下你对微信小程序的三观吧~~~~ 小程序目前被炒得沸沸扬扬,无数媒体和企业借机获取阅读流量. 这再次证明一点,微信想 ...
 - 对兼容ie浏览器所遇到的问题及总结
			
1,若直接给一个元素设置absolute定位.在浏览器缩放的时候.位置会错位.解决的方法是给外层的元素设置为relative定位. 2,低版本ie浏览器不支持placeholder属性 3,盒模型上规 ...
 - 在Linux用libcurl.a在链接的时候出错
			
其实出错是因为curl链接的时候需要别的库.我用如下方法解决 1.http://curl.haxx.se/download/curl-7.45.0.tar.gz官网下载源码 2../configure ...
 - 信息安全系统设计基础exp_4
			
北京电子科技学院(BESTI) 实 验 报 告 课程:信息安全系统设计基础 班级:1353 姓名:郑伟.吴子怡 学号:20135322.20135313 指导教师: 娄嘉鹏 实验 ...
 - iOS——数据安全性问题小结
			
在移动互联网快速发展的今天,iOS应用直接运行在用户的手机上,与运行在服务器后台服务相比,更有可能被黑客攻击. a.网络安全: 1.1 安全地传输用户密码 事先生成一对用于加密的公私钥,客户端登录的时 ...
 - RxJava简介
			
RxJava简介 本文为前段时间学习RxJava时留下的历史遗留笔记,仅作纪念,科学的大神教学帖子在这里-> 给 Android 开发者的 RxJava 详解 通过链式调用序列实现基于事件流的异 ...