{"id":1380,"date":"2013-03-13T19:46:17","date_gmt":"2013-03-13T11:46:17","guid":{"rendered":"https:\/\/blog.rexdf.org\/?p=1380"},"modified":"2013-03-17T13:06:23","modified_gmt":"2013-03-17T05:06:23","slug":"codeforces-round-168-div-2-convex-shape","status":"publish","type":"post","link":"https:\/\/blog.rexdf.org\/ja\/2013\/03\/codeforces-round-168-div-2-convex-shape\/","title":{"rendered":"Codeforces Round #168 (Div. 2) Convex Shape"},"content":{"rendered":"<div>\n<div style=\"text-align: center;\">\n<pre class=\"brush: cpp; gutter: true\">\r\n#include &lt;iostream&gt;\r\n#include &lt;cstdio&gt;\r\n#include &lt;cstdlib&gt;\r\n#include &lt;cstring&gt;\r\n#include &lt;queue&gt;\r\n\r\nusing namespace std;\r\n\r\nchar map[55][55];\r\nint n,m;\r\nint vis2[55][55];\r\nint minvis[55][55];\r\nconst int dx[]={1,0,-1,0};\r\nconst int dy[]={0,1,0,-1};\r\n\r\nstruct node\r\n{\r\n\tint x,y;\r\n\tint dir;\r\n\tint t;\r\n};\r\n\r\nqueue&lt;node&gt; q;\r\n\r\nint global_check_once;\r\n\r\nint bfs(int x,int y)\r\n{\r\n\tnode tmp,top;\r\n\tint tx,ty;\r\n\ttmp.t=0;tmp.x=x;tmp.y=y;\r\n\ttmp.dir=-1;\r\n\twhile(!q.empty())q.pop();\r\n\tmemset(vis2,0,sizeof(vis2));\r\n\tmemset(minvis,0,sizeof(minvis));\r\n\tq.push(tmp);\r\n\tvis2[x][y]=1;\r\n\tminvis[x][y]=0;\r\n\t\/\/cout&lt;&lt;&quot;begin to search &quot;&lt;&lt;x&lt;&lt;&quot; &quot;&lt;&lt;y&lt;&lt;endl;\r\n\twhile(!q.empty())\r\n\t{\r\n\t\ttop=q.front();\r\n\t\tq.pop();\r\n\t\t\/\/cout&lt;&lt;&quot;pop &quot;&lt;&lt;top.x&lt;&lt;&quot; &quot;&lt;&lt;top.y&lt;&lt;&quot; &quot;&lt;&lt;top.dir&lt;&lt;&quot; &quot;&lt;&lt;top.t&lt;&lt;endl;\r\n\t\tfor(int i=0;i&lt;4;i++)\r\n\t\t{\r\n\t\t\ttx=top.x+dx[i];\r\n\t\t\tty=top.y+dy[i];\r\n\t\t\t\r\n\t\t\tif(tx&gt;=0 &amp;&amp; tx&lt;n &amp;&amp; ty&gt;=0 &amp;&amp; ty&lt;m &amp;&amp;\r\n\t\t\t   map[tx][ty]==&#039;B&#039;)\/\/ &amp;&amp; vis[tx][ty][tmp.t][i]==0\r\n\t\t\t{\r\n\t\t\t\ttmp.t=top.t;\r\n\t\t\t\tif(i!=top.dir)tmp.t++;\r\n\t\t\t   \ttmp.x=tx;tmp.y=ty;\r\n\t\t\t\ttmp.dir=i;\r\n\t\t\t\tif(vis2[tx][ty])\r\n\t\t\t\t{\r\n\t\t\t\t\tif(minvis[tx][ty]&lt;tmp.t)\r\n\t\t\t\t\t{\r\n\t\t\t\t\t\t\/\/cout&lt;&lt;&quot;ignore &quot;&lt;&lt;tx&lt;&lt;&quot; &quot;&lt;&lt;ty&lt;&lt;endl;\r\n\t\t\t\t\t\tcontinue;\r\n\t\t\t\t\t}\r\n\t\t\t\t}\r\n\t\t\t\tq.push(tmp);\r\n\t\t\t\t\/\/cout&lt;&lt;x&lt;&lt;&quot;,&quot;&lt;&lt;y&lt;&lt;&quot;push &quot;&lt;&lt;tx&lt;&lt;&quot; &quot;&lt;&lt;ty&lt;&lt;&quot; &quot;&lt;&lt;i&lt;&lt;&quot; &quot;&lt;&lt;tmp.t&lt;&lt;endl;\r\n\t\t\t\t\/\/vis[tx][ty][i]=1;\r\n\t\t\t\tvis2[tx][ty]=1;\r\n\t\t\t\tminvis[tx][ty]=tmp.t;\r\n\t\t\t}\r\n\t\t}\r\n\t}\r\n\tfor(int i=0;i&lt;n;i++)\r\n\t  for(int j=0;j&lt;m;j++)\r\n\t    if(map[i][j]==&#039;B&#039; &amp;&amp; minvis[i][j]&gt;2)\r\n\t\t{\r\n\t\t\t\/\/cout&lt;&lt;&quot;find &gt;2 &quot;&lt;&lt;&quot;(&quot;&lt;&lt;x&lt;&lt;&quot;,&quot;&lt;&lt;y&lt;&lt;&quot;) &quot;&lt;&lt;i&lt;&lt;&quot; &quot;&lt;&lt;j&lt;&lt;endl;\r\n\t\t\treturn 0;\r\n\t\t}\r\n\tif(0==global_check_once)\r\n\t{\r\n\t\tfor(int i=0;i&lt;n;i++)\r\n\t\t  for(int j=0;j&lt;m;j++)\r\n\t\t    if(map[i][j]==&#039;B&#039; &amp;&amp; vis2[i][j]==0)\r\n\t\t\t{\r\n\t\t\t   \/\/cout&lt;&lt;&quot;find seperated &quot;&lt;&lt;endl;\r\n\t\t\t   return 0;\r\n\t\t\t}\r\n\t\tglobal_check_once=1;\r\n\t}\r\n\treturn 1;\r\n}\r\n\r\nint solve()\r\n{\r\n\tglobal_check_once=0;\r\n\tfor(int i=0;i&lt;n;i++)\r\n\t  for(int j=0;j&lt;m;j++)\r\n\t    if(map[i][j]==&#039;B&#039;)\r\n\t    {\r\n\t    \tif(0==bfs(i,j))return 0;\r\n\t    }\r\n\treturn 1;\r\n}\r\n\r\nint main()\r\n{\r\n\t\/\/freopen(&quot;b_in.txt&quot;,&quot;r&quot;,stdin);\r\n\t\/\/freopen(&quot;b_out.txt&quot;,&quot;w&quot;,stdout);\r\n\twhile(cin&gt;&gt;n&gt;&gt;m)\r\n\t{\r\n\t\tfor(int i=0;i&lt;n;i++)\r\n\t\t  cin&gt;&gt;map[i];\r\n\t\tif(solve())cout&lt;&lt;&quot;YES&quot;&lt;&lt;endl;\r\n\t\telse cout&lt;&lt;&quot;NO&quot;&lt;&lt;endl;\r\n\t}\r\n\t\/\/system(&quot;pause&quot;);\r\n\treturn 0;\r\n}\r\n\r\n<\/pre>\n<p>B. Convex Shape<\/p><\/div>\n<div style=\"text-align: center;\">\n<div>time limit per test<\/div>\n<p>2 seconds<\/p><\/div>\n<div style=\"text-align: center;\">\n<div>memory limit per test<\/div>\n<p>256 megabytes<\/p><\/div>\n<div style=\"text-align: center;\">\n<div>input<\/div>\n<p>standard input<\/p><\/div>\n<div style=\"text-align: center;\">\n<div>output<\/div>\n<p>standard output<\/p><\/div>\n<\/div>\n<div>\n<p>Consider an\u00a0<i>n<\/i>\u2009\u00d7\u2009<i>m<\/i>\u00a0grid. 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\u00a0convex\u00a0if one can walk from\u00a0any\u00a0black cell to\u00a0any another\u00a0black 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.<\/p>\n<p>&nbsp;<\/p>\n<p><center><img decoding=\"async\" class=\"ls_lazyimg\" alt=\"\" src=\"https:\/\/static.rexdf.org\/plugins\/images-lazyload-and-slideshow\/blank_250x250.gif\" file=\"http:\/\/codeforces.ru\/renderer\/811f6686ee905d7d1f95e96356ded29f8f77d16f.png\" \/><noscript><img decoding=\"async\" alt=\"\" src=\"http:\/\/codeforces.ru\/renderer\/811f6686ee905d7d1f95e96356ded29f8f77d16f.png\" \/><\/noscript><\/center><\/p>\n<p>&nbsp;<\/p>\n<p>You&#8217;re given a painted grid in the input. Tell Lenny if the grid is convex or not.<\/p>\n<\/div>\n<div>\n<div>Input<\/div>\n<p>The first line of the input contains two integers\u00a0<i>n<\/i>\u00a0and\u00a0<i>m<\/i>\u00a0(1\u2009\u2264\u2009<i>n<\/i>,\u2009<i>m<\/i>\u2009\u2264\u200950)\u00a0\u2014 the size of the grid. Each of the next\u00a0<i>n<\/i>\u00a0lines contains\u00a0<i>m<\/i>characters &#8220;B&#8221; or &#8220;W&#8221;. Character &#8220;B&#8221; denotes a black cell of the grid and &#8220;W&#8221; denotes a white cell of the grid.<\/p>\n<p>It&#8217;s guaranteed that the grid has at least one black cell.<\/p>\n<\/div>\n<div>\n<div>Output<\/div>\n<p>On the only line of the output print &#8220;YES&#8221; if the grid is convex, otherwise print &#8220;NO&#8221;. Do not print quotes.<\/p>\n<\/div>\n<div>\n<div>Sample test(s)<\/div>\n<div>\n<div>\n<div>input<\/div>\n<pre>3 4 WWBW BWWW WWWB<\/pre>\n<\/div>\n<div>\n<div>output<\/div>\n<pre>NO<\/pre>\n<\/div>\n<div>\n<div>input<\/div>\n<pre>3 1 B B W<\/pre>\n<\/div>\n<div>\n<div>output<\/div>\n<pre>YES<\/pre>\n<\/div>\n<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>#include &lt;iostream&gt; #include &lt;c &hellip; <a href=\"https:\/\/blog.rexdf.org\/ja\/2013\/03\/codeforces-round-168-div-2-convex-shape\/\">\u7d9a\u304d\u3092\u8aad\u3080 <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[3],"tags":[125],"class_list":["post-1380","post","type-post","status-publish","format-standard","hentry","category-algorithm","tag-codeforces"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/blog.rexdf.org\/ja\/wp-json\/wp\/v2\/posts\/1380","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.rexdf.org\/ja\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.rexdf.org\/ja\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.rexdf.org\/ja\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.rexdf.org\/ja\/wp-json\/wp\/v2\/comments?post=1380"}],"version-history":[{"count":0,"href":"https:\/\/blog.rexdf.org\/ja\/wp-json\/wp\/v2\/posts\/1380\/revisions"}],"wp:attachment":[{"href":"https:\/\/blog.rexdf.org\/ja\/wp-json\/wp\/v2\/media?parent=1380"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.rexdf.org\/ja\/wp-json\/wp\/v2\/categories?post=1380"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.rexdf.org\/ja\/wp-json\/wp\/v2\/tags?post=1380"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}