zoukankan      html  css  js  c++  java
  • 玲珑OJ 1082:XJT Loves Boggle(爆搜)

    http://www.ifrog.cc/acm/problem/1082

    题意:给出的单词要在3*3矩阵里面相邻连续(相邻包括对角),如果不行就输出0,如果可行就输出对应长度的分数。

    思路:爆搜,但是写砸了。处理一次状态的时候把vis[i][j]设成1,然后DFS。DFS完之后还要把vis[i][j]改成0,否则就会只搜一条路径了。QAQ。正解貌似是枚举所有的可行字符串丢到set里面。

     1 #include <cstdio>
     2 #include <cstring>
     3 #include <cmath>
     4 #include <cstdlib>
     5 #include <algorithm>
     6 #include <string>
     7 #include <iostream>
     8 #include <stack>
     9 #include <map>
    10 #include <queue>
    11 #include <set>
    12 using namespace std;
    13 typedef long long LL;
    14 #define N 100010
    15 #define INF 0x3f3f3f3f
    16 
    17 char mp[100][100], s[100];
    18 int vis[100][100], dx[] = {-1, -1, -1, 0, 0, 1, 1, 1}, dy[] = {1, 0, -1, 1, -1, 1, 0, -1}, flag;
    19 int mm[] = {0, 0, 0, 1, 1, 2, 3, 5, 11, 11};
    20 
    21 bool check(int x, int y, char c) {
    22     if(0 <= x && x < 3 && 0 <= y && y < 3 && mp[x][y] == c && !vis[x][y]) return true;
    23     return false;
    24 }
    25 
    26 void DFS(int x, int y, int now, int len) {
    27     if(now >= len - 1) { flag = 1; return ; }
    28     for(int i = 0; i < 8; i++) {
    29         int nx = dx[i] + x, ny = dy[i] + y;
    30         if(check(nx, ny, s[now+1])) {
    31             vis[nx][ny] = 1;
    32             DFS(nx, ny, now + 1, len);
    33             vis[nx][ny] = 0;
    34         }
    35     }
    36 }
    37 
    38 int main()
    39 {
    40     int t;
    41     scanf("%d", &t);
    42     for(int cas = 1; cas <= t; cas++) {
    43         for(int i = 0; i < 3; i++) scanf("%s", mp[i]);
    44         int q; scanf("%d", &q);
    45         printf("Case #%d:
    ", cas);
    46         while(q--) {
    47             scanf("%s", s);
    48             int len = strlen(s); flag = 0;
    49             if(len >= 3)
    50                 for(int i = 0; i < 3 && !flag; i++) {
    51                     for(int j = 0; j < 3 && !flag; j++) {
    52                         if(mp[i][j] == s[0]) {
    53                             memset(vis, 0, sizeof(vis));
    54                             vis[i][j] = 1;
    55                             DFS(i, j, 0, len);
    56                         }
    57                     }
    58                 }
    59             printf("%d
    ", flag ? mm[len] : 0);
    60         }
    61     }
    62     return 0;
    63 }
  • 相关阅读:
    抓取网页数据C#文件
    ListView嵌套GridView使用详解及注意事项
    listView里面添加gridview
    动态加载图片的Adapter
    如何使用Photoshop(PS)将图片的底色变为透明
    无需序列号安装Photoshop CS6
    Objective-C中.h文件、.m文件中@interface、@synthesize及其它
    Android studio sha1
    Tool bar
    onActivityResult 通过case对不同情况进行处理
  • 原文地址:https://www.cnblogs.com/fightfordream/p/6285489.html
Copyright © 2011-2022 走看看