标签:[1] sizeof struct || query its max app memcpy
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int mod=1e9+7;
const int P=16;
const int mo=1e5;
const int M=800000+5;
int mp[10][8];
int n;
bool kp;
int up=0;
void op(){
for(int i=7;i>=1;i--){
for(int j=1;j<=5;j++){
cout<<mp[i][j]<<" ";
}cout<<endl;
}cout<<endl;
}
struct ha{
int cnt;
ll val[M],nxt[M];
int hd[mo];
void ins(ll x){
int pos=x%mo;
val[++cnt]=x;nxt[cnt]=hd[pos];hd[pos]=cnt;
}
bool query(ll x){
int pos=x%mo;
for(int i=hd[pos];i;i=nxt[i]){
if(val[i]==x) return true;
}return false;
}
}HA;
void down(){
for(int j=1;j<=5;j++){
int top=1;
for(int i=1;i<=7;i++){
if(mp[i][j]){
if(top!=i) mp[top][j]=mp[i][j],mp[i][j]=0;
++top;
}
}
}
}
bool die[10][8];
bool kil(){
memset(die,0,sizeof die);
for(int j=1;j<=5;j++){
int co=0;
int cnt=0;
int st=0;
for(int i=1;i<=7;i++){
if(!mp[i][j]) break;
co=mp[i][j];
st=i;
cnt=0;
while(i<=7&&mp[i][j]==co) cnt++,i++;
i--;
if(cnt>=3){
for(int k=st;k<=i;k++) die[k][j]=1;
}
}
}
for(int i=1;i<=7;i++){
int co=0;
int cnt=0;
int st=0;
for(int j=1;j<=5;j++){
if(!mp[i][j]) break;
co=mp[i][j];
st=j;
cnt=0;
while(j<=5&&mp[i][j]==co) cnt++,j++;
j--;
if(cnt>=3){
for(int k=st;k<=j;k++) die[i][k]=1;
}
}
}
/*cout<<" de "<<endl;
for(int i=7;i>=1;i--){
for(int j=1;j<=5;j++){
cout<<die[i][j]<<" ";
}cout<<endl;
}*/
bool over=false;
for(int i=1;i<=7;i++){
for(int j=1;j<=5;j++){
if(die[i][j]){
over=true;
mp[i][j]=0;
}
}
}
if(over) return true;
return false;
}
struct node{
int x,y,d;
}sta[10],ans[10];
int top;
bool fl;
int las[P];
int nd[P];
int num[P];
int che(int re){
memset(las,0,sizeof las);
memset(nd,0,sizeof nd);
memset(num,0,sizeof num);
ll hsh=0;
int sz=0;
for(int j=1;j<=5;j++){
for(int i=1;i<=7;i++){
hsh=(hsh*P+mp[i][j])%mod;
if(mp[i][j]==0) break;
sz++;
num[mp[i][j]]++;
if(las[mp[i][j]]){
nd[mp[i][j]]+=j-las[mp[i][j]]-1;
}
las[mp[i][j]]=j;
}
}
//cout<<"sz "<<sz<<endl;
if(sz==0) return 2;
if(HA.query(hsh)) return 0;
for(int c=1;c<=up;c++){
if(nd[c]>re) return 0;
if(num[c]==1||num[c]==2) return 0;
}
HA.ins(hsh);
return 1;
}
bool cmp(){
for(int i=1;i<=n;i++){
if(sta[i].x<ans[i].x) return true;
if(sta[i].x>ans[i].x) return false;
if(sta[i].y<ans[i].y) return true;
if(sta[i].y>ans[i].y) return false;
if(sta[i].d>ans[i].d) return true;
if(sta[i].d<ans[i].d) return false;
}
}
int shit=0;
void dfs(int now){
bool kp=false;
if(mp[1][4]==3&&mp[1][5]==2&&mp[2][4]==4&&mp[1][1]==1&&mp[2][5]==3&&now==3) kp=true;
if(kp)cout<<"now "<<now<<endl;
shit++;
//if(shit==1) kp=true;
if(kp) op();
if(now==n+1){
if(che(0)==2){
cout<<"fin"<<endl;
op();
for(int i=1;i<=n;i++)cout<<sta[i].x<<" "<<sta[i].y<<" "<<sta[i].d<<endl;
if(!fl){
fl=true;
for(int i=1;i<=n;i++){
ans[i]=sta[i];
}
}
else if(cmp()){
for(int i=1;i<=n;i++){
ans[i]=sta[i];
}
}
}
return;
}
if(!che(n-now+1)) return;
int tmp[10][8];
memcpy(tmp,mp,sizeof mp);
if(kp) op();
for(int j=1;j<=5;j++){
for(int i=1;i<=7;i++){
if(mp[i][j]){
if(kp)cout<<" ok "<<i<<" "<<j<<endl;
if(j!=1){
swap(mp[i][j],mp[i][j-1]);
if(kp)cout<<" swapped "<<endl;
if(kp)op();
sta[now].x=i,sta[now].y=j;
sta[now].d=-1;
while(1){
down();
//cout<<" down "<<endl;
if(!kil()) break;
}
if(kp)cout<<" to "<<i<<" "<<j<<" "<<sta[now].d<<endl;
if(kp)cout<<" kill "<<endl,op();
dfs(now+1);
sta[now].x=sta[now].y=sta[now].d=0;
memcpy(mp,tmp,sizeof tmp);
}
if(j!=5){
swap(mp[i][j],mp[i][j+1]);
if(kp)cout<<" swapped "<<endl;
if(kp)op();
sta[now].x=i,sta[now].y=j;
sta[now].d=1;
if(kp) cout<<" fid "<<i<<" "<<j<<endl;
while(1){
down();
//if(kp)cout<<"2333 "<<i<<" "<<j<<endl;
if(kp)cout<<" down "<<endl;
if(kp)op();
if(!kil()) break;
}
if(kp)cout<<" to "<<i<<" "<<j<<" "<<sta[now].d<<endl;
if(kp)cout<<" kill"<<endl,op();
dfs(now+1);
sta[now].x=sta[now].y=sta[now].d=0;
memcpy(mp,tmp,sizeof tmp);
}
}
else break;
}
}
}
int main(){
scanf("%d",&n);
for(int j=1;j<=5;j++){
int lp=0;
int t;
while(1){
scanf("%d",&t);
up=max(up,t);
if(t==0) break;
mp[++lp][j]=t;
}
}
cout<<endl;
fl=false;
dfs(1);
if(!fl){
printf("-1");return 0;
}
for(int i=1;i<=n;i++){
printf("%d %d %d\n",ans[i].y-1,ans[i].x-1,ans[i].d);
}
return 0;
}
标签:[1] sizeof struct || query its max app memcpy
原文地址:https://www.cnblogs.com/Miracevin/p/9775432.html