HDU 3368 Reversi (简单dfs)

题意:跟小时候在电视机上玩过的黑白棋一模一样,于是题也没读便开始做。给你一副棋盘,让你走一步黑棋,尽可能多的吃掉白棋,求吃掉的个数。

思路:一开始想从D开始递归,后来一想不对啊,比如,走的那一步黑棋,可以从多个方向上吃掉白棋,那么从D开始递归会麻烦太多。所以:

从*开始dfs,直到遇上D或者*便停止退出递归,把握好递归方向即可。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
using namespace std;
typedef long long LL;
char Map[10][10];
int kase, T = 0, ans, Max , res;
const int dir[9][2] = {
    {-1,1},{0,1},{1,1},
    {-1,0},{1,0},
    {-1,-1},{0,-1},{1,-1},{0,0} //前8个为方向,最后(0,0)纯属充数
};

struct Node{
    int x, y, dirr;
};

void dfs(int x, int y, int dirr, int Max) {
    if(dirr == 8) { //初始递归值,八个方向
        for(int i = 0; i < 8; ++i) {
            int xx = x+dir[i][0];
            int yy = y+dir[i][1];
            if(xx >= 0 && xx < 8 && yy >= 0 && yy < 8) dfs(xx, yy, i, Max);
        }
    }
    if(Map[x][y] == 'D') {
        res += Max; return ;
    }
    if(Map[x][y] == '*') {
        Max = 0; return ;
    }
    if(Map[x][y] == 'L') {
        int xx = x+dir[dirr][0];
        int yy = y+dir[dirr][1];
        if(xx >= 0 && xx < 8 && yy >= 0 && yy < 8) dfs(xx, yy, dirr, Max+1);
    }
}

int main() {
    scanf("%d", &kase);
    while(kase--) {
        ans = Max = res = 0;
        for(int i = 0; i < 8; ++i) scanf("%s", Map[i]);
        for(int i = 0; i < 8; ++i) {
            for(int j = 0; j < 8; ++j) {
                if(Map[i][j] == '*') {
                    res = 0;
                    dfs(i, j, 8, 0);
                    ans = max(ans, res);
                }
            }
        }
        printf("Case %d: %d\n", ++T, ans);
    }
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

JalexDooo

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值