• Codeforces Round #168 (Div. 2) B. Convex Shape(暴力)


    B. Convex Shape
    time limit per test
    2 seconds
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    Consider an n × m grid. Initially all the cells of the grid are colored white. Lenny has painted some of the cells (at least one) black. We call a painted grid convex if one can walk from any black cell to any another black cell using a path of side-adjacent black cells changing his direction at most once during the path. In the figure below, the left grid is convex while the right one is not convex, because there exist two cells which need more than one time to change direction in their path.

    You're given a painted grid in the input. Tell Lenny if the grid is convex or not.

    Input

    The first line of the input contains two integers n and m (1 ≤ n, m ≤ 50) — the size of the grid. Each of the next n lines contains m characters "B" or "W". Character "B" denotes a black cell of the grid and "W" denotes a white cell of the grid.

    It's guaranteed that the grid has at least one black cell.

    Output

    On the only line of the output print "YES" if the grid is convex, otherwise print "NO". Do not print quotes.

    Sample test(s)
    Input
    3 4
    WWBW
    BWWW
    WWWB
    Output
    NO
    Input
    3 1
    B
    B
    W
    Output
    YES

    看完题的第一想法是:暴力解决,因为数据量实在小。
    对于两个黑块(i0, j0)和(i1, j1)
    如果 i0 == i1,不需转弯,检测从j0到j1是否存在w即可,当j0 == j1时类似。
    如果 i0 != i1 && j0 != j1,则由(i0, j0)经(i1, j0)到(i1,j1)或由(i0, j0)经(i0, j1)到(i1,j1)。
    思路很简单,代码丑了点。。。
    AC Code:
      1 #include <iostream>
      2 #include <cstdio>
      3 #include <cstring>
      4 
      5 using namespace std;
      6 
      7 int m, n;
      8 char map[51][51];
      9 
     10 bool Judge(int i0, int j0)
     11 {
     12     for(int i1 = 0; i1 < n; i1++)
     13         for(int j1 = 0; j1 < m; j1++)
     14         {
     15             if(map[i1][j1] == 'B' && (i1 != i0 || j1 != j0))
     16             {
     17                 //以下判断(i0, j0)能否至多经一次转弯到达(i1, j1)
     18                 if(i1 == i0)  //不需转弯
     19                 {
     20                     int d = j1 > j0 ? 1 : -1;
     21                     for(int k = j0 + d; ; k += d)
     22                     {
     23                         if(map[i0][k] == 'W') return false;
     24                         if(k == j1) break;
     25                     }
     26                 }
     27                 else if(j1 == j0)  //不需转弯
     28                 {
     29                     int d = i1 > i0 ? 1 : -1;
     30                     for(int k = i0 + d; ; k += d)
     31                     {
     32                         if(map[k][j0] == 'W') return false;
     33                         if(k == i1) break;
     34                     }
     35                 }
     36                 else  //两个可能的转弯点(i0, j1)和(i1, j0)
     37                 {
     38                     bool tag = true;
     39                     int k, d;
     40                     //由(i0, j0)经(i1, j0)到(i1,j1)
     41                     d = i1 > i0 ? 1 : -1;
     42                     for(k = i0 + d; ; k += d)
     43                     {
     44                         if(map[k][j0] == 'W')
     45                         {
     46                             tag = false;
     47                             break;
     48                         }
     49                         if(k == i1) break;
     50                     }
     51                     if(tag)
     52                     {
     53                         d = j1 > j0 ? 1 : -1;
     54                         for(k = j0 + d; ; k += d)
     55                         {
     56                             if(map[i1][k] == 'W')
     57                             {
     58                                 tag = false;
     59                                 break;
     60                             }
     61                             if(k == j1) break;
     62                         }
     63                     }
     64                     if(!tag)
     65                     {
     66                         tag = true;
     67                         //由(i0, j0)经(i0, j1)到(i1,j1)
     68                         d = j1 > j0 ? 1 : -1;
     69                         for(k = j0 + d; ; k += d)
     70                         {
     71                             if(map[i0][k] == 'W')
     72                             {
     73                                 tag = false;
     74                                 break;
     75                             }
     76                             if(k == j1) break;
     77                         }
     78                         if(tag)
     79                         {
     80                             d = i1 > i0 ? 1 : -1;
     81                             for(k = i0 + d; ; k += d)
     82                             {
     83                                 if(map[k][j1] == 'W')
     84                                 {
     85                                     tag = false;
     86                                     break;
     87                                 }
     88                                 if(k == i1) break;
     89                             }
     90                         }
     91                     }
     92                     if(!tag) return false;
     93                 }
     94             }
     95         }
     96     return true;
     97 }
     98 
     99 int main()
    100 {
    101     bool ok;
    102     while(scanf("%d %d", &n, &m) != EOF)
    103     {
    104         ok = true;
    105         for(int i = 0; i < n; i++)
    106             scanf("%s", map[i]);
    107         for(int i = 0; i < n && ok; i++)
    108         {
    109             for(int j = 0; j < m && ok; j++)
    110             {
    111                 if(map[i][j] == 'B')
    112                     ok = Judge(i, j);
    113             }
    114         }
    115         if(ok) puts("YES");
    116         else puts("NO");
    117     }
    118     return 0;
    119 }



  • 相关阅读:
    牛客 Wannafly 挑战赛26D 禁书目录 排列组合 概率期望
    UOJ#269. 【清华集训2016】如何优雅地求和
    斯特林数 学习笔记
    AtCoder Grand Contest 006 (AGC006) C
    Codeforces 1045D Interstellar battle 概率期望
    Codeforces 1045A Last chance 网络流,线段树,线段树优化建图
    Codeforces 1053C Putting Boxes Together 树状数组
    Codeforces 109D String Transformation 字符串 哈希 KMP
    AtCoder Grand Contest 1~10 做题小记
    AtCoder Grand Contest 002 (AGC002) F
  • 原文地址:https://www.cnblogs.com/cszlg/p/2922117.html
Copyright © 2020-2023  润新知