HDU 3368 Reversi (暴力,DFS)

题意:给定一个8*8的棋盘,然后要懂黑白棋,现在是黑棋走了,问你放一个黑子,最多能翻白子多少个。

析:我是这么想的,反正才是8*8的棋盘,那么就暴吧,反正不会超时,把每一个格能暴力的都暴力,无非是上,下,左,右,左上,左下,右上,右下,

然后都试试就好了。不过懂点黑白棋的还是好做一点。

代码如下:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <set>
#include <vector>
#include <algorithm>

using namespace std;
const int maxn = 10 + 5;
const int dr[] = {0, 0, 1, -1, 1, -1, -1, 1};
const int dc[] = {1, -1, 1, -1, -1, 1, 0, 0};
char s[maxn][maxn];
int vis[maxn][maxn];

int dfs(int x, int y, int r, int c){
    int xx = r + x;
    int yy = c + y;
    if(xx < 0 || yy < 0 || xx > 7 || yy > 7)  return 0;
    vis[x][y] = 1;
    if(s[xx][yy] == 'D' || s[xx][yy] == '*') return 0;
    if(s[xx][yy] == 'L' && !vis[xx][yy])  return 1 + dfs(xx, yy, r, c);
    else if(s[xx][yy] == 'L' && vis[xx][yy])  return dfs(xx, yy, r, c);
    return 0;
}

int main(){
    int n;
    cin >> n;

    for(int kase = 1; kase <= n; ++kase){
        memset(vis, 0, sizeof(vis));
//        int ans = 0;
        for(int i = 0; i < 8; ++i)
            scanf("%s", s[i]);
        int ans = 0;

        for(int i = 0; i < 8; ++i){
            for(int j = 0; j < 8; ++j){
                int cnt = 0;
                memset(vis, 0, sizeof(vis));
                if(s[i][j] == '*'){

                    for(int k = 0; k < 8; ++k){
                        int xx = i, yy = j;
                        bool ok = false;
                        while(xx >= 0 && xx < 8 && yy >= 0 && yy < 8){
                            if(s[xx][yy] == 'D'){ ok = true;  break; }
                            xx += dr[k];
                            yy += dc[k];
                            if(s[xx][yy] == '*') break;
                        }
                        if(ok){  cnt += dfs(i, j, dr[k], dc[k]);   }

                    }
                }
                ans = max(ans, cnt);
            }
        }


        printf("Case %d: %d
", kase, ans);
    }
    return 0;
}
#include <iostream>
#include <cstdio>
#include <cstring>


using namespace std;
const int maxn = 200 + 5;
const int dr[] = {0, 0, 1, -1, 1, -1, -1, 1};
const int dc[] = {1, -1, 1, -1, -1, 1, 0, 0};
char s[10][10];

int dfs(int i, int j, int r, int c){
    if(i < 0 || j < 0 || i > 7 || j > 8)  return -1000;
    else if(s[i][j] == '*')   return -1000;
    else if(s[i][j] == 'D')   return 0;
    else  return 1 + dfs(i+r, c+j, r, c);
}

int main(){
    int T;
    cin >> T;
    for(int kase = 1; kase <= T; ++kase){
        for(int i = 0; i < 8; ++i)
            scanf("%s", s[i]);
        int ans = 0;
        for(int i = 0; i < 8; ++i)
            for(int j = 0; j < 8; ++j)
                if(s[i][j] == '*'){
                    int cnt = 0;
                    for(int k = 0; k < 8; ++k)
                        cnt += max(0, dfs(i+dr[k], j+dc[k], dr[k], dc[k]));
                    ans = max(ans, cnt);
                }

        printf("Case %d: %d
", kase, ans);

    }
    return 0;
}
原文地址:https://www.cnblogs.com/dwtfukgv/p/5709380.html