1 条题解

  • 0
    @ 2021-6-15 1:40:17

    C++ :

    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    #include<vector>
    using namespace std;
    #define op(i,n) for(int i=0;i<n;i++)
    #define ed(i,n) for(int i=n-1;i>=0;i--)
    int n,nn,cnt[20],x[20],y[20],g[20];
    void printState(int sz) {for(int i=1;i<=sz;i++)printf("%d %d %d\n",x[i],y[i],g[i]);}
    struct State {
    	int mp[5][7],sz[5],step;
    	State() {
    		memset(mp,0,sizeof(mp));
    		memset(sz,0,sizeof(sz));
    		step=0;
    	}
    	void input() {
    		op(i,5) {
    			int w;
    			while(true) {
    				scanf("%d",&w);
    				if(!w)break;
    				cnt[w]++;
    				mp[i][sz[i]++]=w;
    			}
    		}
    	}
    	bool isFinal() {
    		op(i,5)if(sz[i])return false;
    		return true;
    	}
    	void nextState(int c,int r,int k) {
    		step++;
    		if(sz[c]<=r) {step=n+1;return;}
    		if(k==1) {
    			if(c==4) {step=n+1;return;}
    		} else {
    			if(c==0) {step=n+1;return;}
    		}
    		if(sz[c+k]>r)swap(mp[c][r],mp[c+k][r]);
    		else {
    			mp[c+k][sz[c+k]++]=mp[c][r];
    			for(int j=r;j<sz[c];j++)mp[c][j]=mp[c][j+1];
    			sz[c]--; mp[c][sz[c]]=0;
    		}
    		bool flag=1,vis[5][7];
    		while(flag) {
    			flag=0;
    			memset(vis,0,sizeof(vis));
    			op(i,5)op(j,sz[i]) {
    				int col=mp[i][j];
    				int k=i;
    				while(k<5&&mp[k][j]==col)k++;
    				if(k-i>2)for(int x=i;x<k;x++)vis[x][j]=1;
    				k=j;
    				while(k<sz[i]&&mp[i][k]==col)k++;
    				if(k-j>2)for(int x=j;x<k;x++)vis[i][x]=1;
    			}
    			op(i,5)ed(j,sz[i]) {
    				if(vis[i][j]) {
    					flag=1;
    					for(int k=j;k<sz[i]-1;k++)mp[i][k]=mp[i][k+1];
    					sz[i]--; mp[i][sz[i]]=0;
    				}
    			}
    		}
    	}
    };
    bool dfs(State s) {
    	if(s.step>n)return false;
    	if(s.step==n)
    		if(s.isFinal()){printState(s.step);return true;}
    		else return false;
    	op(i,5)op(j,s.sz[i])for(int k=1;k>=-1;k-=2) {
    		if(k==-1)if(i==0||s.sz[i-1]>j)continue;
    		if(s.mp[i][j]==s.mp[i+k][j])continue;
    		State ns=s;
    		ns.nextState(i,j,k);
    		x[ns.step]=i; y[ns.step]=j; g[ns.step]=k;
    		if(dfs(ns))return true;
    	}
    	return false;
    }
    
    int main() {
    	while(~scanf("%d",&n)) {
    		memset(cnt,0,sizeof(cnt));
    		State s;
    		s.input();
    		int flag=0;
    		for(int i=1;i<=10;i++)if(cnt[i]&&cnt[i]<3)flag=-1;
    		if(flag==-1||!dfs(s))puts("-1");
    	}
    	return 0;
    }
    

    Pascal :

    program mayan;
    TYPE Tmap=array[0..4,0..7] of shortint;
    var n,i,j,step:longint;
        a:Tmap;
        t:shortint;
        x,y,g:array[1..5] of integer;
    procedure swap(var a,b:shortint);
    var t:shortint;begin t:=a;a:=b;b:=t;end;
    procedure print;
              var i:longint;
              begin
              for i:=1 to step do
                writeln(x[i],' ',y[i],' ',g[i]);
              halt;
              end;
    function  clean(var map:Tmap):boolean;
              var i,j,k:integer;
                  finish:boolean;
                  f:array[0..4,0..6] of boolean;
              begin
              clean:=true;
              fillchar(f,sizeof(f),false);
              for i:=0 to 4 do
                for j:=0 to 6 do
                  if map[i][j]<>0 then
                    begin
                    if(i in [1..3])and
                      (map[i][j]=map[i-1][j])and
                      (map[i][j]=map[i+1][j])
                      then begin
                           f[i][j]:=true;
                           f[i-1][j]:=true;
                           f[i+1][j]:=true;
                           end;
                    if(j in [1..5])and
                      (map[i][j]=map[i][j+1])and
                      (map[i][j]=map[i][j-1])
                      then begin
                           f[i][j]:=true;
                           f[i][j+1]:=true;
                           f[i][j-1]:=true;
                           end;
                    end;
              for i:=0 to 4 do
                for j:=6 downto 0 do
                  if f[i][j] then
                    begin
                    clean:=false;
                    map[i][j]:=0;
                    end;
              end;
    procedure fall(var map:Tmap);
              var i,j,k:integer;
              begin
              for i:=0 to 4 do
                for j:=1 to 6 do
                  if(map[i][j]<>0)and(map[i][j-1]=0)
                    then begin
                         k:=j;
                         while(k>0)and(map[i][k-1]=0)do dec(k);
                         map[i][k]:=map[i][j];map[i][j]:=0;
                         end;
              end;
    function  finish(map:Tmap):boolean;
              var i,j:longint;
              begin
              for i:=0 to 4 do
                for j:=0 to 6 do
                  if map[i][j]<>0
                    then exit(false);
              exit(true);
              end;
    procedure DFS(map:Tmap);
              var i,j,k:integer;
              begin
              if step<>0 then swap(map[x[step]][y[step]],map[x[step]+g[step]][y[step]]);
              repeat fall(map) until clean(map);
              if step=n then if finish(map)then print
                                           else exit;
              for i:=0 to 4 do
                for j:=0 to 6 do
                  begin
                  if map[i][j]<>0
                    then begin
                         inc(step);
                         if(i>0)and(map[i-1][j]=0)
                           then begin
                                x[step]:=i;
                                y[step]:=j;
                                g[step]:=-1;
                                DFS(map);
                                end;
                         if(i<4)
                           then begin
                                x[step]:=i;
                                y[step]:=j;
                                g[step]:=1;
                                DFS(map);
                                end;
                         dec(step);
                         end;
                  end;
              end;
    begin
    readln(n);step:=0;
    for i:=0 to 4 do
      begin
      t:=-1;
      repeat
        inc(t);
        read(a[i][t]);
      until a[i][t]=0;
      end;
    DFS(a);
    writeln('-1');
    end.
    
    • 1

    信息

    ID
    290
    时间
    3000ms
    内存
    128MiB
    难度
    (无)
    标签
    递交数
    0
    已通过
    0
    上传者