第二题 POJ 2965

做了第一题,见上一个发布,发现是一样的方法,4x4都没变,就是多加了一个输出变换路径,我也没置零置一,直接写了个大模拟,翻转之后记录,用步数记录,妙,对递归理解又多了一点。
明天再手写一次,加油。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
char str[5][5];
int ansx[20],ansy[20];
int tx[20],ty[20];
int ans=50;
bool check(){
    for(int i=0;i<4;i++){
        for(int j=0;j<4;j++){
            if(str[i][j]!='-') return 0;
        }
    }
    return 1;
}
void change(int x){
    int xx=x/4;
    int yy=x%4;
    for(int i=0;i<4;i++){
        if(str[i][yy]=='-') str[i][yy]='+';
        else str[i][yy]='-';
        if(str[xx][i]=='-') str[xx][i]='+';
        else str[xx][i]='-';
    }
    if(str[xx][yy]=='-') str[xx][yy]='+';
    else str[xx][yy]='-';
}
void dfs(int x,int step){
    if(check()){
        if(step<ans){
            ans=step;
            for(int i=1;i<=ans;i++){
                ansx[i]=tx[i];
                ansy[i]=ty[i];
            }
        }
        return ;
    }
    if(x>=16) return ;
    dfs(x+1,step);
    change(x);
    tx[step+1]=x/4+1;
    ty[step+1]=x%4+1;
    dfs(x+1,step+1);
    change(x);
}
int main(){
    for(int i=0;i<4;i++){
        scanf("%s",str[i]);
    }
    dfs(0,0);
    printf("%d\n",ans);
    for(int i=1;i<=ans;i++){
        printf("%d% d\n",ansx[i],ansy[i]);
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值