HDU 3368 Reversi (简单dfs)

来源:互联网 发布:js创建一个数组 编辑:程序博客网 时间:2024/06/05 14:24

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

思路:一开始想从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 0
原创粉丝点击