DFS csu1719 Boggle

传送门:点击打开链接

题意:真正的题意是,告诉你一些字符串,然后告诉你很多个字符格子,问这些字符串能否在字符格子中连起来,在格子中对角线也认为是连在一起的。如果格子中的字符是q,其实是代表着qu

思路:这题迷之英语,各种猜题意啊,,不过运气好比较早就猜中了,嘿嘿嘿

懂题意了后就很简单了,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\n", A[id].c_str());
            }
            printf("-\n");
        }
    }
    return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值