c++实现2048

题目链接:2048

这个已经很接近我们平时玩的2048了,稍微有点不同的是在每次移动以后,新出现的格子并不是随机的,这个稍微改一下就行

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 103;
ll mp[N][N];
ll st[N],top,flag[N];
int n;
int main(){
	scanf("%d",&n);
	int k,m;
	scanf("%d",&k);
	ll val,x,y;
	for(int i = 1; i <= k; i++){
		scanf("%lld%d%d",&val,&x,&y);
		mp[x][y]=val;
	}
	char op[3];
	ll ans = 0;
	scanf("%d",&m);
	for(int v = 1; v <= m; v++){
		scanf("%s%lld%d%d",op,&val,&x,&y);
		if(op[0]=='R'){
			for(int i = 1; i <= n; i++){
				top=0;
				for(int j = n; j >= 1; j--){
					if(mp[i][j]){
						st[++top]=mp[i][j];
						flag[top]=0;
						if(top>1&&st[top]==st[top-1]&&!flag[top-1])
						top--,st[top]*=2ll,flag[top]=1,ans+=st[top];
						mp[i][j]=0;
					}
				}
				for(int j = n-top+1,g = top; j <= n; j++,g--)
				mp[i][j]=st[g];
			}
		}else if(op[0]=='L'){
			for(int i = 1; i <= n; i++){
				top=0;
				for(int j = 1; j <= n; j++){
					if(mp[i][j]){
						st[++top]=mp[i][j];
						flag[top]=0;
						if(top>1&&st[top]==st[top-1]&&!flag[top-1])
						top--,st[top]*=2ll,flag[top]=1,ans+=st[top];
						mp[i][j]=0;
					}
				}
				for(int j = 1; j <= top; j++) mp[i][j]=st[j];
			}
		}else if(op[0]=='D'){
			for(int i = 1; i <= n; i++){//列 
			 	top=0;
				for(int j = n; j >= 1; j--){
					if(mp[j][i]){
						st[++top]=mp[j][i];
						flag[top]=0;
						if(top>1&&st[top]==st[top-1]&&!flag[top-1])
						top--,st[top]*=2ll,flag[top]=1,ans+=st[top];
						mp[j][i]=0;
					}
				}
				for(int j = n-top+1,g = top; j <= n; j++,g--)
				mp[j][i]=st[g];
			}
		}else if(op[0]=='U'){
			for(int i = 1; i <= n; i++){
				top=0;
				for(int j = 1; j <= n; j++){
					if(mp[j][i]){
						st[++top]=mp[j][i];
						flag[top]=0;
						if(top>1&&st[top]==st[top-1]&&!flag[top-1])
						top--,st[top]*=2ll,flag[top]=1,ans+=st[top];
						mp[j][i]=0;
					} 
				}
				for(int j = 1; j <= top; j++) mp[j][i]=st[j];
			}
		}
		mp[x][y]=val;
	}
	printf("%lld\n",ans);
	return 0;
}

/*
6
5
2 1 2
2 1 3
4 1 5
4 1 6
2 3 1
2
L 4 2 1
*/
原创文章 85 获赞 103 访问量 2491

猜你喜欢

转载自blog.csdn.net/weixin_43824564/article/details/105761680
今日推荐