Codeforces Round #168 (Div. 2) B. Convex Shape
来源:互联网 发布:淘宝万事屋手办 编辑:程序博客网 时间:2024/05/29 14:47
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 gridconvex 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.
The first line of the input contains two integers n andm (1 ≤ n, m ≤ 50) — the size of the grid. Each of the nextn 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.
On the only line of the output print "YES" if the grid is convex, otherwise print "NO". Do not print quotes.
3 4WWBWBWWWWWWB
NO
3 1BBW
YES
题意: 从地图上任意一个黑格 到达另外的任意一个黑格,其中必须经过黑格且只能拐弯两次..
思路: 一开始想复杂了, 用了DFS+BFS, 会有各种意外情况发生, 其实数据那么小 完全可以使用暴力.
CODE:
#include<stdio.h>#include<iostream>using namespace std;char map[55][55];int n,m,x[3000],y[3000];int check(int xx1,int yy1,int xx2,int yy2){ for(int i=min(yy1,yy2);i<=max(yy1,yy2);i++) if(map[xx1][i]!='B') return 0; for(int i=min(xx1,xx2);i<=max(xx1,xx2);i++) if(map[i][yy2]!='B') return 0; return 1;}int main(){ cin>>n>>m; getchar(); int k=-1; for(int i=0;i<n;i++) { cin>>map[i]; for(int j=0;j<m;j++) { if(map[i][j]=='B') x[++k]=i,y[k]=j; } //getchar(); } for(int i=0;i<k;i++) for(int j=i+1;j<=k;j++) { if(check(x[i],y[i],x[j],y[j])) continue; if(check(x[j],y[j],x[i],y[i])) continue; printf("NO\n"); return 0; } printf("YES\n"); return 0;}
- Codeforces Round#168(Div 2)B Convex Shape
- Codeforces Round #168 (Div. 2) B Convex Shape
- Codeforces Round #168 (Div. 2) B. Convex Shape
- Codeforces Round #131 (Div. 2) A B
- Codeforces Round #134 (Div. 2)B. Airport
- Codeforces Round #170 (Div. 2) problem B
- Codeforces Round #173 (Div. 2) Problem B
- Codeforces Round #181 (Div. 2) B. Coach
- Codeforces Round #185 (Div. 2)--A,B
- Codeforces Round #171 (Div. 2) B
- Codeforces Round #169 (Div. 2) B题
- Codeforces Round #188 (Div. 2) B题
- Codeforces Round #192 (Div. 2) B
- Codeforces Round #203 (Div. 2) B. Resort
- Codeforces Round #206 (Div. 2) - b
- Codeforces Round #202 (Div. 2)B-贪心
- Codeforces Round #203 (Div. 2)B
- Codeforces Round #203(Div. 2)B. Resort
- 第7周作业1——背包问题
- 开源中国iOS客户端学习——(十三)使用UIWebView控件布局视图
- Cocos2dx: 弹出对话框的设计与实现
- linux文件操作
- ubuntu 终端中文显示乱码问题!
- Codeforces Round #168 (Div. 2) B. Convex Shape
- 智能指针
- 面试中会出现的问题
- struts2+jQuery+ajax调用演示
- 主主主主主席树Orz
- linux程序后台调度代码写法(没有终端)
- 複習最短路 hdu 2544
- C# 序列化
- 14 Coding Challenges to Help You Train Your Brain