前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >【hdu 6172】Array Challenge(数列、找规律)

【hdu 6172】Array Challenge(数列、找规律)

作者头像
饶文津
发布2020-06-02 15:57:29
4120
发布2020-06-02 15:57:29
举报
文章被收录于专栏:饶文津的专栏饶文津的专栏

多校10 1002 HDU 6172 Array Challenge

题意

题解

代码

代码语言:javascript
复制
#include <cstdio>
#include <map>
#include <cstdlib>
#include <queue>
using namespace std;
#define mem(a,b) memset(a,b,sizeof(a))
#define rep(i,l,r) for (int i=l;i<r;++i)
typedef unsigned long long ull;
int dx[4]={1,1,-1,-1},dy[4]={0,1,0,-1};
map<ull,bool>vis;
struct Sta{
	int a[6][6],step,x,y;
	Sta(){step=x=y=0;}
};
int gujia(Sta s){
	int ans=0;
	rep(i,0,6)rep(j,0,i+1)
	if(s.a[i][j])ans+=abs(s.a[i][j]-i);
	return ans;
}
ull haxi(Sta s){
	ull ans=0;
	rep(i,0,6)rep(j,0,i+1){
		ans<<=3;ans|=s.a[i][j];
	}
	return ans;
}
int bfs(Sta s){
	vis.clear();
	queue<Sta>q;q.push(s);
	while(!q.empty()){
		Sta now=q.front();q.pop();
		if(gujia(now)==0)return now.step;
		rep(i,0,4){
			int x=now.x,y=now.y;
			int nx=x+dx[i],ny=y+dy[i];
			if(nx>=0 && nx<6 && ny>=0 && ny<=nx){
				swap(now.a[x][y],now.a[nx][ny]);
				now.x=nx,now.y=ny,++now.step;
				ull hx=haxi(now);
				if(!vis[hx]&&gujia(now)+now.step<21){
					q.push(now);
					vis[hx]=true;
				}
				swap(now.a[x][y],now.a[nx][ny]);
				now.x-=dx[i],now.y-=dy[i],--now.step;
			}
		}
	}
	return -1;
}
int main() {
	int t;
	scanf("%d",&t);
	while(t--){
		Sta s;
		rep(i,0,6)
		rep(j,0,i+1){
			scanf("%d",&s.a[i][j]);
			if(s.a[i][j]==0)s.x=i,s.y=j;
		}
		int ans=bfs(s);
		if(ans==-1)puts("too difficult");else printf("%d\n",ans);
	}
	return 0;
}
代码语言:javascript
复制
#include <cstdio>
#include <algorithm>
#include <vector>
using namespace std;
#define rep(i,a,n) for (int i=a;i<n;i++)
#define pb push_back
#define SZ(x) ((int)(x).size())
typedef vector<int> VI;
typedef long long ll;
const ll mod=1000000007;
ll qpow(ll a,ll b) {ll res=1;for(a%=mod;b;b>>=1,a=a*a%mod)if(b&1)res=res*a%mod;return res;}
VI BM(VI s) {//c[0]s[0]+c[1]s[1]+...=0
	VI C(1,1),B(1,1);
	int L=0,m=1,rev=1;
	rep(n,0,SZ(s)) {
		ll d=0;
		rep(i,0,L+1) d=(d+(ll)C[i]*s[n-i])%mod;
		if (d==0) ++m;
		else if (2*L<=n) {
			VI T=C;
			ll c=mod-d*rev%mod;
			C.resize(SZ(B)+m);
			rep(i,0,SZ(B)) C[i+m]=(C[i+m]+c*B[i])%mod;
			L=n+1-L; B=T; rev=qpow(d,mod-2); m=1;
		} else {
			ll c=mod-d*rev%mod;
			C.resize(SZ(B)+m);
			rep(i,0,SZ(B)) C[i+m]=(C[i+m]+c*B[i])%mod;
			++m;
		}
	}
	rep(i,0,SZ(C))printf("%dx[%d]%s",C[i],i,i+1==SZ(C)?"=0\n":"+");
	return C;
}
调用:BM(VI{31,197,1255,7997})
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2017-08-29 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 题意
  • 题解
  • 代码
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档