zoukankan      html  css  js  c++  java
  • CF1065D (Wa)Three Pieces

    这道题我Wa了。。。

    调不过来。。。

    思路很暴力

    但我就是写不出来

    比较好些的方法是状态间建边最短路(bfs)

    我用的是记忆化搜索,但代码能力极弱未能实现

    卡在了CF34点

    我太蒻了。。。

    希望路过的大佬指出错误!

      1 //0为车,1为象,2为马 
      2 #include<cstdio>
      3 #include<cstring>
      4 #include<algorithm>
      5 using namespace std;
      6 int n,m,cnt,tot,x,y,ans1,ans2;
      7 int mp[12][12];
      8 int hrx[8]={1,2,-2,1,-2,2,-1,-1};
      9 int hry[8]={2,1,1,-2,-1,-1,2,-2};
     10 bool vis[102][12][12][3];
     11 int f[102][12][12][3];
     12 int g[102][12][12][3];
     13 int dfs(int x,int y,int typ,int fr){
     14     if((mp[x][y]==n*n)&&(fr==n*n-1)){
     15         g[fr][x][y][typ]=0;
     16         return f[fr][x][y][typ]=0;
     17     }
     18     if(f[fr][x][y][typ]!=0x3f3f3f3f)return f[fr][x][y][typ];
     19     vis[fr][x][y][typ]=true;
     20     int now=(mp[x][y]==fr+1)?mp[x][y]:fr;
     21     if(typ==0){
     22         for(int i=1;i<=n;i++){
     23             if((i!=x)&&((!vis[now][i][y][typ])||(f[now][i][y][typ]!=0x3f3f3f3f))){
     24                 int tmp=dfs(i,y,typ,now)+1;
     25                 if(tmp<f[fr][x][y][typ]){
     26                     f[fr][x][y][typ]=tmp;
     27                     g[fr][x][y][typ]=g[now][i][y][typ];
     28                 }
     29                 else if(tmp==f[fr][x][y][typ]){
     30                     g[fr][x][y][typ]=min(g[now][i][y][typ],g[fr][x][y][typ]);
     31                 }
     32             }
     33             if((i!=y)&&((!vis[now][x][i][typ])||(f[now][x][i][typ]!=0x3f3f3f3f))){
     34                 int tmp=dfs(x,i,typ,now)+1;
     35                 if(tmp<f[fr][x][y][typ]){
     36                     f[fr][x][y][typ]=tmp;
     37                     g[fr][x][y][typ]=g[now][x][i][typ];
     38                 }
     39                 else if(tmp==f[fr][x][y][typ]){
     40                     g[fr][x][y][typ]=min(g[now][x][i][typ],g[fr][x][y][typ]);
     41                 }
     42             }
     43         }
     44     }
     45     if(typ==1){
     46         for(int i=1;i<=n;i++){
     47             if((x>=i+1)&&(y>=i+1)){
     48                 if((!vis[now][x-i][y-i][typ])||(f[now][x-i][y-i][typ]!=0x3f3f3f3f)){
     49                     int tmp=dfs(x-i,y-i,typ,now)+1;
     50                     if(tmp<f[fr][x][y][typ]){
     51                         f[fr][x][y][typ]=tmp;
     52                         g[fr][x][y][typ]=g[now][x-i][y-i][typ];
     53                     }
     54                     else if(tmp==f[fr][x][y][typ]){
     55                         g[fr][x][y][typ]=min(g[fr][x][y][typ],g[now][x-i][y-i][typ]);
     56                     }
     57                 }
     58             }
     59             if((x+i<=n)&&(y+i<=n)){
     60                 if((!vis[now][x+i][y+i][typ])||(f[now][x+i][y+i][typ]!=0x3f3f3f3f)){
     61                     int tmp=dfs(x+i,y+i,typ,now)+1;
     62                     if(tmp<f[fr][x][y][typ]){
     63                         f[fr][x][y][typ]=tmp;
     64                         g[fr][x][y][typ]=g[now][x+i][y+i][typ];
     65                     }
     66                     else if(tmp==f[fr][x][y][typ]){
     67                         g[fr][x][y][typ]=min(g[fr][x][y][typ],g[now][x+i][y+i][typ]);
     68                     }
     69                 }
     70             }
     71             if((x>=i+1)&&(y+i<=n)){
     72                 if((!vis[now][x-i][y+i][typ])||(f[now][x-i][y+i][typ]!=0x3f3f3f3f)){
     73                     int tmp=dfs(x-i,y+i,typ,now)+1;
     74                     if(tmp<f[fr][x][y][typ]){
     75                         f[fr][x][y][typ]=tmp;
     76                         g[fr][x][y][typ]=g[now][x-i][y+i][typ];
     77                     }
     78                     else if(tmp==f[fr][x][y][typ]){
     79                         g[fr][x][y][typ]=min(g[fr][x][y][typ],g[now][x-i][y+i][typ]);
     80                     }
     81                 }
     82             }
     83             if((x+i<=n)&&(y>=i+1)){
     84                 if((!vis[now][x+i][y-i][typ])||(f[now][x+i][y-i][typ]!=0x3f3f3f3f)){
     85                     int tmp=dfs(x+i,y-i,typ,now)+1;
     86                     if(tmp<f[fr][x][y][typ]){
     87                         f[fr][x][y][typ]=tmp;
     88                         g[fr][x][y][typ]=g[now][x+i][y-i][typ];
     89                     }
     90                     else if(tmp==f[fr][x][y][typ]){
     91                         g[fr][x][y][typ]=min(g[fr][x][y][typ],g[now][x+i][y-i][typ]);
     92                     }
     93                 }
     94             }
     95         }
     96     }
     97     if(typ==2){
     98         for(int i=0;i<8;i++){
     99             int xx=x+hrx[i];
    100             int yy=y+hry[i];
    101             if(xx>n||yy>n||xx<1||yy<1)continue;
    102             int tmp=0x3f3f3f3f;
    103             if((!vis[now][xx][yy][typ])||(f[now][xx][yy][typ]!=0x3f3f3f3f)){
    104                 tmp=dfs(xx,yy,typ,now)+1;
    105             }
    106             if(tmp<f[fr][x][y][typ]){
    107                 f[fr][x][y][typ]=tmp;
    108                 g[fr][x][y][typ]=g[now][xx][yy][typ];
    109             }
    110             else if(tmp==f[fr][x][y][typ]){
    111                 g[fr][x][y][typ]=min(g[fr][x][y][typ],g[now][xx][yy][typ]);
    112             }
    113         }
    114     }
    115     int tmp1=0x3f3f3f3f;
    116     int tmp2=0x3f3f3f3f;
    117     if((!vis[fr][x][y][(typ+1)%3])||(f[fr][x][y][(typ+1)%3]!=0x3f3f3f3f)){
    118         tmp1=dfs(x,y,(typ+1)%3,fr)+1;
    119     }
    120     if((!vis[fr][x][y][(typ+2)%3])||(f[fr][x][y][(typ+2)%3]!=0x3f3f3f3f)){
    121         tmp2=dfs(x,y,(typ+2)%3,fr)+1;
    122     }
    123     if(tmp1<f[fr][x][y][typ]){
    124         f[fr][x][y][typ]=tmp1;
    125         g[fr][x][y][typ]=g[fr][x][y][(typ+1)%3]+1;
    126     }
    127     if(tmp1==f[fr][x][y][typ]){
    128         g[fr][x][y][typ]=min(g[fr][x][y][(typ+1)%3]+1,g[fr][x][y][typ]);
    129     }
    130     if(tmp2<f[fr][x][y][typ]){
    131         f[fr][x][y][typ]=tmp2;
    132         g[fr][x][y][typ]=g[fr][x][y][(typ+2)%3]+1;
    133     }
    134     if(tmp2==f[fr][x][y][typ]){
    135         g[fr][x][y][typ]=min(g[fr][x][y][(typ+2)%3]+1,g[fr][x][y][typ]);
    136     }
    137     vis[fr][x][y][typ]=false;
    138     return f[fr][x][y][typ];
    139 }
    140 int main(){
    141     scanf("%d",&n);
    142     memset(f,0x3f,sizeof(f));
    143     memset(g,0x3f,sizeof(g));    
    144     for(int i=1;i<=n;i++){
    145         for(int j=1;j<=n;j++){
    146             scanf("%d",&mp[i][j]);
    147             if(mp[i][j]==1){
    148                 x=i;y=j;
    149             }
    150         }
    151     }
    152     int tmp1=dfs(x,y,0,0);
    153     int tmp4=g[0][x][y][0];
    154     int tmp2=dfs(x,y,1,0);
    155     int tmp5=g[0][x][y][1];
    156     int tmp3=dfs(x,y,2,0);
    157     int tmp6=g[0][x][y][2];
    158     if(tmp1<tmp2){
    159         ans1=tmp1;
    160         ans2=tmp4;
    161     }
    162     else if(tmp1==tmp2){
    163         ans1=tmp1;
    164         ans2=min(tmp4,tmp5);
    165     }else{
    166         ans1=tmp2;
    167         ans2=tmp5;
    168     }
    169     if(ans1>tmp3){
    170         ans1=tmp3;
    171         ans2=tmp6;
    172     }
    173     else if(ans1==tmp3){
    174         ans2=min(ans2,tmp6);
    175     }
    176     printf("%d %d
    ",ans1,ans2);
    177     return 0;
    178 }
  • 相关阅读:
    How To Scan QRCode For UWP (4)
    How To Crop Bitmap For UWP
    How To Scan QRCode For UWP (3)
    How To Scan QRCode For UWP (2)
    How To Scan QRCode For UWP (1)
    How to change windows applicatioin's position via Win32 API
    8 Ways to Become a Better Coder
    How to resize or create a thumbnail image from file stream on UWP
    C# winform压缩文件夹带进度条
    MS ACCESS MID函数
  • 原文地址:https://www.cnblogs.com/lnxcj/p/9897936.html
Copyright © 2011-2022 走看看