zoukankan      html  css  js  c++  java
  • 【HDOJ】1987 Decoding

    简单搜索。

     1 /* hdoj 1987 */
     2 #include <iostream>
     3 #include <cstdio>
     4 #include <cstring>
     5 #include <cstdlib>
     6 #include <algorithm>
     7 using namespace std;
     8 
     9 char s[505];
    10 char map[25][25];
    11 bool visit[25][25];
    12 int n, m, len;
    13 int dir[4][2] = {
    14     0,1, 1,0, 0,-1, -1,0
    15 }; // right down left up
    16 
    17 bool check(int x, int y) {
    18     return x<0 || x>=n || y<0 || y>=m;
    19 }
    20 
    21 void solve() {
    22     int i, j, k, tmp;
    23     int d = 0;
    24     int t = n*m/5;
    25     int x, y, xx, yy;
    26     
    27     len = 0;
    28     memset(visit, false, sizeof(visit));
    29     
    30     x = 0, y = -1;
    31     while (t--) {
    32         k = 5;
    33         tmp = 0;
    34         while (k) {
    35             xx = x + dir[d][0];
    36             yy = y + dir[d][1];
    37             if (check(xx, yy) || visit[xx][yy]) {
    38                 d = (d+1) & 3;
    39                 continue;
    40             }
    41             visit[xx][yy] = true;
    42             tmp = (tmp<<1) + map[xx][yy]-'0';
    43             --k;
    44             x = xx;
    45             y = yy;
    46         }
    47         if (tmp == 0)
    48             s[len++] = ' ';
    49         else
    50             s[len++] = tmp + 'A' - 1;
    51     }
    52 }
    53 
    54 int main() {
    55     int t, tt;
    56     int i, j, k;
    57     
    58     #ifndef ONLINE_JUDGE
    59         freopen("data.in", "r", stdin);
    60     #endif
    61     
    62     scanf("%d", &t);
    63     for (tt=1; tt<=t; ++tt) {
    64         scanf("%d %d %s", &n, &m, s);
    65         k = 0;
    66         for (i=0; i<n; ++i)
    67             for (j=0; j<m; ++j)
    68                 map[i][j] = s[k++];
    69         solve();
    70         i = len - 1;
    71         while (s[i] == ' ')
    72             --i;
    73         s[i+1] = '';
    74         i = 0;
    75         while (s[i] == ' ')
    76             ++i;
    77         printf("%d %s
    ", tt, s+i);
    78     }
    79     
    80     return 0;
    81 }
  • 相关阅读:
    iOS有用的三方库和高效工具记录
    正则表达式
    Exception Type & Exception Code
    信鸽推送(XGPush)
    在vue中使用animate.css
    vue 中父子组件传值:props和$emit
    预编译scss以及scss和less px 转rem
    ES6箭头函数及模版字符串
    移动端页面a input去除点击效果及pc端切换
    vue2搭建简易spa
  • 原文地址:https://www.cnblogs.com/bombe1013/p/4251308.html
Copyright © 2011-2022 走看看