poj2965 DFS暴力枚举

和poj1753很像  只是翻转的方式改变了~还是用DFS暴力枚举。值得注意的就是记录路径了。‘可以用rr和cc数组来代表路径的横纵坐标。

rr[deep] cc[deep]表示的是第deep步的坐标。这样记录省空间并且容易。一开始用双向队列记录的时候会把错误的步数也记录上去。用数组就好很多

#include<stdio.h>
#include<string.h>
#include <iostream>
#include<queue>
using namespace std;
int rr[16];
int cc[16];
bool chess[6][6]={false};
int step;
bool flag;
bool judge_all(void)
{
    for(int i=1; i<5; i++)
    {
        for(int j=1; j<5; j++)
        {
            if(chess[i][j]!=1)
            {
                return false;
            }
        }
    }
    return true;
}
void filp(int row,int col)
{
    for(int i=1; i<5; i++)
    {
        if(i!=col)
        chess[row][i]=!chess[row][i];
        if(i!=row)
        chess[i][col]=!chess[i][col];
    }
    chess[row][col]=!chess[row][col];
    return;
}
void dfs(int row,int col,int deep)
{
    if(deep==step)
    {
        flag=judge_all();
        return;
    }
    if(flag||row==5)
       {
            return;
       }
    filp(row,col);
    rr[deep]=row;
    cc[deep]=col;
    if(col<4)
        dfs(row,col+1,deep+1);
    else
        dfs(row+1,1,deep+1);
    filp(row,col);
    if(col<4)
        dfs(row,col+1,deep);
    else
        dfs(row+1,1,deep);
    return;
}
int main()
{
    char temp;
    for(int i=1; i<5; i++)
    {
        for(int j=1; j<5; j++)
        {
            cin>>temp;
            if(temp=='-')
                chess[i][j]=true;
        }
    }
    for(step=0; step<=16; step++)
    {
        dfs(1,1,0);
        if(flag)
        {
            break;
        }
    }
    printf("%d\n",step);
    for(int i=0;i<step;i++)
        printf("%d %d\n",rr[i],cc[i]);
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值