DFS csu1719 Boggle

传送门:点击打开链接

题意:真正的题意是,告诉你一些字符串。然后告诉你非常多个字符格子,问这些字符串是否能在字符格子中连起来,在格子中对角线也觉得是连在一起的。假设格子中的字符是q,事实上是代表着qu

思路:这题迷之英语。各种猜题意啊,,只是运气好比較早就猜中了。嘿嘿嘿

懂题意了后就非常easy了,DFS各种搜即可了,由于数据范围比較小

#include <map>
#include <set>
#include <cmath>
#include <ctime>
#include <stack>
#include <queue>
#include <cstdio>
#include <cctype>
#include <bitset>
#include <string>
#include <vector>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <functional>
#define fuck(x) cout << "[" << x << "]"
#define FIN freopen("input.txt", "r", stdin)
#define FOUT freopen("output.txt", "w+", stdout)
using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
 
const int MX = 200 + 5;
string A[MX];
 
int n, m;
char stemp[MX];
char S[10][10], vis[10][10];
 
bool DFS(int id, int i, int x, int y, char w) {
    int len = A[id].length();
    if(A[id][i] != w) return false;
    if(w == 'q') {
        if(!(i + 1 < len && A[id][i+1] == 'u')) return false;
        else i++;
    }
    if(i == len - 1) return true;
    vis[x][y] = 1;
 
    for(int dx = -1; dx <= 1; dx++) {
        for(int dy = -1; dy <= 1; dy++) {
            if(dx == 0 && dy == 0) continue;
            int nx = dx + x, ny = dy + y;
            if(nx < 0 || nx > m || ny < 0 || ny > m || vis[nx][ny]) continue;
            if(DFS(id, i + 1, nx, ny, S[nx][ny])) {
                vis[x][y] = 0;
                return true;
            }
        }
    }
    vis[x][y] = 0;
    return false;
}
 
int main() {
    //FIN;
    while(~scanf("%d", &n)) {
        for(int i = 1; i <= n; i++) {
            scanf("%s", stemp);
            A[i] = string(stemp);
        }
        sort(A + 1, A + 1 + n);
 
        while(scanf("%d", &m), m) {
            for(int i = 1; i <= m; i++) {
                scanf("%s", S[i] + 1);
            }
 
            for(int id = 1; id <= n; id++) {
                bool sign = false;
                for(int i = 1; i <= m; i++) {
                    for(int j = 1; j <= m; j++) {
                        if(DFS(id, 0, i, j, S[i][j])) {
                            sign = true; break;
                        }
                    }
                    if(sign) break;
                }
                if(sign) printf("%s
", A[id].c_str());
            }
            printf("-
");
        }
    }
    return 0;
}


原文地址:https://www.cnblogs.com/clnchanpin/p/7365952.html