DFS csu1719 Boggle

来源:互联网 发布:淘宝申诉不成功怎么办 编辑:程序博客网 时间:2024/05/29 18:34

传送门:点击打开链接

题意:真正的题意是,告诉你一些字符串,然后告诉你很多个字符格子,问这些字符串能否在字符格子中连起来,在格子中对角线也认为是连在一起的。如果格子中的字符是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
原创粉丝点击