真是巨模拟
我的
#include<bits/stdc++.h> using namespace std; #define ll long long ll mp[10][10],sta[10],sta2[10],vis[10],lst[10][10]; ll n,m,x1,x2,yy1,y2,v1,v2,d,k,v,cnt=0,top,top2,ok=0,fen=0,already=0; void print(){ for(ll i=1;i<=n;i++,puts("")) for(ll j=1;j<=n;j++) printf("%lld ",mp[i][j]); } int main(){ // freopen("game_sample2.in","r",stdin); scanf("%lld%lld",&n,&m); scanf("%lld%lld%lld%lld%lld%lld",&x1,&yy1,&v1,&x2,&y2,&v2); mp[x1][yy1]=v1; mp[x2][y2]=v2; for(ll q=1;q<=m;q++){ ok=1; scanf("%lld%lld%lld",&d,&k,&v); for(ll i=1;i<=n;i++) for(ll j=1;j<=n;j++) lst[i][j]=mp[i][j]; if(already) continue ; if(d==0){//向上 for(ll i=1;i<=n;i++){//列 memset(vis,0,sizeof(vis)); memset(sta,0,sizeof(sta)); for(ll j=1;j<=n;j++){//行 if(mp[j][i]) sta[++top]=mp[j][i]; } for(ll j=1;j<=top;j++){ if(!vis[j]&&sta[j]==sta[j+1]){ sta2[++top2]=sta[j]*2; fen+=sta[j]*2; vis[j+1]=1; } else if(!vis[j]) sta2[++top2]=sta[j]; } for(ll j=1;j<=n;j++) mp[j][i]=0; for(ll j=1;j<=top2;j++) mp[j][i]=sta2[j]; top2=0;top=0; } } else if(d==1){//向下 for(ll i=1;i<=n;i++){//列 memset(vis,0,sizeof(vis)); memset(sta,0,sizeof(sta)); for(ll j=n;j>=1;j--){//行 if(mp[j][i]) sta[++top]=mp[j][i]; } for(ll j=1;j<=top;j++){ if(!vis[j]&&sta[j]==sta[j+1]){ sta2[++top2]=sta[j]*2; fen+=sta[j]*2; vis[j+1]=1; } else if(!vis[j]) sta2[++top2]=sta[j]; } for(ll j=1;j<=n;j++) mp[j][i]=0; for(ll j=1;j<=top2;j++) mp[n-j+1][i]=sta2[j]; // print(); // printf("**i=%lld top2=%lld top=%lld\n",i,top2,top); top=0;top2=0; } } else if(d==2){//向左 for(ll i=1;i<=n;i++){//行 memset(vis,0,sizeof(vis)); memset(sta,0,sizeof(sta)); for(ll j=1;j<=n;j++){//列 if(mp[i][j]) sta[++top]=mp[i][j]; } for(ll j=1;j<=top;j++){ if(!vis[j]&&sta[j]==sta[j+1]){ sta2[++top2]=sta[j]*2; fen+=sta[j]*2; vis[j+1]=1; } else if(!vis[j]) sta2[++top2]=sta[j]; } for(ll j=1;j<=n;j++) mp[i][j]=0; for(ll j=1;j<=top2;j++) mp[i][j]=sta2[j]; // print(); // printf("**i=%lld top2=%lld top=%lld\n",i,top2,top); top=0;top2=0; } } else if(d==3){//向右 for(ll i=1;i<=n;i++){//行 memset(vis,0,sizeof(vis)); memset(sta,0,sizeof(sta)); for(ll j=n;j>=1;j--){ if(mp[i][j]) sta[++top]=mp[i][j]; } for(ll j=1;j<=top;j++){ if(!vis[j]&&sta[j]==sta[j+1]){ sta2[++top2]=sta[j]*2; fen+=sta[j]*2; vis[j+1]=1; } else if(!vis[j]) sta2[++top2]=sta[j]; } for(ll j=1;j<=n;j++) mp[i][j]=0; for(ll j=1;j<=top2;j++) mp[i][n-j+1]=sta2[j]; top2=0;top=0; } } // printf("q=%lld\n",q); // print(); for(ll i=1;i<=n;i++) for(ll j=1;j<=n;j++){ if(mp[i][j]!=lst[i][j]){ ok=0; } } // print(); ll cnt=0; for(ll i=1;i<=n;i++){ for(ll j=1;j<=n;j++){ if(!mp[i][j]){ cnt++; } } }if(cnt){ k%=cnt; k++; } else k=1; cnt=0; for(ll i=1;i<=n;i++){ for(ll j=1;j<=n;j++){ if(!mp[i][j]){ cnt++; if(cnt==k) { mp[i][j]=v; goto E; } } } } E:; // print(); if(ok==1){ already=q; } } if(already==0){ printf("%lld\n%lld\n",m,fen); } else printf("%lld\n%lld\n",already-1,fen); }
lsc的2048[专业版]
#include<bits/stdc++.h> using namespace std; #define int long long #define re register inline int read() { int x=0,f=1;char cc=getchar(); while(cc>‘9‘||cc<‘0‘){if(cc==‘-‘)f=-1;cc=getchar();} while(cc>=‘0‘&&cc<=‘9‘){x=(x<<1)+(x<<3)+cc-‘0‘;cc=getchar();} return x*f; } int n,m; int mp[520][520]; int las[520][520]; bool vis[520][520]; int ans=0; bool jud=0; inline bool judge() { for(int i=1;i<=n;i++) { for(int j=1;j<=n;j++) { if(mp[i][j]!=las[i][j])return 0; } } return 1; } inline void debug()//记得删! { for(int i=1;i<=n;i++) { for(int j=1;j<=n;j++) { printf("%lld ",mp[i][j]); } puts(""); } puts(""); return ; } inline void get(int dir) { //0/1/2/3分别代表上下左右! memset(vis,0,sizeof(vis)); for(int i=1;i<=n;i++) { for(int j=1;j<=n;j++) { las[i][j]=mp[i][j]; } } if(dir==0) { for(int i=1;i<=n;i++) { for(int j=1;j<=n;j++) { if(!mp[i][j])continue; int cp=i-1; while(!mp[cp-1][j]&&cp-1>=1)cp--; if(mp[cp][j])cp++; cp=max(cp,1ll); //printf("up %d %d %d %d\n",i,j,cp,j); if(mp[cp-1][j]&&mp[cp-1][j]==mp[i][j]&&!vis[cp-1][j]) { mp[cp-1][j]*=2;ans+=mp[cp-1][j];vis[cp-1][j]=1; mp[cp][j]=mp[i][j]=0; } else { int pc=mp[i][j];mp[i][j]=0;mp[cp][j]=pc; } } } } else if(dir==1) { for(int i=n;i>=1;i--) { for(int j=1;j<=n;j++) { if(!mp[i][j])continue; int cp=i+1; while(!mp[cp+1][j]&&cp+1<=n)cp++; if(mp[cp][j])cp--; cp=min(cp,n); //printf("down %d %d %d %d\n",i,j,cp,j); if(mp[cp+1][j]&&mp[cp+1][j]==mp[i][j]&&!vis[cp+1][j]) { //printf("*2maker_it!\n"); mp[cp+1][j]*=2;ans+=mp[cp+1][j];vis[cp+1][j]=1; mp[i][j]=mp[cp][j]=0; } else { //printf("putong maker_it\n"); int pc=mp[i][j];mp[i][j]=0;mp[cp][j]=pc; } } } } else if(dir==2) { for(int j=1;j<=n;j++) { for(int i=1;i<=n;i++) { if(!mp[i][j])continue; int cp=j-1; while(!mp[i][cp-1]&&cp-1>=1)cp--; if(mp[i][cp])cp++; cp=max(cp,1ll); //printf("left %d %d %d %d \n",i,j,i,cp); if(mp[i][cp-1]&&mp[i][cp-1]==mp[i][j]&&!vis[i][cp-1]) { //printf("type1 out!\n"); mp[i][cp-1]*=2;ans+=mp[i][cp-1];vis[i][cp-1]=1; mp[i][j]=mp[i][cp]=0; } else { //printf("type2 out!\n"); int pc=mp[i][j]; mp[i][j]=0;mp[i][cp]=pc; } } } } else if(dir==3) { for(int j=n;j>=1;j--) { for(int i=1;i<=n;i++) { if(!mp[i][j])continue; int cp=j+1; while(!mp[i][cp+1]&&cp+1<=n)cp++; if(mp[i][cp])cp--; cp=min(cp,n); //printf("right %d %d %d %d \n",i,j,i,cp); if(mp[i][cp+1]&&mp[i][cp+1]==mp[i][j]&&!vis[i][cp+1]) { mp[i][cp+1]*=2;ans+=mp[i][cp+1];vis[i][cp+1]=1; mp[i][j]=mp[i][cp]=0; } else { int pc=mp[i][j];mp[i][j]=0;mp[i][cp]=pc; } } } } //debug();// return ; } inline void make(int pos,int vall) { int ress=0; for(int i=1;i<=n;i++) { for(int j=1;j<=n;j++) { if(!mp[i][j])ress++; } } int poss=1+pos%ress; //printf("%d %d\n",ress,poss); ress=0; for(int i=1;i<=n;i++) { for(int j=1;j<=n;j++) { if(!mp[i][j])ress++; if(ress==poss) { mp[i][j]=vall; //printf("make : %d %d \n",i,j); return ; } } } return ; } signed main() { //freopen("game_sample2.in","r",stdin); //freopen("cnm1.in","r",stdin); //freopen("me1.out","w",stdout); int cpp=0; n=read(),m=read(); int xx,yy,vv; xx=rand()%n+1,yy=rand()%n+1,vv=2;mp[xx][yy]=vv; xx=rand()%n+1,yy=rand()%n+1,vv=2;mp[xx][yy]=vv; //debug();// //0/1/2/3分别代表上下左右! int dir,pos,val; char s; while(1) { cin>>s; if(s==‘w‘)dir=0; if(s==‘s‘)dir=1; if(s==‘a‘)dir=2; if(s==‘d‘)dir=3; pos=rand()%100,val=2; //printf("%d\n",dir); get(dir); //printf("judement %d\n",judge()); if(judge()){puts("YOU HAVE DIED!");break;} cpp++; make(pos,val); debug(); printf("%lld\n",ans); } printf("%d\n%d\n",cpp,ans); return 0; }
原文:https://www.cnblogs.com/znsbc-13/p/11673198.html