{"id":143,"date":"2011-11-26T20:05:59","date_gmt":"2011-11-26T12:05:59","guid":{"rendered":"http:\/\/www.shuizilong.com\/house\/?p=143"},"modified":"2012-03-03T20:11:08","modified_gmt":"2012-03-03T12:11:08","slug":"hdu-4127-flood-it","status":"publish","type":"post","link":"https:\/\/www.shuizilong.com\/house\/archives\/hdu-4127-flood-it\/","title":{"rendered":"HDU 4127. Flood-it!"},"content":{"rendered":"<h3>Brief description: <\/h3>\n<p>\u7565\u3002\u3002<\/p>\n<p><!--more--><\/p>\n<h3>Analysis: <\/h3>\n<p>\u3002\u3002bfs() TLE \u4e86\u4e00\u6b21\u3002\u3002\u540e\u6765\u6362\u6210\u666e\u901a\u7684 dfs() \u8fc7\u6389\u4e86\u3002\u3002\u770b\u6765\u8fd8\u662f\u7ecf\u9a8c\u4e0d\u8db3\u3002\u3002<br \/>\n\u3002\u8fd9\u9898\u4e2d\u7684\u72b6\u6001\u5b58\u5728\u7740\u4e00\u5b9a\u7684\u9636\u6bb5\u6027\uff08\u5de6\u4e0a\u89d2\u533a\u57df\u4f1a\u9010\u6b65\u6269\u5927\u3002\u3002\uff09\u3002\u3002\u4f46\u662f\u641c\u7d22\u6811\u4e2d\u4ecd\u7136\u4f1a\u4ea7\u751f\u4e00\u4e9b\u91cd\u590d\uff08\u7531\u4e0d\u540c\u7684\u8def\u5f84\u5f97\u5230\uff09\u3002\u3002\u8fd9\u5c31\u51fa\u73b0\u4e86\u5224\u91cd\u3001\u6216\u8005\u4e0d\u5224\u91cd\u7684\u95ee\u9898\u3002\u3002<br \/>\n\uff08\u3002\u3002\u53ef\u5229\u7528\u7684\u526a\u679d\u4f3c\u4e4e\u5e76\u4e0d\u660e\u663e\u3002\u3002\uff1f\uff09\u3002\u3002\u3002<\/p>\n<table id=\"ItemTable\" cellspacing=\"0\" width=\"100%\">\n<tr style=\"BORDER-BOTTOM-COLOR: #f5f8fa; BORDER-TOP-COLOR: #f5f8fa; BORDER-RIGHT-COLOR: #f5f8fa; BORDER-LEFT-COLOR: #f5f8fa\" id=\"itemId_6\" bgcolor=\"#f5f8fa\">\n<td id=\"tn\">\n<div style=\"PADDING-LEFT: 10px; COLOR: #42b475\"><font style=\"FONT-FAMILY: '\u5fae\u8f6f\u96c5\u9ed1'; FONT-SIZE: 12px\"><\/p>\n<div style=\"FLOAT: left; MARGIN-RIGHT: 3px\">HIT]xiaodao(251815992)&nbsp;17:34:57<\/div>\n<p><\/font>&nbsp;<\/div>\n<div class=\"msgContentDiv\"><font style=\"FONT-FAMILY: '\u5fae\u8f6f\u96c5\u9ed1','MS Sans Serif',sans-serif; FONT-SIZE: 11pt\" color=\"#600000\">\u5b8c\u5168\u6ca1\u61c2\u3002\u3002<\/font><\/div>\n<\/td>\n<\/tr>\n<tr style=\"BORDER-BOTTOM-COLOR: #f5f8fa; BORDER-TOP-COLOR: #f5f8fa; BORDER-RIGHT-COLOR: #f5f8fa; BORDER-LEFT-COLOR: #f5f8fa\" id=\"itemId_7\" bgcolor=\"#f5f8fa\">\n<td id=\"tn\">\n<div style=\"PADDING-LEFT: 10px; COLOR: #42b475\"><font style=\"FONT-FAMILY: '\u5fae\u8f6f\u96c5\u9ed1'; FONT-SIZE: 12px\"><\/p>\n<div style=\"FLOAT: left; MARGIN-RIGHT: 3px\">[HIT]xiaodao(251815992)&nbsp;17:35:12<\/div>\n<p><\/font>&nbsp;<\/div>\n<div class=\"msgContentDiv\"><font style=\"FONT-FAMILY: '\u5fae\u8f6f\u96c5\u9ed1','MS Sans Serif',sans-serif; FONT-SIZE: 11pt\" color=\"#600000\">6.\u30026\u6b21\u65b9\u7a0b\u3002\u3002\u3002\uff1f\uff09<\/font><\/div>\n<\/td>\n<\/tr>\n<tr style=\"BORDER-BOTTOM-COLOR: #f5f8fa; BORDER-TOP-COLOR: #f5f8fa; BORDER-RIGHT-COLOR: #f5f8fa; BORDER-LEFT-COLOR: #f5f8fa\" id=\"itemId_8\" bgcolor=\"#f5f8fa\">\n<td id=\"tn\">\n<div style=\"PADDING-LEFT: 10px; COLOR: #006efe\"><font style=\"FONT-FAMILY: '\u5fae\u8f6f\u96c5\u9ed1'; FONT-SIZE: 12px\"><\/p>\n<div style=\"FLOAT: left; MARGIN-RIGHT: 3px\">[BUAA]Bella;(83620452)&nbsp;17:35:46<\/div>\n<p><\/font>&#8230; .&nbsp;<\/div>\n<div class=\"msgContentDiv\"><\/div>\n<\/td>\n<\/tr>\n<tr style=\"BORDER-BOTTOM-COLOR: #f5f8fa; BORDER-TOP-COLOR: #f5f8fa; BORDER-RIGHT-COLOR: #f5f8fa; BORDER-LEFT-COLOR: #f5f8fa\" id=\"itemId_9\" bgcolor=\"#f5f8fa\">\n<td id=\"tn\">\n<div style=\"PADDING-LEFT: 10px; COLOR: #006efe\"><font style=\"FONT-FAMILY: '\u5fae\u8f6f\u96c5\u9ed1'; FONT-SIZE: 12px\"><\/p>\n<div style=\"FLOAT: left; MARGIN-RIGHT: 3px\">\u300e\u6d77\u9614\u5929\u7a7a\u300f(82158598)&nbsp;17:43:31<\/div>\n<p><\/font>&nbsp;<\/div>\n<div class=\"msgContentDiv\"><font style=\"FONT-FAMILY: '\u6977\u4f53_GB2312','MS Sans Serif',sans-serif; FONT-SIZE: 12pt\" color=\"#0000ff\">hdu&nbsp;4127\u8868\u793a\u6df1\u641c+\u526a\u679d\uff0cRP\u6bd4\u8f83\u597d\u5c31\u8fc7\u4e86\u3002<\/font><\/div>\n<\/td>\n<\/tr>\n<tr style=\"BORDER-BOTTOM-COLOR: #f5f8fa; BORDER-TOP-COLOR: #f5f8fa; BORDER-RIGHT-COLOR: #f5f8fa; BORDER-LEFT-COLOR: #f5f8fa\" id=\"itemId_10\" bgcolor=\"#f5f8fa\">\n<td id=\"tn\">\n<div style=\"PADDING-LEFT: 10px; COLOR: #42b475\"><font style=\"FONT-FAMILY: '\u5fae\u8f6f\u96c5\u9ed1'; FONT-SIZE: 12px\"><\/p>\n<div style=\"FLOAT: left; MARGIN-RIGHT: 3px\">[HIT]xiaodao(251815992)&nbsp;17:43:40<\/div>\n<p><\/font>&nbsp;<\/div>\n<div class=\"msgContentDiv\"><font style=\"FONT-FAMILY: '\u5fae\u8f6f\u96c5\u9ed1','MS Sans Serif',sans-serif; FONT-SIZE: 11pt\" color=\"#600000\">\u3002\u3002\u3002\u3002\u3002\u3002\u3002\u3002\u3002\u3002\u3002\u3002\u3002\u3002\u3002\u3002\u3002\u3002\u3002\u3002\u3002\u3002\u3002\u3002\u3002\u3002\u3002\u3002\u3002\u3002\u3002<\/font><\/div>\n<\/td>\n<\/tr>\n<tr style=\"BORDER-BOTTOM-COLOR: #f5f8fa; BORDER-TOP-COLOR: #f5f8fa; BORDER-RIGHT-COLOR: #f5f8fa; BORDER-LEFT-COLOR: #f5f8fa\" id=\"itemId_11\" bgcolor=\"#f5f8fa\">\n<td id=\"tn\">\n<div style=\"PADDING-LEFT: 10px; COLOR: #42b475\"><font style=\"FONT-FAMILY: '\u5fae\u8f6f\u96c5\u9ed1'; FONT-SIZE: 12px\"><\/p>\n<div style=\"FLOAT: left; MARGIN-RIGHT: 3px\">[HIT]xiaodao(251815992)&nbsp;17:43:53<\/div>\n<p><\/font>&nbsp;<\/div>\n<div class=\"msgContentDiv\"><font style=\"FONT-FAMILY: '\u5fae\u8f6f\u96c5\u9ed1','MS Sans Serif',sans-serif; FONT-SIZE: 11pt\" color=\"#600000\">\u6709\u3002\u3002\u6709\u4ec0\u4e48\u9700\u8981\u989d\u5916\u6ce8\u610f\u7684\u5730\u65b9\u4e48\u3002\u3002<\/font><\/div>\n<\/td>\n<\/tr>\n<tr style=\"BORDER-BOTTOM-COLOR: #f5f8fa; BORDER-TOP-COLOR: #f5f8fa; BORDER-RIGHT-COLOR: #f5f8fa; BORDER-LEFT-COLOR: #f5f8fa\" id=\"itemId_12\" bgcolor=\"#f5f8fa\">\n<td id=\"tn\">\n<div style=\"PADDING-LEFT: 10px; COLOR: #006efe\"><font style=\"FONT-FAMILY: '\u5fae\u8f6f\u96c5\u9ed1'; FONT-SIZE: 12px\"><\/p>\n<div style=\"FLOAT: left; MARGIN-RIGHT: 3px\">\u300e\u6d77\u9614\u5929\u7a7a\u300f(82158598)&nbsp;17:44:26<\/div>\n<p><\/font>&nbsp;<\/div>\n<div class=\"msgContentDiv\"><font style=\"FONT-FAMILY: '\u6977\u4f53_GB2312','MS Sans Serif',sans-serif; FONT-SIZE: 12pt\" color=\"#0000ff\">\u6211\u628a\u521a\u5f00\u59cb\u7684\u8fde\u901a\u5757\u7f29\u70b9\u4e86<\/font><\/div>\n<\/td>\n<\/tr>\n<tr style=\"BORDER-BOTTOM-COLOR: #f5f8fa; BORDER-TOP-COLOR: #f5f8fa; BORDER-RIGHT-COLOR: #f5f8fa; BORDER-LEFT-COLOR: #f5f8fa\" id=\"itemId_13\" bgcolor=\"#f5f8fa\">\n<td id=\"tn\">\n<div style=\"PADDING-LEFT: 10px; COLOR: #42b475\"><font style=\"FONT-FAMILY: '\u5fae\u8f6f\u96c5\u9ed1'; FONT-SIZE: 12px\"><\/p>\n<div style=\"FLOAT: left; MARGIN-RIGHT: 3px\">[HIT]xiaodao(251815992)&nbsp;17:44:41<\/div>\n<p><\/font>&nbsp;<\/div>\n<div class=\"msgContentDiv\"><font style=\"FONT-FAMILY: '\u5fae\u8f6f\u96c5\u9ed1','MS Sans Serif',sans-serif; FONT-SIZE: 11pt\" color=\"#600000\">\u8fd9\u6837\u554a\u3002\u3002\u3002<\/font><\/div>\n<\/td>\n<\/tr>\n<tr style=\"BORDER-BOTTOM-COLOR: #f5f8fa; BORDER-TOP-COLOR: #f5f8fa; BORDER-RIGHT-COLOR: #f5f8fa; BORDER-LEFT-COLOR: #f5f8fa\" id=\"itemId_14\" bgcolor=\"#f5f8fa\">\n<td id=\"tn\">\n<div style=\"PADDING-LEFT: 10px; COLOR: #006efe\"><font style=\"FONT-FAMILY: '\u5fae\u8f6f\u96c5\u9ed1'; FONT-SIZE: 12px\"><\/p>\n<div style=\"FLOAT: left; MARGIN-RIGHT: 3px\">\u300e\u6d77\u9614\u5929\u7a7a\u300f(82158598)&nbsp;17:45:41<\/div>\n<p><\/font>&nbsp;<\/div>\n<div class=\"msgContentDiv\"><font style=\"FONT-FAMILY: '\u6977\u4f53_GB2312','MS Sans Serif',sans-serif; FONT-SIZE: 12pt\" color=\"#0000ff\">\u6211\u7684\u526a\u679d\u662f\u7528\u5269\u4f59\u591a\u5c11\u79cd\u989c\u8272\u505a\u526a\u679d\u3002<\/font><\/div>\n<\/td>\n<\/tr>\n<tr style=\"BORDER-BOTTOM-COLOR: #f5f8fa; BORDER-TOP-COLOR: #f5f8fa; BORDER-RIGHT-COLOR: #f5f8fa; BORDER-LEFT-COLOR: #f5f8fa\" id=\"itemId_15\" bgcolor=\"#f5f8fa\">\n<td id=\"tn\">\n<div style=\"PADDING-LEFT: 10px; COLOR: #42b475\"><font style=\"FONT-FAMILY: '\u5fae\u8f6f\u96c5\u9ed1'; FONT-SIZE: 12px\"><\/p>\n<div style=\"FLOAT: left; MARGIN-RIGHT: 3px\">[HIT]xiaodao(251815992)&nbsp;17:46:15<\/div>\n<p><\/font>&nbsp;<\/div>\n<div class=\"msgContentDiv\"><font style=\"FONT-FAMILY: '\u5fae\u8f6f\u96c5\u9ed1','MS Sans Serif',sans-serif; FONT-SIZE: 11pt\" color=\"#600000\">\u81f3\u591a\u662f\u8fde\u901a\u5757\u4e2a\u6570\u6b21\u3002\u3002\u81f3\u5c11\u662f\u5269\u4f59\u7684\u989c\u8272\u6b21\u8fd9\u6837\u4e48\u3002\u3002<\/font><\/div>\n<\/td>\n<\/tr>\n<tr style=\"BORDER-BOTTOM-COLOR: #f5f8fa; BORDER-TOP-COLOR: #f5f8fa; BORDER-RIGHT-COLOR: #f5f8fa; BORDER-LEFT-COLOR: #f5f8fa\" id=\"itemId_16\" bgcolor=\"#f5f8fa\">\n<td id=\"tn\">\n<div style=\"PADDING-LEFT: 10px; COLOR: #42b475\"><font style=\"FONT-FAMILY: '\u5fae\u8f6f\u96c5\u9ed1'; FONT-SIZE: 12px\"><\/p>\n<div style=\"FLOAT: left; MARGIN-RIGHT: 3px\">[HIT]xiaodao(251815992)&nbsp;17:46:39<\/div>\n<p><\/font>&nbsp;<\/div>\n<div class=\"msgContentDiv\"><font style=\"FONT-FAMILY: '\u5fae\u8f6f\u96c5\u9ed1','MS Sans Serif',sans-serif; FONT-SIZE: 11pt\" color=\"#600000\">\u3002\u3002\u3002\u597d\u5427\u3002\u3002\u6211\u518d\u53bb\u6539\u6539\u3002\u3002\uff09<\/font><\/div>\n<\/td>\n<\/tr>\n<tr style=\"BORDER-BOTTOM-COLOR: #f5f8fa; BORDER-TOP-COLOR: #f5f8fa; BORDER-RIGHT-COLOR: #f5f8fa; BORDER-LEFT-COLOR: #f5f8fa\" id=\"itemId_17\" bgcolor=\"#f5f8fa\">\n<td id=\"tn\">\n<div style=\"PADDING-LEFT: 10px; COLOR: #006efe\"><font style=\"FONT-FAMILY: '\u5fae\u8f6f\u96c5\u9ed1'; FONT-SIZE: 12px\"><\/p>\n<div style=\"FLOAT: left; MARGIN-RIGHT: 3px\">\u300e\u6d77\u9614\u5929\u7a7a\u300f(82158598)&nbsp;17:46:54<\/div>\n<p><\/font>&nbsp;<\/div>\n<div class=\"msgContentDiv\"><font style=\"FONT-FAMILY: '\u6977\u4f53_GB2312','MS Sans Serif',sans-serif; FONT-SIZE: 12pt\" color=\"#0000ff\">\u55ef\uff0c\u6211\u6ca1\u6709\u81f3\u591a\uff0c\u76f4\u63a5\u7528\u81f3\u5c11<\/font><\/div>\n<\/td>\n<\/tr>\n<\/table>\n<pre class=\"brush: cpp; collapse: true; light: false; title: G; toolbar: true; notranslate\" title=\"G\">\r\n\/** ` Micro Mezzo Macro Flation -- Overheated Economy ., **\/\r\n\r\n#include &lt;algorithm&gt;\r\n#include &lt;iostream&gt;\r\n#include &lt;iomanip&gt;\r\n#include &lt;sstream&gt;\r\n#include &lt;cstring&gt;\r\n#include &lt;complex&gt;\r\n#include &lt;cstdio&gt;\r\n#include &lt;string&gt;\r\n#include &lt;vector&gt;\r\n#include &lt;bitset&gt;\r\n#include &lt;queue&gt;\r\n#include &lt;stack&gt;\r\n#include &lt;cmath&gt;\r\n#include &lt;ctime&gt;\r\n#include &lt;list&gt;\r\n#include &lt;set&gt;\r\n#include &lt;map&gt;\r\n\r\nusing namespace std;\r\n\r\n#define REP(i, n) for (int i=0;i&lt;int(n);++i)\r\n#define FOR(i, a, b) for (int i=int(a);i&lt;int(b);++i)\r\n#define DWN(i, b, a) for (int i=int(b-1);i&gt;=int(a);--i)\r\n#define REP_1(i, n) for (int i=1;i&lt;=int(n);++i)\r\n#define FOR_1(i, a, b) for (int i=int(a);i&lt;=int(b);++i)\r\n#define DWN_1(i, b, a) for (int i=int(b);i&gt;=int(a);--i)\r\n#define REP_C(i, n) for (int n____=int(n),i=0;i&lt;n____;++i)\r\n#define FOR_C(i, a, b) for (int b____=int(b),i=a;i&lt;b____;++i)\r\n#define DWN_C(i, b, a) for (int a____=int(a),i=b-1;i&gt;=a____;--i)\r\n#define REP_N(i, n) for (i=0;i&lt;int(n);++i)\r\n#define FOR_N(i, a, b) for (i=int(a);i&lt;int(b);++i)\r\n#define DWN_N(i, b, a) for (i=int(b-1);i&gt;=int(a);--i)\r\n#define REP_1_C(i, n) for (int n____=int(n),i=1;i&lt;=n____;++i)\r\n#define FOR_1_C(i, a, b) for (int b____=int(b),i=a;i&lt;=b____;++i)\r\n#define DWN_1_C(i, b, a) for (int a____=int(a),i=b;i&gt;=a____;--i)\r\n#define REP_1_N(i, n) for (i=1;i&lt;=int(n);++i)\r\n#define FOR_1_N(i, a, b) for (i=int(a);i&lt;=int(b);++i)\r\n#define DWN_1_N(i, b, a) for (i=int(b);i&gt;=int(a);--i)\r\n#define REP_C_N(i, n) for (n____=int(n),i=0;i&lt;n____;++i)\r\n#define FOR_C_N(i, a, b) for (b____=int(b),i=a;i&lt;b____;++i)\r\n#define DWN_C_N(i, b, a) for (a____=int(a),i=b-1;i&gt;=a____;--i)\r\n#define REP_1_C_N(i, n) for (n____=int(n),i=1;i&lt;=n____;++i)\r\n#define FOR_1_C_N(i, a, b) for (b____=int(b),i=a;i&lt;=b____;++i)\r\n#define DWN_1_C_N(i, b, a) for (a____=int(a),i=b;i&gt;=a____;--i)\r\n\r\n#define DO(n) while(n--)\r\n#define DO_C(n) int n____ = n; while(n____--)\r\n#define TO(i, a, b) int s_=a&lt;b?1:-1,b_=b+s_;for(int i=a;i!=b_;i+=s_)\r\n#define TO_1(i, a, b) int s_=a&lt;b?1:-1,b_=b;for(int i=a;i!=b_;i+=s_)\r\n#define SQZ(i, j, a, b) for (int i=int(a),j=int(b)-1;i&lt;j;++i,--j)\r\n#define SQZ_1(i, j, a, b) for (int i=int(a),j=int(b);i&lt;=j;++i,--j)\r\n#define REP_2(i, j, n, m) REP(i, n) REP(j, m)\r\n#define REP_2_1(i, j, n, m) REP_1(i, n) REP_1(j, m)\r\n\r\n#define ALL(A) A.begin(), A.end()\r\n#define LLA(A) A.rbegin(), A.rend()\r\n#define CPY(A, B) memcpy(A, B, sizeof(A))\r\n#define INS(A, P, B) A.insert(A.begin() + P, B)\r\n#define ERS(A, P) A.erase(A.begin() + P)\r\n#define BSC(A, X) find(ALL(A), X) \/\/ != A.end()\r\n#define CTN(T, x) (T.find(x) != T.end())\r\n#define SZ(A) int(A.size())\r\n#define PB push_back\r\n#define MP(A, B) make_pair(A, B)\r\n\r\n#define Rush int T____; RD(T____); DO(T____)\r\n#pragma comment(linker, &quot;\/STACK:36777216&quot;)\r\n#pragma GCC optimize (&quot;O2&quot;)\r\n#define Ruby system(&quot;ruby main.rb&quot;)\r\n#define Haskell system(&quot;runghc main.hs&quot;)\r\n#define Pascal system(&quot;fpc main.pas&quot;)\r\n\r\ntypedef long long LL;\r\ntypedef double DB;\r\ntypedef unsigned UINT;\r\ntypedef unsigned long long ULL;\r\n\r\ntypedef vector&lt;int&gt; VI;\r\ntypedef vector&lt;char&gt; VC;\r\ntypedef vector&lt;string&gt; VS;\r\ntypedef vector&lt;LL&gt; VL;\r\ntypedef vector&lt;DB&gt; VD;\r\ntypedef set&lt;int&gt; SI;\r\ntypedef set&lt;string&gt; SS;\r\ntypedef set&lt;LL&gt; SL;\r\ntypedef set&lt;DB&gt; SD;\r\ntypedef map&lt;int, int&gt; MII;\r\ntypedef map&lt;string, int&gt; MSI;\r\ntypedef map&lt;LL, int&gt; MLI;\r\ntypedef map&lt;DB, int&gt; MDI;\r\ntypedef map&lt;int, bool&gt; MIB;\r\ntypedef map&lt;string, bool&gt; MSB;\r\ntypedef map&lt;LL, bool&gt; MLB;\r\ntypedef map&lt;DB, bool&gt; MDB;\r\ntypedef pair&lt;int, int&gt; PII;\r\ntypedef pair&lt;int, bool&gt; PIB;\r\ntypedef vector&lt;PII&gt; VII;\r\ntypedef vector&lt;VI&gt; VVI;\r\ntypedef vector&lt;VII&gt; VVII;\r\ntypedef set&lt;PII&gt; SII;\r\ntypedef map&lt;PII, int&gt; MPIII;\r\ntypedef map&lt;PII, bool&gt; MPIIB;\r\n\r\n\r\n\/** I\/O Accelerator **\/\r\n\r\n\/* ... :&quot; We are I\/O Accelerator ... Use us at your own risk ;) ... &quot; .. *\/\r\n\r\ntemplate&lt;class T&gt; inline void RD(T &amp;);\r\ntemplate&lt;class T&gt; inline void OT(const T &amp;);\r\n\r\ninline int RD(){ int x; RD(x); return x;}\r\ntemplate&lt;class T&gt; inline T&amp; _RD(T &amp;x){ RD(x); return x;}\r\ninline void RC(char &amp;c){scanf(&quot; %c&quot;, &amp;c);}\r\ninline void RS(char *s){scanf(&quot;%s&quot;, s);}\r\n\r\ntemplate&lt;class T0, class T1&gt; inline void RD(T0 &amp;x0, T1 &amp;x1){RD(x0), RD(x1);}\r\ntemplate&lt;class T0, class T1, class T2&gt; inline void RD(T0 &amp;x0, T1 &amp;x1, T2 &amp;x2){RD(x0), RD(x1), RD(x2);}\r\ntemplate&lt;class T0, class T1, class T2, class T3&gt; inline void RD(T0 &amp;x0, T1 &amp;x1, T2 &amp;x2, T3 &amp;x3){RD(x0), RD(x1), RD(x2), RD(x3);}\r\ntemplate&lt;class T0, class T1, class T2, class T3, class T4&gt; inline void RD(T0 &amp;x0, T1 &amp;x1, T2 &amp;x2, T3 &amp;x3, T4 &amp;x4){RD(x0), RD(x1), RD(x2), RD(x3), RD(x4);}\r\ntemplate&lt;class T0, class T1, class T2, class T3, class T4, class T5&gt; inline void RD(T0 &amp;x0, T1 &amp;x1, T2 &amp;x2, T3 &amp;x3, T4 &amp;x4, T5 &amp;x5){RD(x0), RD(x1), RD(x2), RD(x3), RD(x4), RD(x5);}\r\ntemplate&lt;class T0, class T1, class T2, class T3, class T4, class T5, class T6&gt; inline void RD(T0 &amp;x0, T1 &amp;x1, T2 &amp;x2, T3 &amp;x3, T4 &amp;x4, T5 &amp;x5, T6 &amp;x6){RD(x0), RD(x1), RD(x2), RD(x3), RD(x4), RD(x5), RD(x6);}\r\ntemplate&lt;class T0, class T1&gt; inline void OT(T0 &amp;x0, T1 &amp;x1){OT(x0), OT(x1);}\r\ntemplate&lt;class T0, class T1, class T2&gt; inline void OT(T0 &amp;x0, T1 &amp;x1, T2 &amp;x2){OT(x0), OT(x1), OT(x2);}\r\ntemplate&lt;class T0, class T1, class T2, class T3&gt; inline void OT(T0 &amp;x0, T1 &amp;x1, T2 &amp;x2, T3 &amp;x3){OT(x0), OT(x1), OT(x2), OT(x3);}\r\ntemplate&lt;class T0, class T1, class T2, class T3, class T4&gt; inline void OT(T0 &amp;x0, T1 &amp;x1, T2 &amp;x2, T3 &amp;x3, T4 &amp;x4){OT(x0), OT(x1), OT(x2), OT(x3), OT(x4);}\r\ntemplate&lt;class T0, class T1, class T2, class T3, class T4, class T5&gt; inline void OT(T0 &amp;x0, T1 &amp;x1, T2 &amp;x2, T3 &amp;x3, T4 &amp;x4, T5 &amp;x5){OT(x0), OT(x1), OT(x2), OT(x3), OT(x4), OT(x5);}\r\ntemplate&lt;class T0, class T1, class T2, class T3, class T4, class T5, class T6&gt; inline void OT(T0 &amp;x0, T1 &amp;x1, T2 &amp;x2, T3 &amp;x3, T4 &amp;x4, T5 &amp;x5, T6 &amp;x6){OT(x0), OT(x1), OT(x2), OT(x3), OT(x4), OT(x5), OT(x6);}\r\n\r\ntemplate&lt;class T&gt; inline void RST(T &amp;A){memset(A, 0, sizeof(A));}\r\ntemplate&lt;class T0, class T1&gt; inline void RST(T0 &amp;A0, T1 &amp;A1){RST(A0), RST(A1);}\r\ntemplate&lt;class T0, class T1, class T2&gt; inline void RST(T0 &amp;A0, T1 &amp;A1, T2 &amp;A2){RST(A0), RST(A1), RST(A2);}\r\ntemplate&lt;class T0, class T1, class T2, class T3&gt; inline void RST(T0 &amp;A0, T1 &amp;A1, T2 &amp;A2, T3 &amp;A3){RST(A0), RST(A1), RST(A2), RST(A3);}\r\ntemplate&lt;class T0, class T1, class T2, class T3, class T4&gt; inline void RST(T0 &amp;A0, T1 &amp;A1, T2 &amp;A2, T3 &amp;A3, T4 &amp;A4){RST(A0), RST(A1), RST(A2), RST(A3), RST(A4);}\r\ntemplate&lt;class T0, class T1, class T2, class T3, class T4, class T5&gt; inline void RST(T0 &amp;A0, T1 &amp;A1, T2 &amp;A2, T3 &amp;A3, T4 &amp;A4, T5 &amp;A5){RST(A0), RST(A1), RST(A2), RST(A3), RST(A4), RST(A5);}\r\ntemplate&lt;class T0, class T1, class T2, class T3, class T4, class T5, class T6&gt; inline void RST(T0 &amp;A0, T1 &amp;A1, T2 &amp;A2, T3 &amp;A3, T4 &amp;A4, T5 &amp;A5, T6 &amp;A6){RST(A0), RST(A1), RST(A2), RST(A3), RST(A4), RST(A5), RST(A6);}\r\n\r\ntemplate&lt;class T&gt; inline void CLR(T &amp;A){A.clear();}\r\ntemplate&lt;class T0, class T1&gt; inline void CLR(T0 &amp;A0, T1 &amp;A1){CLR(A0), CLR(A1);}\r\ntemplate&lt;class T0, class T1, class T2&gt; inline void CLR(T0 &amp;A0, T1 &amp;A1, T2 &amp;A2){CLR(A0), CLR(A1), CLR(A2);}\r\ntemplate&lt;class T0, class T1, class T2, class T3&gt; inline void CLR(T0 &amp;A0, T1 &amp;A1, T2 &amp;A2, T3 &amp;A3){CLR(A0), CLR(A1), CLR(A2), CLR(A3);}\r\ntemplate&lt;class T0, class T1, class T2, class T3, class T4&gt; inline void CLR(T0 &amp;A0, T1 &amp;A1, T2 &amp;A2, T3 &amp;A3, T4 &amp;A4){CLR(A0), CLR(A1), CLR(A2), CLR(A3), CLR(A4);}\r\ntemplate&lt;class T0, class T1, class T2, class T3, class T4, class T5&gt; inline void CLR(T0 &amp;A0, T1 &amp;A1, T2 &amp;A2, T3 &amp;A3, T4 &amp;A4, T5 &amp;A5){CLR(A0), CLR(A1), CLR(A2), CLR(A3), CLR(A4), CLR(A5);}\r\ntemplate&lt;class T0, class T1, class T2, class T3, class T4, class T5, class T6&gt; inline void CLR(T0 &amp;A0, T1 &amp;A1, T2 &amp;A2, T3 &amp;A3, T4 &amp;A4, T5 &amp;A5, T6 &amp;A6){CLR(A0), CLR(A1), CLR(A2), CLR(A3), CLR(A4), CLR(A5), CLR(A6);}\r\ntemplate&lt;class T&gt; inline void CLR(T &amp;A, int n){REP(i, n) CLR(A&#x5B;i]);}\r\ntemplate&lt;class T&gt; inline void FLC(T &amp;A, int x){memset(A, x, sizeof(A));}\r\ntemplate&lt;class T0, class T1&gt; inline void FLC(T0 &amp;A0, T1 &amp;A1, int x){FLC(A0, x), FLC(A1, x);}\r\ntemplate&lt;class T0, class T1, class T2&gt; inline void FLC(T0 &amp;A0, T1 &amp;A1, T2 &amp;A2){FLC(A0), FLC(A1), FLC(A2);}\r\ntemplate&lt;class T0, class T1, class T2, class T3&gt; inline void FLC(T0 &amp;A0, T1 &amp;A1, T2 &amp;A2, T3 &amp;A3){FLC(A0), FLC(A1), FLC(A2), FLC(A3);}\r\ntemplate&lt;class T0, class T1, class T2, class T3, class T4&gt; inline void FLC(T0 &amp;A0, T1 &amp;A1, T2 &amp;A2, T3 &amp;A3, T4 &amp;A4){FLC(A0), FLC(A1), FLC(A2), FLC(A3), FLC(A4);}\r\ntemplate&lt;class T0, class T1, class T2, class T3, class T4, class T5&gt; inline void FLC(T0 &amp;A0, T1 &amp;A1, T2 &amp;A2, T3 &amp;A3, T4 &amp;A4, T5 &amp;A5){FLC(A0), FLC(A1), FLC(A2), FLC(A3), FLC(A4), FLC(A5);}\r\ntemplate&lt;class T0, class T1, class T2, class T3, class T4, class T5, class T6&gt; inline void FLC(T0 &amp;A0, T1 &amp;A1, T2 &amp;A2, T3 &amp;A3, T4 &amp;A4, T5 &amp;A5, T6 &amp;A6){FLC(A0), FLC(A1), FLC(A2), FLC(A3), FLC(A4), FLC(A5), FLC(A6);}\r\n\r\ntemplate&lt;class T&gt; inline void SRT(T &amp;A){sort(ALL(A));}\r\ntemplate&lt;class T, class C&gt; inline void SRT(T &amp;A, C B){sort(ALL(A), B);}\r\n\r\n\r\n\r\n\r\n\/** Add - On **\/\r\n\r\nconst int MOD = 1000000007;\r\nconst int INF = 0x7fffffff;\r\nconst DB EPS = 1e-6;\r\nconst DB OO = 1e15;\r\nconst DB PI = acos(-1.0);\r\n\r\n\/\/ &lt;&lt;= ` 0. Daily Use .,\r\n\r\ntemplate&lt;class T&gt; inline void checkMin(T &amp;a,const T b){if (b&lt;a) a=b;}\r\ntemplate&lt;class T&gt; inline void checkMax(T &amp;a,const T b){if (b&gt;a) a=b;}\r\ntemplate &lt;class T, class C&gt; inline void checkMin(T&amp; a, const T b, C c){if (c(b,a)) a = b;}\r\ntemplate &lt;class T, class C&gt; inline void checkMax(T&amp; a, const T b, C c){if (c(a,b)) a = b;}\r\ntemplate&lt;class T&gt; inline T min(T a, T b, T c){return min(min(a, b), c);}\r\ntemplate&lt;class T&gt; inline T max(T a, T b, T c){return max(max(a, b), c);}\r\ntemplate&lt;class T&gt; inline T sqr(T a){return a*a;}\r\ntemplate&lt;class T&gt; inline T cub(T a){return a*a*a;}\r\nint Ceil(int x, int y){return (x - 1) \/ y + 1;}\r\n\r\n\/\/ &lt;&lt;= ` 1. Bitwise Operation .,\r\n\r\ninline bool _1(LL x, int i){return x &amp; 1LL&lt;&lt;i;}\r\ninline LL _1(int i){return 1LL&lt;&lt;i;}\r\ninline LL _U(int i){return _1(i) - 1LL;};\r\n\r\ninline int count_bits(int x){\r\n    x = (x &amp; 0x55555555) + ((x &amp; 0xaaaaaaaa) &gt;&gt; 1);\r\n    x = (x &amp; 0x33333333) + ((x &amp; 0xcccccccc) &gt;&gt; 2);\r\n    x = (x &amp; 0x0f0f0f0f) + ((x &amp; 0xf0f0f0f0) &gt;&gt; 4);\r\n    x = (x &amp; 0x00ff00ff) + ((x &amp; 0xff00ff00) &gt;&gt; 8);\r\n    x = (x &amp; 0x0000ffff) + ((x &amp; 0xffff0000) &gt;&gt; 16);\r\n    return x;\r\n}\r\n\r\ntemplate&lt;class T&gt; inline T low_bit(T x) {\r\n    return x &amp; -x;\r\n}\r\n\r\ntemplate&lt;class T&gt; inline T high_bit(T x) {\r\n    T p = low_bit(x);\r\n    while (p != x) x -= p, p = low_bit(x);\r\n    return p;\r\n}\r\n\r\n\/\/ &lt;&lt;= ` 2. Modular Arithmetic Basic .,\r\n\r\ninline void INC(int &amp;a, int b){a += b; if (a &gt;= MOD) a -= MOD;}\r\ninline int sum(int a, int b){a += b; if (a &gt;= MOD) a -= MOD; return a;}\r\ninline void DEC(int &amp;a, int b){a -= b; if (a &lt; 0) a += MOD;}\r\ninline int dff(int a, int b){a -= b; if (a &lt; 0) a  += MOD; return a;}\r\ninline void MUL(int &amp;a, int b){a = int((LL)a * b % MOD);}\r\ninline int pdt(int a, int b){return int((LL)a * b % MOD);}\r\n\r\n\r\n\/\/ &lt;&lt;= ' 0. I\/O Accelerator interface .,\r\n\r\ntemplate&lt;class T&gt; inline void RD(T &amp;x){\r\n    \/\/cin &gt;&gt; x;\r\n    \/\/scanf(&quot;%d&quot;, &amp;x);\r\n    char c; for (c = getchar(); c &lt; '0'; c = getchar()); x = c - '0'; for (c = getchar(); c &gt;= '0'; c = getchar()) x = x * 10 + c - '0';\r\n    \/\/char c; c = getchar(); x = c - '0'; for (c = getchar(); c &gt;= '0'; c = getchar()) x = x * 10 + c - '0';\r\n\r\n}\r\n\r\nint ____Case;\r\ntemplate&lt;class T&gt; inline void OT(const T &amp;x){\r\n    \/\/cout &lt;&lt; x &lt;&lt; endl;\r\n    printf(&quot;%d\\n&quot;, x);\r\n    \/\/printf(&quot;%.2lf\\n&quot;, x);\r\n    \/\/printf(&quot;Case %d: %d\\n&quot;, ++____Case, x);\r\n}\r\n\r\n\r\n#define For_each(it, A) for (SII::iterator it = A.begin(); it != A.end(); ++it)\r\n#define state pair&lt; pair&lt;PII, PII&gt;, pair&lt;PII, PII&gt; &gt;\r\n\/* .................................................................................................................................. *\/\r\n\r\nconst int dx&#x5B;] = {-1, 0, 1, 0}, dy&#x5B;] = {0, 1, 0, -1};\r\nconst int N = 8, C = 6;\r\n\r\nLL B&#x5B;C+1], G&#x5B;C], vis; int ans;\r\nint n;\r\n\r\ninline int Board(int x, int y){\r\n    REP(i, C) if (_1(B&#x5B;i], x * n + y)) return i;\r\n    return C;\r\n}\r\n\r\nvoid dfs1(int x, int y){\r\n    B&#x5B;C] |= _1(x * n + y); REP(i, 4){\r\n        int xx = x + dx&#x5B;i], yy = y + dy&#x5B;i];\r\n        if (xx &lt; 0 || yy &lt; 0 || xx == n || yy == n) continue;\r\n        if (Board(xx, yy) == Board(0, 0) &amp;&amp; !_1(B&#x5B;C], xx * n + yy)) dfs1(xx, yy);\r\n    }\r\n}\r\n\r\n\r\n\r\nvoid dfs2(int x, int y){\r\n\r\n    vis |= _1(x * n + y); REP(i, 4){\r\n        int xx = x + dx&#x5B;i], yy = y + dy&#x5B;i];\r\n        if (xx &lt; 0 || yy &lt; 0 || xx == n || yy == n) continue;\r\n        if (!_1(vis, xx * n + yy)){\r\n            if (Board(xx, yy) == C) dfs2(xx, yy);\r\n            else {\r\n                if (Board(x, y) == C || Board(x, y) == Board(xx, yy)){\r\n                    G&#x5B;Board(xx, yy)] |= _1(xx * n + yy);\r\n                    dfs2(xx, yy);\r\n                }\r\n            }\r\n        }\r\n    }\r\n}\r\n\r\nvoid init(){\r\n    RST(B); REP_2(i, j, n, n) B&#x5B;RD()] |= _1(i * n + j);\r\n    dfs1(0, 0), B&#x5B;Board(0, 0)] ^= B&#x5B;C];\r\n\r\n}\r\n\r\nvoid display(LL x){\r\n    REP(i, n){\r\n        REP(j, n) cout &lt;&lt; _1(x, i * n + j) &lt;&lt; &quot; &quot;;\r\n        cout &lt;&lt; endl;\r\n    }\r\n    cout &lt;&lt; endl;\r\n}\r\n\r\nvoid patch(){\r\n    REP(i, n){\r\n        REP(j, n) cout &lt;&lt; Board(i, j) &lt;&lt; &quot; &quot;;\r\n        cout &lt;&lt; endl;\r\n    }\r\n    cout &lt;&lt; endl;\r\n}\r\n\r\ninline void dfs(int color, int step){\r\n    if (step + color &gt;= ans) return;\r\n    if (color == 0) ans = step;\r\n    else {\r\n        RST(G), vis = 0, dfs2(0, 0); LL GG&#x5B;C]; CPY(GG, G); REP(i, C) if (GG&#x5B;i]){\r\n            B&#x5B;C] |= GG&#x5B;i], B&#x5B;i] ^= GG&#x5B;i]; dfs(color - !B&#x5B;i], step + 1);\r\n            B&#x5B;C] ^= GG&#x5B;i], B&#x5B;i] |= GG&#x5B;i];\r\n        }\r\n    }\r\n}\r\n\r\ninline int count_color(){\r\n    int res = 0; REP(i, C) if (B&#x5B;i]) ++res;\r\n    return res;\r\n}\r\n\r\n\r\nvoid solve(){\r\n\r\n    LL flag; if (n == 8) flag = -1; else flag = _U(n * n);\r\n    if (B&#x5B;C] == flag) ans = 0;\r\n    else ans = INF, dfs(count_color(), 0);\r\n\r\n}\r\n\r\nint main(){\r\n\r\n    \/\/freopen(&quot;in.txt&quot;, &quot;r&quot;, stdin);\r\n\r\n    while (scanf(&quot;%d&quot;, &amp;n) != EOF &amp;&amp; n){\r\n\r\n        init(); solve(); OT(ans);\r\n    }\r\n}\r\n<\/pre>\n<h3>External link: <\/h3>\n<p><a href=\"http:\/\/acm.hdu.edu.cn\/showproblem.php?pid=4127\">http:\/\/acm.hdu.edu.cn\/showproblem.php?pid=4127<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Brief description: \u7565\u3002\u3002<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","enabled":false}}},"categories":[47],"tags":[48],"class_list":["post-143","post","type-post","status-publish","format-standard","hentry","category-hdu","tag-48"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p2tdP7-2j","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.shuizilong.com\/house\/wp-json\/wp\/v2\/posts\/143","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.shuizilong.com\/house\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.shuizilong.com\/house\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.shuizilong.com\/house\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.shuizilong.com\/house\/wp-json\/wp\/v2\/comments?post=143"}],"version-history":[{"count":0,"href":"https:\/\/www.shuizilong.com\/house\/wp-json\/wp\/v2\/posts\/143\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.shuizilong.com\/house\/wp-json\/wp\/v2\/media?parent=143"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.shuizilong.com\/house\/wp-json\/wp\/v2\/categories?post=143"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.shuizilong.com\/house\/wp-json\/wp\/v2\/tags?post=143"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}