1 条题解

  • 0
    @ 2021-6-14 23:29:47

    C++ :

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    using namespace std;
    int n,zm[30],c[5][31];
    char c1;
    bool f[30],F;
    bool jc()
    {
    	int p=0;
    	for(int i=n-1;i>=0;--i)
    	{
    	  if (zm[c[1][i]]==-1||zm[c[2][i]]==-1||zm[c[3][i]]==-1)return false;
    	  int x=zm[c[1][i]]+zm[c[2][i]]+p;
    	  if(x%n!=zm[c[3][i]])return true;
    	  p=x/n;
    	}
    	return false;
    }
    bool jc2()
    {
    	for(int i=n-1;i>=0;--i)//a+b?c
    	  if(zm[c[1][i]]!=-1&&zm[c[2][i]]!=-1&&zm[c[3][i]]!=-1)
    	  {
    		int x=(zm[c[1][i]]+zm[c[2][i]])%n;
    		if(!(x%n==zm[c[3][i]]||(x+1)%n==zm[c[3][i]]))return true;
    	  }
    	for(int i=n-1;i>=0;--i)//a+?=c
    	  if(zm[c[1][i]]!=-1&&zm[c[2][i]]==-1&&zm[c[3][i]]!=-1)
    	  {
    		int x=(zm[c[3][i]]-zm[c[1][i]]+n)%n;
    		if(f[x]&&f[(x-1)%n])return true;
    	  }
    	for(int i=n-1;i>=0;--i)//?+b=c
    	  if(zm[c[1][i]]==-1&&zm[c[2][i]]!=-1&&zm[c[3][i]]!=-1)
    	  {
    		int x=(zm[c[3][i]]-zm[c[2][i]]+n)%n;
    		if(f[x]&&f[(x-1)%n])return true;
    	  }
    	for(int i=n-1;i>=0;--i)//a+b=?
    	  if(zm[c[1][i]]!=-1&&zm[c[2][i]]!=-1&&zm[c[3][i]]==-1)
    	  {
    		int x=(zm[c[1][i]]+zm[c[2][i]])%n;
    		if(f[x]&&f[(x+1)%n])return true;
    	  }
    	return false;
    }
    void work(int k,int w)
    {
    	if(F)return;
    	if(jc())return;
    	if(jc2())return;
    	//if(zm[c[1][0]-65]+zm[c[2][0]-65]>n)return;
    	if(k==0&&w==3)
    	{
    	  for(int i=0;i<n-1;++i)printf("%d ",zm[i]);
    	  printf("%d\n",zm[n-1]);F=true;return;
    	}
    	
    	
    	if(zm[c[w][k]]!=-1)
    	{
    	  if(w==3){w=1;--k;}else ++w;
    	  work(k,w);
    	  if(w==1){w=3;++k;}else --w;
    	}
    	else
    	{
    	  for(int i=n-1;i>=0;--i)
    	    if(!f[i])
    	    {
    		  zm[c[w][k]]=i,f[i]=true;
    		  if(w==3){w=1;--k;}else ++w;
    		  work(k,w);
    		  if(w==1){w=3;++k;}else --w;
    		  zm[c[w][k]]=-1;f[i]=false;
    	    }
    	}
    	
    }
    int main()
    {
    	memset(c,0,sizeof(c));
    	scanf("%d",&n);
    	for(int i=1;i<=3;++i)
    	  for(int j=0;j<n;++j)
    	    {cin>>c1;c[i][j]=c1-65;}
    	F=false;
    	memset(f,0,sizeof(f));
    	for(int i=0;i<n;++i)zm[i]=-1;
    	work(n-1,1);
    	return 0;
    }
    
    

    Pascal :

    var
      a:Array[1..3,1..26]of char;
      b:array['A'..'Z']of longint;
      c:array[0..25]of boolean;
      i,j,n:longint;
    function check(x,m:longint):boolean;
    var
      yy:boolean;
      i,j:longint;
    begin
      if ((b[a[1,x]]+b[a[2,x]]+m)mod n<>b[a[3,x]]) then exit(false);
      for i:=1 to x-1 do
      if (b[a[1,i]]<>-1)and(b[a[2,i]]<>-1)and(b[a[3,i]]<>-1)then
        begin
          yy:=true;
          for j:=0 to 1 do
            if (b[a[1,i]]+b[a[2,i]]+j)mod n=b[a[3,i]] then yy:=false;
          if yy then exit(false);
        end;
      check:=true;
    end;
    procedure dfs(x,m:longint);
    var i,j,d,p,mm:longint;
        aa,bb,cc:boolean;
    begin
      if x=0 then
        begin
          if m=0 then
            begin
              for i:=1 to n-1 do write(b[chr(i+64)],' ');
              writeln(b[chr(n+64)]);
              halt;
            end;
          exit;
        end;
      aa:=false;
      bb:=false;
      cc:=false;
      if b[a[1,x]]<>-1 then aa:=true;
      if b[a[2,x]]<>-1 then bb:=true;
      if b[a[3,x]]<>-1 then cc:=true;
      if (aa and bb and cc) then
        begin
          if check(x,m) then dfs(x-1,(m+b[a[1,x]]+b[a[2,x]])div n);
        end
        else if aa and bb then
          begin
            d:=(b[a[1,x]]+b[a[2,x]]+m)mod n;
            if c[d] then
              begin
                b[a[3,x]]:=d;
                c[d]:=false;
                mm:=(b[a[1,x]]+b[a[2,x]]+m)div n;
                if check(x,m) then dfs(x-1,mm);
                  c[d]:=true;
                  b[a[3,x]]:=-1;
              end;
          end
        else if aa and cc then
      begin
        d:=(b[a[3,x]]-m-b[a[1,x]]+n)mod n;
        if c[d] then
        begin
          b[a[2,x]]:=d;
          c[d]:=false;
          mm:=(d+b[a[1,x]]+m)div n;
          if check(x,m) then dfs(x-1,mm);
          m:=mm;
          c[d]:=true;
          b[a[2,x]]:=-1;
        end;
      end
      else if bb and cc then
        begin
          d:=(b[a[3,x]]-m-b[a[2,x]]+n)mod n;
            if c[d] then
              begin
                b[a[1,x]]:=d;
                c[d]:=false;
                mm:=(d+b[a[2,x]]+m)div n;
                if check(x,m) then dfs(x-1,mm);
                m:=mm;
                c[d]:=true;
                b[a[1,x]]:=-1;
              end;
        end
      else if (not aa)and(not bb)and(not cc) then
        begin
          if a[1,x]<>a[2,x] then
            begin
              for i:=n-1 downto 0 do
                for j:=n-1 downto 0 do
                  if (c[i] and c[j])and(i<>j) then
                    begin
                      c[i]:=false;
                      c[j]:=false;
                      b[a[1,x]]:=j;
                      b[a[2,x]]:=i;
                      dfs(x,m);
                      c[i]:=true;
                      c[j]:=true;
                      b[a[1,x]]:=-1;
                      b[a[2,x]]:=-1;
                    end;
        end
    else
    for i:=n-1 downto 0 do
    if c[i] then
    begin
      b[a[1,x]]:=i;
      c[i]:=false;
      dfs(x,m);
      b[a[p,x]]:=-1;
      c[i]:=true;
    end;
    end
    else begin
      for i:=n-1 downto 0 do
      if c[i] then
        begin
          if not aa then p:=1
          else if not bb then p:=2
          else if not cc then p:=3;
          b[a[p,x]]:=i;
          c[i]:=false;
          dfs(x,m);
          b[a[p,x]]:=-1;
          c[i]:=true;
        end;
          end;
    end;
    begin
      readln(n);
      for i:=1 to 3 do
        begin
          for j:=1 to n do
            read(a[i,j]);
          readln;
        end;
      fillchar(b,sizeof(b),255);
      fillchar(c,sizeof(c),true);
      dfs(n,0);
    end.
    
    
    • 1