标签:
#include <bits/stdc++.h> using namespace std; int vis1[7]; int vis2[7][7]; int vis3[7][7][7]; int vis4[7][7][7][7]; int vis5[7][7][7][7][7]; int vis6[7][7][7][7][7][7]; int vis7[7][7][7][7][7][7][7]; struct node{ int a[7]; int step; }; bool check(int a[],int num,int step){ //检查有没有走过 if(num==1){ if(vis1[a[0]]>=0) return true; else{vis1[a[0]]=step;return false;} } if(num==2){ if(vis2[a[0]][a[1]]>=0) return true; else {vis[a[0]][a[1]]=step;return false;} } if(num==3){ if(vis3[a[0]][a[1]][a[2]]>=0) return true; else {vis[a[0]][a[1]][a[2]]=step;return false;} } if(num==4){ if(vis4[a[0]][a[1]][a[2]][a[3]]>=0) return true; else {vis[a[0]][a[1]][a[2]][a[3]]=step;return false;} } if(num==5){ if(vis5[a[0]][a[1]][a[2]][a[3]][a[4]]>=0) return true; else {vis5[a[0]][a[1]][a[2]][a[3]][a[4]]=step;return false;} } if(num==6){ if(vis6[a[0]][a[1]][a[2]][a[3]][a[4]][a[5]]>=0) return true; else {vis6[a[0]][a[1]][a[2]][a[3]][a[4]][a[5]]=step;return false;} } if(num==7){ if(vis7[a[0]][a[1]][a[2]][a[3]][a[4]][a[5]][a[6]]>=0) return true; else {vis7[a[0]][a[1]][a[2]][a[3]][a[4]][a[5]][a[6]]=step;return false;} } } void inite(){ memset(vis1,-1,sizeof(vis1)); memset(vis2,-1,sizeof(vis2)); memset(vis3,-1,sizeof(vis3)); memset(vis4,-1,sizeof(vis4)); memset(vis5,-1,sizeof(vis5)); memset(vis6,-1,sizeof(vis6)); memset(vis7,-1,sizeof(vis7)); } void bfs(int cc[],int num){ node k;k.step=0;int b[7]; for(int i=0;i<num;i++){k.a[i]=i;} check(a,num,0); queue<node> que;que.push(); while(!que.empty()){ node top=que.top();que.pop(); for(int i=0;i<num;++i) b[i]=top.a[i]; for(int i=0;i<num;++i){ int l=b[i]-1; int r=b[i]+1; if(l<0) l=-1; if(r>=num) r=-1; for(int j=0;j<i;++j) if(b[j]==b[i]){l=r=-1;} for(int j=0;j<i;++j) if(b[j]==b[i]-1) {l=-1;} for(int j=0;j<i;++j) if(b[j]==b[i]+1) {r=-1;} if(l!=-1){ int temp=b[i]; b[i]=l; node s; for(int j=0;j<num;++j) {s.a[j]=a[j];} s.step=top.step; if(!check(a,num,s.step)){ que.push(s); } b[i]=temp; } if(r!=-1){ int temp=b[i]; b[i]=r; node s; for(int j=0;j<num;++j) {s.a[j]=a[j];} s.step=top.step; if(!check(a,num,s.step)){ que.push(s); } b[i]=temp; } } } } int main(){ int T; for(scanf("%d")) return 0; }
北京网络赛G BOXES 状态压缩+有序BFS+高维数组判重
标签:
原文地址:http://www.cnblogs.com/linkzijun/p/5280903.html