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 }

 




转载于:https://www.cnblogs.com/cszlg/archive/2013/02/22/2922117.html

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值