锘??xml version="1.0" encoding="utf-8" standalone="yes"?>久久精品www人人爽人人,一本久久综合亚洲鲁鲁五月天亚洲欧美一区二区 ,久久久久亚洲av无码专区http://www.shnenglu.com/y346491470/category/17560.html涓烘ⅵ鎯寵屽鏂楋紒zh-cnFri, 06 Jul 2012 11:00:46 GMTFri, 06 Jul 2012 11:00:46 GMT60hust 1017 Exact cover - DLXhttp://www.shnenglu.com/y346491470/articles/181915.htmly @ The Angry Teletubbiesy @ The Angry TeletubbiesFri, 06 Jul 2012 09:03:00 GMThttp://www.shnenglu.com/y346491470/articles/181915.htmlhttp://www.shnenglu.com/y346491470/comments/181915.htmlhttp://www.shnenglu.com/y346491470/articles/181915.html#Feedback0http://www.shnenglu.com/y346491470/comments/commentRss/181915.htmlhttp://www.shnenglu.com/y346491470/services/trackbacks/181915.html
銆愰瑙c戯細綺劇‘瑕嗙洊錛孌LX鏈鍩虹鐨勯鐩紝鍗佸瓧閾捐〃紜疄瓚沖寮哄ぇ銆傜暀涓婦LX妯℃澘~

銆愪唬鐮併戯細
  1 #include "iostream"
  2 #include "cstdio"
  3 #include "cstring"
  4 #include "algorithm"
  5 #include "vector"
  6 #include "queue"
  7 #include "cmath"
  8 #include "string"
  9 #include "cctype"
 10 #include "map"
 11 #include "iomanip"
 12 #include "set"
 13 #include "utility"
 14 using namespace std;
 15 typedef pair<intint> pii;
 16 #define pb push_back
 17 #define mp make_pair
 18 #define fi first
 19 #define se second
 20 #define sof(x) sizeof(x)
 21 #define lc(x) (x << 1)
 22 #define rc(x) (x << 1 | 1)
 23 #define lowbit(x) (x & (-x))
 24 #define ll long long
 25 
 26 #define CC 1011
 27 #define MM 102011
 28 
 29 int L[MM], R[MM], U[MM], D[MM], C[MM], X[MM];
 30 int H[CC], S[CC], Q[CC];
 31 int size, n, m;
 32 
 33 void remove(int c) {
 34     R[L[c]] = R[c], L[R[c]] = L[c];
 35     for(int i = D[c]; i != c; i = D[i])
 36         for(int j = R[i]; j != i; j = R[j])
 37             U[D[j]] = U[j], D[U[j]] = D[j], --S[C[j]];
 38 }
 39 
 40 void resume(int c) {
 41     R[L[c]] = L[R[c]] = c;
 42     for(int i = U[c]; i != c; i = U[i])
 43         for(int j = L[i]; j != i ; j = L[j])
 44             ++S[C[U[D[j]] = D[U[j]] = j]];
 45 }
 46 
 47 bool Dance(int k) {
 48     int c = -1;
 49     if(!R[0]) {
 50         printf("%d", k);
 51         for(int i = 0; i < k; i++) printf(" %d", X[Q[i]]);
 52         puts("");
 53         return true;
 54     }
 55     for(int tmp = CC, i = R[0]; i; i = R[i])
 56         if(S[i] < tmp) tmp = S[c=i];
 57     remove(c);
 58     for(int i = D[c]; i != c; i = D[i]) {
 59         Q[k] = i;
 60         for(int j = R[i]; j != i; j = R[j]) remove(C[j]);
 61         if(Dance(k + 1)) return true;
 62         for(int j = L[i]; j != i; j = L[j]) resume(C[j]);
 63     }
 64     resume(c);
 65     return false;
 66 }
 67 
 68 void Link(int r, int c) {
 69     ++S[C[size] = c];
 70     D[size] = D[c];
 71     U[D[c]] = size;
 72     U[size] = c;
 73     D[c] = size;
 74     if(H[r] < 0) H[r] = L[size] = R[size] = size;
 75     else {
 76         R[size] = R[H[r]];
 77         L[R[H[r]]] = size;
 78         L[size] = H[r];
 79         R[H[r]] = size;
 80     }
 81     X[size++] = r;
 82 }
 83 
 84 int main() {
 85     int num;
 86     while(~scanf("%d%d", &n, &m)) {
 87         for(int i = 0; i <= m; i++) {
 88             S[i] = 0;
 89             D[i] = U[i] = i;
 90             L[i+1] = i;
 91             R[i] = i + 1;
 92         }
 93         R[m] = 0;
 94         L[0] = m;
 95         size = m + 1;
 96         for(int i = 1, j; i <= n; i++) {
 97             H[i] = -1;//璁板緱瑕佸厛緗澶存寚閽堜負-1
 98             scanf("%d", &num);
 99             while(num--) {
100                 scanf("%d", &j);
101                 Link(i, j);
102             }
103         }
104         if(!Dance(0)) puts("NO");
105     }
106     return 0;
107 }
108 


y @ The Angry Teletubbies 2012-07-06 17:03 鍙戣〃璇勮
]]>
hdoj 4198 Quick out of the Harbour - 浼樺厛闃熷垪 + bfshttp://www.shnenglu.com/y346491470/articles/170043.htmly @ The Angry Teletubbiesy @ The Angry TeletubbiesWed, 04 Apr 2012 05:25:00 GMThttp://www.shnenglu.com/y346491470/articles/170043.htmlhttp://www.shnenglu.com/y346491470/comments/170043.htmlhttp://www.shnenglu.com/y346491470/articles/170043.html#Feedback0http://www.shnenglu.com/y346491470/comments/commentRss/170043.htmlhttp://www.shnenglu.com/y346491470/services/trackbacks/170043.html
銆愰瑙c戯細璧よ8瑁哥殑浼樺厛闃熷垪+bfs銆?br />               鎼滃埌絎竴涓竟鐣岀偣鍗充負絳旀銆?br />
銆愪唬鐮併戯細
 1 #include "iostream"
 2 #include "cstdio"
 3 #include "cstring"
 4 #include "algorithm"
 5 #include "vector"
 6 #include "queue"
 7 #include "cmath"
 8 #include "string"
 9 #include "cctype"
10 #include "map"
11 #include "iomanip"
12 using namespace std;
13 #define pb push_back
14 #define mp make_pair
15 #define fi first
16 #define se second
17 #define lc(x) (x << 1)
18 #define rc(x) (x << 1 | 1)
19 #define lowbit(x) (x & (-x))
20 #define ll long long
21 int h, w, d;
22 int sx, sy;
23 char maz[510][510];
24 int dx[] = {0, 0, 1, -1};
25 int dy[]=  {-1, 1, 0, 0};
26 bool visit[510][510];
27 struct Node {
28     int x, y, ti;
29     Node(){}
30     Node(int _x, int _y, int _ti) {
31         x = _x, y = _y, ti = _ti;
32     }
33     bool operator<(const Node &x) const {
34         return ti > x.ti;
35     }
36 };
37 
38 bool check(int x, int y) {
39     if(x >= 1 && x <= h && y >= 1 && y <= w) return true;
40     return false;
41 }
42 
43 void bfs() {
44     int ans;
45     memset(visit, falsesizeof(visit));
46     priority_queue<Node> que;
47     que.push(Node(sx, sy, 1));
48     while(!que.empty()) {
49         Node now = que.top();
50         que.pop();
51         if(visit[now.x][now.y]) continue;
52         else visit[now.x][now.y] = true;
53         if((now.x == 1 || now.x == h) || (now.y == 1 || now.y == w)) {
54             ans = now.ti;
55             break;
56         }
57         for(int i = 0; i < 4; i++) {
58             int nx = now.x + dx[i], ny = now.y + dy[i];
59             if(!check(nx, ny) || maz[nx][ny] == '#' || visit[nx][ny]) continue;
60             if(maz[nx][ny] == '@') que.push(Node(nx, ny, now.ti + d + 1));
61             else que.push(Node(nx, ny, now.ti + 1));
62         }
63     }
64     cout << ans << endl;
65 }
66 
67 int main() {
68     int T;
69     scanf("%d", &T);
70     while(T--) {
71         scanf("%d%d%d", &h, &w, &d);
72         for(int i = 1; i <= h; i++) {
73             scanf("%s", &maz[i][1]);
74         }
75         for(int i = 1; i <= h; i++) {
76             for(int j = 1; j <= w; j++) {
77                 if(maz[i][j] == 'S') sx = i, sy = j;
78             }
79         }
80         bfs();
81     }
82     return 0;
83 }
84 


y @ The Angry Teletubbies 2012-04-04 13:25 鍙戣〃璇勮
]]>
poj 1129 Channel Allocation - IDA鏄?+ 鍥炬煋鑹?/title><link>http://www.shnenglu.com/y346491470/articles/169031.html</link><dc:creator>y @ The Angry Teletubbies</dc:creator><author>y @ The Angry Teletubbies</author><pubDate>Mon, 26 Mar 2012 09:27:00 GMT</pubDate><guid>http://www.shnenglu.com/y346491470/articles/169031.html</guid><wfw:comment>http://www.shnenglu.com/y346491470/comments/169031.html</wfw:comment><comments>http://www.shnenglu.com/y346491470/articles/169031.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.shnenglu.com/y346491470/comments/commentRss/169031.html</wfw:commentRss><trackback:ping>http://www.shnenglu.com/y346491470/services/trackbacks/169031.html</trackback:ping><description><![CDATA[銆愰鎰忋戯細緇欏嚭涓涓鉤闈㈠浘錛岄棶鏈灝戠敤澶氬皯縐嶉鑹叉潵鏌撹壊鑳藉浣垮緱鍥句腑鐩擱偦緇撶偣棰滆壊涓嶅悓銆?br /><br />銆愰瑙c戯細騫抽潰鍥炬煋鑹詫紝鍥涜壊瀹氱悊銆?br />               鐩存帴dfs鍗沖彲銆?br /><br />銆愪唬鐮併戯細<br /><div style="background-color:#eeeeee;font-size:13px;border:1px solid #CCCCCC;padding-right: 5px;padding-bottom: 4px;padding-left: 4px;padding-top: 4px;width: 98%;word-break:break-all"><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><span style="color: #008080; "> 1</span> #include "iostream"<br /><span style="color: #008080; "> 2</span> #include "cstdio"<br /><span style="color: #008080; "> 3</span> #include "cstring"<br /><span style="color: #008080; "> 4</span> #include "algorithm"<br /><span style="color: #008080; "> 5</span> #include "vector"<br /><span style="color: #008080; "> 6</span> #include "queue"<br /><span style="color: #008080; "> 7</span> #include "cmath"<br /><span style="color: #008080; "> 8</span> #include "string"<br /><span style="color: #008080; "> 9</span> #include "cctype"<br /><span style="color: #008080; ">10</span> #include "map"<br /><span style="color: #008080; ">11</span> #include "iomanip"<br /><span style="color: #008080; ">12</span> <span style="color: #0000FF; ">using</span> <span style="color: #0000FF; ">namespace</span> std;<br /><span style="color: #008080; ">13</span> <span style="color: #0000FF; ">#define</span> pb push_back<br /><span style="color: #008080; ">14</span> <span style="color: #0000FF; ">#define</span> mp make_pair<br /><span style="color: #008080; ">15</span> <span style="color: #0000FF; ">#define</span> fi first<br /><span style="color: #008080; ">16</span> <span style="color: #0000FF; ">#define</span> se second<br /><span style="color: #008080; ">17</span> <span style="color: #0000FF; ">#define</span> lc(x) (x << 1)<br /><span style="color: #008080; ">18</span> <span style="color: #0000FF; ">#define</span> rc(x) (x << 1 | 1)<br /><span style="color: #008080; ">19</span> <span style="color: #0000FF; ">#define</span> lowbit(x) (x & (-x))<br /><span style="color: #008080; ">20</span> <span style="color: #0000FF; ">#define</span> ll long long<br /><span style="color: #008080; ">21</span> <span style="color: #0000FF; ">#define</span> maxn 30<br /><span style="color: #008080; ">22</span> <span style="color: #0000FF; ">bool</span> maz[maxn][maxn], visit[maxn];<br /><span style="color: #008080; ">23</span> <span style="color: #0000FF; ">int</span> color[maxn];<br /><span style="color: #008080; ">24</span> <span style="color: #0000FF; ">int</span> n;<br /><span style="color: #008080; ">25</span> <span style="color: #0000FF; ">char</span> a, ch;<br /><span style="color: #008080; ">26</span> <br /><span style="color: #008080; ">27</span> <span style="color: #0000FF; ">int</span> dfs(<span style="color: #0000FF; ">int</span> u, <span style="color: #0000FF; ">int</span> cnt) {<br /><span style="color: #008080; ">28</span>     memset(visit, <span style="color: #0000FF; ">false</span>, <span style="color: #0000FF; ">sizeof</span>(visit));<br /><span style="color: #008080; ">29</span>     <span style="color: #0000FF; ">if</span>(u == n) <span style="color: #0000FF; ">return</span> <span style="color: #0000FF; ">true</span>;<br /><span style="color: #008080; ">30</span>     <span style="color: #0000FF; ">for</span>(<span style="color: #0000FF; ">int</span> i = 0; i < n; i++) {<br /><span style="color: #008080; ">31</span>         <span style="color: #0000FF; ">if</span>(color[i] != -1 && maz[u][i]) {<br /><span style="color: #008080; ">32</span>             visit[color[i]] = <span style="color: #0000FF; ">true</span>;<br /><span style="color: #008080; ">33</span>         }<br /><span style="color: #008080; ">34</span>     }<br /><span style="color: #008080; ">35</span>     <span style="color: #0000FF; ">for</span>(<span style="color: #0000FF; ">int</span> i = 0; i < cnt; i++) {<br /><span style="color: #008080; ">36</span>         <span style="color: #0000FF; ">if</span>(!visit[i]) {<br /><span style="color: #008080; ">37</span>             color[u] = i;<br /><span style="color: #008080; ">38</span>             <span style="color: #0000FF; ">if</span>(dfs(u+1, cnt)) <span style="color: #0000FF; ">return</span> <span style="color: #0000FF; ">true</span>;<br /><span style="color: #008080; ">39</span>         }<br /><span style="color: #008080; ">40</span>     }<br /><span style="color: #008080; ">41</span>     <span style="color: #0000FF; ">return</span> <span style="color: #0000FF; ">false</span>;<br /><span style="color: #008080; ">42</span> }<br /><span style="color: #008080; ">43</span> <br /><span style="color: #008080; ">44</span> <span style="color: #0000FF; ">void</span> solve() {<br /><span style="color: #008080; ">45</span>     <span style="color: #0000FF; ">int</span> ans = 5;<br /><span style="color: #008080; ">46</span>     <span style="color: #0000FF; ">for</span>(<span style="color: #0000FF; ">int</span> i = 1; i < 5; i++) {<br /><span style="color: #008080; ">47</span>         memset(color, -1, <span style="color: #0000FF; ">sizeof</span>(color));<br /><span style="color: #008080; ">48</span>         <span style="color: #0000FF; ">if</span>(dfs(0, i)) {<br /><span style="color: #008080; ">49</span>             ans = i;<br /><span style="color: #008080; ">50</span>             <span style="color: #0000FF; ">break</span>;<br /><span style="color: #008080; ">51</span>         }<br /><span style="color: #008080; ">52</span>     }<br /><span style="color: #008080; ">53</span>     <span style="color: #0000FF; ">if</span>(ans == 1) printf("1 channel needed.\n");<br /><span style="color: #008080; ">54</span>     <span style="color: #0000FF; ">else</span> printf("%d channels needed.\n", ans);<br /><span style="color: #008080; ">55</span> }<br /><span style="color: #008080; ">56</span> <br /><span style="color: #008080; ">57</span> <span style="color: #0000FF; ">int</span> main() {<br /><span style="color: #008080; ">58</span>     <span style="color: #0000FF; ">while</span>(~scanf("%d", &n), n) {<br /><span style="color: #008080; ">59</span>         getchar();<br /><span style="color: #008080; ">60</span>         memset(maz, <span style="color: #0000FF; ">false</span>, <span style="color: #0000FF; ">sizeof</span>(maz));<br /><span style="color: #008080; ">61</span>         <span style="color: #0000FF; ">for</span>(<span style="color: #0000FF; ">int</span> i = 0; i < n; i++) {<br /><span style="color: #008080; ">62</span>             scanf("%c:", &a);<br /><span style="color: #008080; ">63</span>             <span style="color: #0000FF; ">while</span>((ch = getchar()) != 10) {<br /><span style="color: #008080; ">64</span>                 maz[a-'A'][ch-'A'] = maz[ch-'A'][a-'A'] = <span style="color: #0000FF; ">true</span>;<br /><span style="color: #008080; ">65</span>             }<br /><span style="color: #008080; ">66</span>         }<br /><span style="color: #008080; ">67</span>         solve();<br /><span style="color: #008080; ">68</span>     }<br /><span style="color: #008080; ">69</span>     <span style="color: #0000FF; ">return</span> 0;<br /><span style="color: #008080; ">70</span> }<br /><span style="color: #008080; ">71</span> </div><img src ="http://www.shnenglu.com/y346491470/aggbug/169031.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.shnenglu.com/y346491470/" target="_blank">y @ The Angry Teletubbies</a> 2012-03-26 17:27 <a href="http://www.shnenglu.com/y346491470/articles/169031.html#Feedback" target="_blank" style="text-decoration:none;">鍙戣〃璇勮</a></div>]]></description></item><item><title>sgu 536 Berland Chess - 鐘舵佸帇緙?+ bfshttp://www.shnenglu.com/y346491470/articles/159820.htmly @ The Angry Teletubbiesy @ The Angry TeletubbiesTue, 08 Nov 2011 06:07:00 GMThttp://www.shnenglu.com/y346491470/articles/159820.htmlhttp://www.shnenglu.com/y346491470/comments/159820.htmlhttp://www.shnenglu.com/y346491470/articles/159820.html#Feedback0http://www.shnenglu.com/y346491470/comments/commentRss/159820.htmlhttp://www.shnenglu.com/y346491470/services/trackbacks/159820.html               * 鐧借壊鐨勭殗 鍙互鍚戜換鎰忓懆鍥村叓鏍肩Щ鍔ㄤ竴鏍?br />               R 榛戣壊鐨勮濺 鍙互妯潃鎴栫珫鐫縐誨姩浠繪剰鏍?br />               K 榛戣壊鐨勯┈  縐誨姩“鏃?#8221;瀛?br />               B 榛戣壊鐨勮薄  鍙互鏂滅潃璧頒換鎰忔牸
               宸茬煡媯嬬洏鏈澶氭湁15涓瀛愶紝鏈変笖鍙湁涓涓櫧鑹茬殑鐨囷紝 涓旈粦媯嬪湪鍚冧笉鍒扮殗鐨勬儏鍐典笅鏄笉浼氱Щ鍔ㄧ殑銆?br />               闂紝鍦ㄤ笉琚悆鐨勬儏鍐典笅錛岀殗鏈灝戠敤澶氬皯姝ュ彲浠ュ悆鍏夋墍鏈夌殑榛戞銆?br />
銆愰瑙c戯細鑰冭檻鍒版渶澶氬彧鏈?5涓瀛愶紝鍙互榪涜鐘舵佸帇緙╃殑bfs銆傝繖閲屾湁涓猼rick灝辨槸錛岃薄鍜岃濺鍙兘浼氳鍏朵粬媯嬪瓙鎸$潃鑰屽悆涓嶅埌鐨囥?br />
銆愪唬鐮併戯細
  1 #include "iostream"
  2 #include "cstdio"
  3 #include "cstring"
  4 #include "queue"
  5 using namespace std;
  6 int r, c, sx, sy;
  7 int cnt;
  8 char maz[20][20];
  9 int maze[20][20];
 10 bool visit[20][20][40000];
 11 int dir[][2= {0110-100-11-1-1111-1-1};
 12 int kdir[][2= {-2-1-21212-1121-2-12-1-2};
 13 int bdir[][2= {1-111-1-1-11};
 14 int rdir[][2= {0110-100-1};
 15 struct Node {
 16     int x, y, ti, mask;
 17     Node(){}
 18     Node(int _x, int _y, int _ti, int _mask) {
 19         x = _x, y = _y, ti = _ti, mask = _mask;
 20     }
 21 };
 22 
 23 struct Piece {
 24     int x, y, type;
 25     Piece(){}
 26     Piece(int _x, int _y, int _type) {
 27         x = _x, y = _y, type = _type;
 28     }
 29 }p[20];
 30 
 31 bool check(int x, int y) {
 32     if(x >= 1 && x <= r && y >= 1 && y <= c) return true;
 33     return false;
 34 }
 35 
 36 bool IsEat(int x, int y, int mask) {
 37     //Knight
 38     for(int i = 0; i < 8; i++) {
 39         int xx = x + kdir[i][0], yy = y + kdir[i][1];
 40         int tmp = maze[xx][yy];
 41         if(!check(xx, yy)) continue;
 42         if(tmp >= 0 && !(mask & (1 << tmp)) && p[tmp].type == 0return true;
 43     }
 44     //Bishop
 45     for(int i = 0; i < 4; i++) {
 46         int xx = x + bdir[i][0], yy = y + bdir[i][1];
 47         int tmp = maze[xx][yy];
 48         while(check(xx, yy)) {
 49             if(tmp >= 0 && !(mask & (1 << tmp))) {
 50                 if(p[tmp].type == 1return true;
 51                 else break;
 52             } else xx += bdir[i][0], yy += bdir[i][1], tmp = maze[xx][yy];
 53         }
 54     }
 55     //Rook
 56     for(int i = 0; i < 4; i++) {
 57         int xx = x + rdir[i][0], yy = y + rdir[i][1];
 58         int tmp = maze[xx][yy];
 59         while(check(xx, yy)) {
 60             if(tmp >= 0 && !(mask & (1 << tmp))) {
 61                 if(p[tmp].type == 2return true;
 62                 else break;
 63             } else xx += rdir[i][0], yy += rdir[i][1], tmp = maze[xx][yy];
 64         }
 65     }
 66     return false;
 67 }
 68 
 69 int bfs() {
 70     queue<Node> que;
 71     memset(visit, falsesizeof(visit));
 72     visit[sx][sy][0= true;
 73     que.push(Node(sx, sy, 00));
 74     while(!que.empty()) {
 75         Node now = que.front();
 76         que.pop();
 77         if(now.mask == (1 << cnt) - 1return now.ti;
 78         for(int i = 0; i < 8; i++) {
 79             int nx = now.x + dir[i][0], ny = now.y + dir[i][1], mask = now.mask;
 80             if(!check(nx, ny)) continue;
 81             int tmp = maze[nx][ny];
 82             if(tmp >= 0) mask |= (1 << tmp);
 83             if(visit[nx][ny][mask]) continue;
 84             if(IsEat(nx, ny, mask)) continue;
 85             que.push(Node(nx, ny, now.ti + 1, mask));
 86             visit[nx][ny][mask] = true;
 87         }
 88     }
 89     return -1;
 90 }
 91 
 92 int main() {
 93     while(~scanf("%d%d"&r, &c)) {
 94         cnt = 0;
 95         memset(maze, -1sizeof(maze));
 96         for(int i = 1; i <= r; i++) scanf("%s"&maz[i][1]);
 97         for(int i = 1; i <= r; i++) {
 98             for(int j = 1; j <= c; j++) {
 99                 if(maz[i][j] == '.'continue;
100                 if(maz[i][j] == '*') sx = i, sy = j;
101                 else if(maz[i][j] == 'K') {
102                     maze[i][j] = cnt, p[cnt++= Piece(i, j, 0);
103                 } else if(maz[i][j] == 'B') {
104                     maze[i][j] = cnt, p[cnt++= Piece(i, j, 1);
105                 } else maze[i][j] = cnt, p[cnt++= Piece(i, j, 2);
106             }
107         }
108         int ans = bfs();
109         cout << ans << endl;
110     }
111     return 0;
112 }




y @ The Angry Teletubbies 2011-11-08 14:07 鍙戣〃璇勮
]]>
SWERC 2010 F Jumping monkey - 鐘舵佸帇緙?+ bfshttp://www.shnenglu.com/y346491470/articles/159298.htmly @ The Angry Teletubbiesy @ The Angry TeletubbiesFri, 28 Oct 2011 17:17:00 GMThttp://www.shnenglu.com/y346491470/articles/159298.htmlhttp://www.shnenglu.com/y346491470/comments/159298.htmlhttp://www.shnenglu.com/y346491470/articles/159298.html#Feedback0http://www.shnenglu.com/y346491470/comments/commentRss/159298.htmlhttp://www.shnenglu.com/y346491470/services/trackbacks/159298.html
銆愰瑙c戯細闈炲父濂界殑鎼滅儲棰橈紒鍒氬紑濮嬩互涓烘槸涓崥寮堢殑涓滆タ錛屽悗鏉ヤ竴鐩存帹涓嶅埌鏈浼樺喅絳栵紝鎰忚瘑鍒版暟鎹湁榪欎箞灝戯紝搴旇鍙互鎼滅儲榪囧幓銆?br />              鍥犱負n灝忎簬絳変簬21錛屽彲浠ラ噰鐢ㄧ姸鎬佸帇緙┿傚浜庣尨瀛愬彲鑳藉湪鐨勬煇浜涙5鏍戞爣璁頒負1錛屽惁鍒欐爣璁頒負0錛屾渶緇堢姸鎬佷負鍏?錛屽嵆0.
              鐚村瓙浠庡綋鍓嶇殑鏍戣煩鍒扮浉閭葷殑鏍戣繖涓鍏堥澶勭悊錛屼互鍚庢瘡嬈″彲浠ョ敤O(n)鐨勬椂闂村鏉傚害杞Щ鐘舵併?br />              涓嶅彲鑳藉瓨鍦ㄧ尨瀛愮殑鏍戞槸涓嶇敤綆$殑錛岃繖閲屽彲浠ュ壀鏋濓紱濡傛灉m > n - 1鍗沖叧緋諱腑瀛樺湪鐜垯涓瀹氭棤瑙c?br />
銆愪唬鐮併戯細
 1 #include "iostream"
 2 #include "cstdio"
 3 #include "cstring"
 4 #include "queue"
 5 #include "string"
 6 using namespace std;
 7 int n, m;
 8 bool visit[1<<22];
 9 int edge[22];
10 string ans;
11 struct Node {
12     int mask;
13     string path;
14     Node(){}
15     Node(int _mask, string _path) {
16         mask = _mask, path = _path;
17     }
18 };
19 
20 bool bfs() {
21     queue<Node> que;
22     memset(visit, falsesizeof(visit));
23     que.push(Node((1 << n) - 1""));
24     visit[(1<<n)-1= true;
25     while(!que.empty()) {
26         Node now = que.front();
27         que.pop();
28         for(int i = 0; i < n; i++) {
29             if(now.mask & (1 << i)) {
30                 int mask = now.mask ^ (1 << i);
31                 int newmask = 0;
32                 for(int j = 0; j < n; j++)
33                     if(mask & (1 << j)) newmask |= edge[j];
34                 if(visit[newmask]) continue;
35                 visit[newmask] = true;
36                 char ch = i + '0';
37                 string tmp = now.path + ch;
38                 if(newmask == 0) {
39                     ans = tmp;
40                     return true;
41                 }
42                 que.push(Node(newmask, tmp));
43             }
44         }
45     }
46     return false;
47 }
48 
49 void solve() {
50     if((m > n - 1|| !bfs()) printf("Impossible\n");
51     else {
52         printf("%d:", ans.length());
53         for(int i = 0; i < ans.length(); i++) printf(" %d", ans[i] - '0');
54         printf("\n");
55     }
56     return;
57 }
58 
59 int main() {
60     while(~scanf("%d%d"&n, &m)) {
61         if(!&& !m) break;
62         memset(edge, 0sizeof(edge));
63         for(int i = 0; i < m; i++) {
64             int u, v;
65             scanf("%d%d"&u, &v);
66             edge[u] |= (1 << v), edge[v] |= (1 << u);
67         }
68         solve();
69     }
70     return 0;
71 }



y @ The Angry Teletubbies 2011-10-29 01:17 鍙戣〃璇勮
]]>
hdoj 1043 Eight - IDA_starhttp://www.shnenglu.com/y346491470/articles/156092.htmly @ The Angry Teletubbiesy @ The Angry TeletubbiesSun, 18 Sep 2011 02:18:00 GMThttp://www.shnenglu.com/y346491470/articles/156092.htmlhttp://www.shnenglu.com/y346491470/comments/156092.htmlhttp://www.shnenglu.com/y346491470/articles/156092.html#Feedback0http://www.shnenglu.com/y346491470/comments/commentRss/156092.htmlhttp://www.shnenglu.com/y346491470/services/trackbacks/156092.html銆愰鎰忋戯細鍏暟鐮侀棶棰?br />
銆愰瑙c戯細IDA*錛岀敤姣忎釜鏁板瓧鍒扮洰鏍囦綅緗殑鍝堟浖欏胯礬闀挎潵鍋氫及浠峰嚱鏁幫紝濂囧伓鍒ゆ柇鏈夋病鏈夊彲琛岃В銆?br />
銆愪唬鐮併戯細
 1 #include "iostream"
 2 #include "cstdio"
 3 #include "cstring"
 4 #include "cstdlib"
 5 #include "queue"
 6 using namespace std;
 7 #define SIZE 3
 8 const int dx[] = {-1001};
 9 const int dy[] = {0-110};
10 char op[] = {'u''l''r''d'};
11 int goal_state[10][2= {{0,0}, {1,1}, {1,2}, {1,3}, {2,1}, {2,2}, {2,3}, {3,1}, {3,2}, {3,3}};
12 int bound;
13 char a[10];
14 bool flag;
15 char solution[1000];
16 
17 struct Eight {
18     char board[4][4];
19     int x, y;
20 };
21 
22 int h(char board[][4]) {
23     int cost = 0;
24     for(int i = 1; i <= SIZE; i++)
25         for(int j = 1; j <= SIZE; j++)
26             if(board[i][j]) cost += abs(i - goal_state[board[i][j]][0]) + abs(j - goal_state[board[i][j]][1]);
27     return cost;
28 }
29 
30 bool solvable() {
31     int cnt = 0;
32     for(int i = 1; i <= 9; i++)
33         for(int j = 1; j < i; j++)
34             if(a[j] < a[i] && a[j]) cnt++;
35     if(cnt & 1return false;
36     return true;
37 }
38 
39 bool check(int x, int y) {
40     if(x >= 1 && x <= SIZE && y >= 1 && y <= SIZE) return true;
41     return false;
42 }
43 
44 bool dfs(Eight now, int dv, char pre) {
45     if(h(now.board) == 0) {
46         solution[dv] = 0;
47         return true;
48     }
49     for(int i = 0; i < 4; i++) {
50         if(i + pre == 3continue;//涓庝笂涓姝ョ浉鍙嶇殑縐誨姩
51         Eight next = now;
52         next.x += dx[i], next.y += dy[i];
53         if(check(next.x, next.y)) {
54             solution[dv] = op[i];
55             swap(next.board[next.x][next.y], next.board[now.x][now.y]);
56             if(h(next.board) + dv >= bound) continue;
57             if(dfs(next, dv + 1, i)) return true;
58         }
59     }
60     return false;
61 }
62         
63 void IDA_star(Eight st) {
64     bound = h(st.board);
65     for(bound = h(st.board); !dfs(st, 0-10); bound++);
66     puts(solution);
67 }
68 
69 int main() {
70     int sx, sy;
71     char c[2];
72     while(1) {
73         Eight st;
74         for(int i = 1; i <= SIZE; i++) {
75             for(int j = 1; j <= SIZE; j++) {
76                 if(scanf("%s", c) == EOF) return 0;
77                 if(c[0== 'x') {
78                     st.board[i][j] = 0;
79                     st.x = i, st.y = j;
80                 } else st.board[i][j] = c[0- '0';
81                 a[(i - 1* SIZE + j] = st.board[i][j];
82             }
83         }
84         if(solvable()) IDA_star(st);
85         else printf("unsolvable\n");
86     }
87     return 0;
88 }
89 




y @ The Angry Teletubbies 2011-09-18 10:18 鍙戣〃璇勮
]]>
poj 1011/uva 307 Sticks - dfs鍥炴函 + 鍓灊http://www.shnenglu.com/y346491470/articles/155318.htmly @ The Angry Teletubbiesy @ The Angry TeletubbiesWed, 07 Sep 2011 17:18:00 GMThttp://www.shnenglu.com/y346491470/articles/155318.htmlhttp://www.shnenglu.com/y346491470/comments/155318.htmlhttp://www.shnenglu.com/y346491470/articles/155318.html#Feedback4http://www.shnenglu.com/y346491470/comments/commentRss/155318.htmlhttp://www.shnenglu.com/y346491470/services/trackbacks/155318.html
銆愰殢絎斻戯細榪欓亾棰樺緢鏃╀箣鍓嶅氨榪囦簡錛岃繖鍑犲ぉ鐪嬪洖涔嬪墠鐨勪唬鐮侊紝瑙夊緱浠ュ墠鐨勪唬鐮佸啓寰楀お鐑備簡錛岃屼笖閭d釜浠g爜椋庢牸涔熶笉濂界湅錛屼簬鏄喅瀹氬啀閲嶅啓涓杈癸紝欏轟究鎶妘va鐨勪篃榪囦簡銆傝繖嬈¢噸鍐欐瘮涔嬪墠鐨勪唬鐮佸姞澶氫簡涓涓噸瑕佺殑鍓灊錛屾渶鍚庡湪uva涓婁互0.2s榪囦簡銆?br />
銆愰瑙c戯細涓嶅緱涓嶈錛岃繖閬撻鍑哄緱闈炲父濂斤紝鐗瑰埆鏄痷va閭i噷鐨勫ぇ鏁版嵁錛坧oj鐨勬暟鎹お姘翠簡錛夛紝瀵逛簬鍓灊鑳藉姏瑕佹眰寰堥珮銆備笅闈㈣涓嬪嚑涓噸瑕佺殑鍓灊錛?br />1.鎶婃墍鏈夋湪媯嶇殑闀垮害浠庡ぇ鍒板皬鎺掑垪錛岀粍鍚堟湪媯掓椂浼樺厛浣跨敤闀跨殑鏈ㄦ錛岃繖鏍峰彲浠ュ姞蹇粍鍚堥熷害錛屽茍涓斿鍚庨潰鐨勫壀鏋濇湁甯姪銆?br />2.鏈ㄦ鐨勯暱搴︿竴瀹氭槸澶т簬絳変簬鏈闀挎湪媯嶇殑闀垮害騫朵笖灝忎簬絳変簬鎵鏈夋湪媯嶉暱搴︾殑鍜岋紝榪欎釜寰堝鏄撹瘉鏄庛?br />3.鏈ㄦ鐨勯暱搴︿竴瀹氭槸鎵鏈夋湪媯嶉暱搴︾殑鍜岀殑綰︽暟錛岃繖涓篃寰堝鏄撹瘉鏄庛?br />4.鍦ㄦ煇涓涓湪媯掔殑緇勫悎榪囩▼涓紝瀵逛簬褰撳墠鐨勬湪媯峴tick[i]錛屽鏋渟tick[i-1]娌℃湁琚粍鍚堝茍涓攕tick[i] == stick[i-1]錛岄偅涔堜笉鐢ㄨ冭檻stick[i]錛屾樉鐒秙tick[i]鏈緇堜篃涓嶄細琚粍鍚堛?br />5.濡傛灉姝ゆ鏄湪灝濊瘯絎琲涓湪媯掔殑絎竴孌碉紝鍋囪stick[j]涓哄綋鍓嶅彲浠ヨ浣跨敤鐨勬渶闀跨殑鏈ㄦ錛屽鏋滄嬈$粍鍚堝け璐ワ紝鐩存帴閫鍑烘悳绱紝鍗抽鍥炲埌瀵圭i-1涓湪媯掔殑鎼滅儲銆傝瘯鎯籌細澶辮觸璇存槑鐜板湪浣跨敤stick[j]鏄笉鍙鐨勶紝閭d箞浠ュ悗鏃犺浠涔堟椂鍊欎嬌鐢╯tick[j]閮芥槸涓嶅彲琛岀殑錛屽洜涓轟互鍚庡啀澶勭悊stick[j]鏃跺彲浣跨敤鐨勬湪媯嶄竴瀹氭槸褰撳墠鍙嬌鐢ㄧ殑鏈ㄦ鐨勫瓙闆嗭紝鍦ㄦ洿澶氭湪媯嶉夋嫨鐨勬儏鍐典笅閮戒笉鑳界粍鍚堟垚鍔燂紝閭d箞錛屽湪鏇村皯鏈ㄦ閫夋嫨鐨勬儏鍐典笅涓瀹氫笉鑳界粍鍚堟垚鍔熴?br />
銆愪唬鐮併戯細
 1 #include "iostream"
 2 #include "cstdio"
 3 #include "cstring"
 4 #include "algorithm"
 5 #include "functional"
 6 using namespace std;
 7 #define maxn 65
 8 int n, sum, goal;
 9 int stick[maxn];
10 bool visit[maxn];
11 
12 bool cmp(const int &a, const int &b) {
13     return a > b;
14 }
15 
16 bool dfs(int now, int index, int cnt) {
17     if(goal * cnt == sum) return true;
18     for(int i = index; i < n; i++) {
19         if(visit[i] || (i && !visit[i-1&& stick[i] == stick[i-1])) continue;
20         if(now + stick[i] == goal) {
21             visit[i] = true;
22             if(dfs(00, cnt + 1)) return true;
23             visit[i] = false;
24             return false;
25         } else if(now + stick[i] < goal) {
26             visit[i] = true;
27             if(dfs(now + stick[i], i + 1, cnt)) return true;
28             visit[i] = false;
29             if(now == 0return false;
30         }
31     }
32     return false;
33 }
34 
35 int solve() {
36     sort(stick, stick + n, cmp);
37     for(goal = stick[0]; goal < sum; goal++) {
38         if(sum % goal != 0continue;
39         memset(visit, falsesizeof(visit));
40         if(dfs(000)) break;
41     }
42     return goal;
43 }
44 
45 int main() {
46     while(~scanf("%d"&n)) {
47         if(!n) break;
48         sum = 0;
49         for(int i = 0; i < n; i++) {
50             scanf("%d"&stick[i]);
51             sum += stick[i];
52         }
53         printf("%d\n", solve());
54     }
55     return 0;
56 }



y @ The Angry Teletubbies 2011-09-08 01:18 鍙戣〃璇勮
]]>
poj 3593 Sea Base Exploration - 浼樺厛闃熷垪 + bfs + 鐘舵佸帇緙?/title><link>http://www.shnenglu.com/y346491470/articles/154450.html</link><dc:creator>y @ The Angry Teletubbies</dc:creator><author>y @ The Angry Teletubbies</author><pubDate>Fri, 26 Aug 2011 12:13:00 GMT</pubDate><guid>http://www.shnenglu.com/y346491470/articles/154450.html</guid><wfw:comment>http://www.shnenglu.com/y346491470/comments/154450.html</wfw:comment><comments>http://www.shnenglu.com/y346491470/articles/154450.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.shnenglu.com/y346491470/comments/commentRss/154450.html</wfw:commentRss><trackback:ping>http://www.shnenglu.com/y346491470/services/trackbacks/154450.html</trackback:ping><description><![CDATA[銆愰鎰忋戯細鍦板浘涓湁K縐嶇墿鍝? 娌℃湁鏀墮泦璧勬簮鏃訛紝姣忚蛋涓姝ョ敤鑳介噺1錛屾寲鎺樺拰鏀墮泦璧勬簮 i 闇瑕佽兘閲廇i 錛屾敹闆嗕簡璧勬簮 i 鍚? 姣忚蛋涓姝ワ紝娑堣楄兘閲忛渶瑕佸姞Bi銆傞棶鏈鍚庢敹闆嗗畬K縐嶇墿鍝佸茍涓斿洖鍒拌搗鐐圭殑鏈灝忔秷鑰椼?br /><br />銆愰瑙c戯細閲囩敤浜嗕紭鍏堥槦鍒?鐘舵佸帇緙゜fs錛岃繖閲岃棰勫鐞嗗ソ鎼哄甫浜嗙姸鎬乻鐨勪唬浠穋ost[s]錛屼箣鍚庣殑bfs璇ユ庝箞鍋氬氨鎬庝箞鍋氥傝繖閲岀敤浼樺厛闃熷垪鏈変釜濂藉錛岃繖鏍峰氨淇濊瘉姣忔鍑洪槦鐨勯兘鏄渶浼樼殑鐐癸紝鐩告瘮鐢ㄤ竴涓暟緇勬潵璁板綍錛屾洿鐪佺┖闂達紝騫朵笖浠g爜閲忎篃灝戜簡銆?br /><br />銆愪唬鐮併戯細 <div style="border-bottom: #cccccc 1px solid; border-left: #cccccc 1px solid; padding-bottom: 4px; background-color: #eeeeee; padding-left: 4px; width: 98%; padding-right: 5px; font-size: 13px; word-break: break-all; border-top: #cccccc 1px solid; border-right: #cccccc 1px solid; padding-top: 4px"><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><span style="color: #008080"> 1</span> <span style="color: #000000">#include </span><span style="color: #000000">"</span><span style="color: #000000">iostream</span><span style="color: #000000">"</span><span style="color: #000000"><br /></span><span style="color: #008080"> 2</span> <span style="color: #000000">#include </span><span style="color: #000000">"</span><span style="color: #000000">cstdio</span><span style="color: #000000">"</span><span style="color: #000000"><br /></span><span style="color: #008080"> 3</span> <span style="color: #000000">#include </span><span style="color: #000000">"</span><span style="color: #000000">cstring</span><span style="color: #000000">"</span><span style="color: #000000"><br /></span><span style="color: #008080"> 4</span> <span style="color: #000000">#include </span><span style="color: #000000">"</span><span style="color: #000000">queue</span><span style="color: #000000">"</span><span style="color: #000000"><br /></span><span style="color: #008080"> 5</span> <span style="color: #000000"></span><span style="color: #0000ff">using</span><span style="color: #000000"> </span><span style="color: #0000ff">namespace</span><span style="color: #000000"> std;<br /></span><span style="color: #008080"> 6</span> <span style="color: #000000"></span><span style="color: #0000ff">char</span><span style="color: #000000"> maz[</span><span style="color: #000000">25</span><span style="color: #000000">][</span><span style="color: #000000">25</span><span style="color: #000000">];<br /></span><span style="color: #008080"> 7</span> <span style="color: #000000"></span><span style="color: #0000ff">int</span><span style="color: #000000"> k, p, r, c;<br /></span><span style="color: #008080"> 8</span> <span style="color: #000000"></span><span style="color: #0000ff">int</span><span style="color: #000000"> a[</span><span style="color: #000000">15</span><span style="color: #000000">], b[</span><span style="color: #000000">15</span><span style="color: #000000">], cost[</span><span style="color: #000000">1</span><span style="color: #000000"><<</span><span style="color: #000000">11</span><span style="color: #000000">];<br /></span><span style="color: #008080"> 9</span> <span style="color: #000000"></span><span style="color: #0000ff">const</span><span style="color: #000000"> </span><span style="color: #0000ff">int</span><span style="color: #000000"> dx[] </span><span style="color: #000000">=</span><span style="color: #000000"> {</span><span style="color: #000000">0</span><span style="color: #000000">, </span><span style="color: #000000">1</span><span style="color: #000000">, </span><span style="color: #000000">0</span><span style="color: #000000">, </span><span style="color: #000000">-</span><span style="color: #000000">1</span><span style="color: #000000">};<br /></span><span style="color: #008080">10</span> <span style="color: #000000"></span><span style="color: #0000ff">const</span><span style="color: #000000"> </span><span style="color: #0000ff">int</span><span style="color: #000000"> dy[] </span><span style="color: #000000">=</span><span style="color: #000000"> {</span><span style="color: #000000">-</span><span style="color: #000000">1</span><span style="color: #000000">, </span><span style="color: #000000">0</span><span style="color: #000000">, </span><span style="color: #000000">1</span><span style="color: #000000">, </span><span style="color: #000000">0</span><span style="color: #000000">};<br /></span><span style="color: #008080">11</span> <span style="color: #000000"></span><span style="color: #0000ff">int</span><span style="color: #000000"> sx, sy;<br /></span><span style="color: #008080">12</span> <span style="color: #000000"></span><span style="color: #0000ff">bool</span><span style="color: #000000"> visit[</span><span style="color: #000000">25</span><span style="color: #000000">][</span><span style="color: #000000">25</span><span style="color: #000000">][</span><span style="color: #000000">1</span><span style="color: #000000"><<</span><span style="color: #000000">11</span><span style="color: #000000">];<br /></span><span style="color: #008080">13</span> <span style="color: #000000"><br /></span><span style="color: #008080">14</span> <span style="color: #000000"></span><span style="color: #0000ff">struct</span><span style="color: #000000"> pos {<br /></span><span style="color: #008080">15</span> <span style="color: #000000">    </span><span style="color: #0000ff">int</span><span style="color: #000000"> x, y, cost, collect;<br /></span><span style="color: #008080">16</span> <span style="color: #000000">    pos() {}</span><span style="color: #008000">//</span><span style="color: #008000">鏋勯犲嚱鏁?/span><span style="color: #008000"><br /></span><span style="color: #008080">17</span> <span style="color: #008000"></span><span style="color: #000000">    pos(</span><span style="color: #0000ff">int</span><span style="color: #000000"> a, </span><span style="color: #0000ff">int</span><span style="color: #000000"> b, </span><span style="color: #0000ff">int</span><span style="color: #000000"> c, </span><span style="color: #0000ff">int</span><span style="color: #000000"> d) {<br /></span><span style="color: #008080">18</span> <span style="color: #000000">        x </span><span style="color: #000000">=</span><span style="color: #000000"> a, y </span><span style="color: #000000">=</span><span style="color: #000000"> b, cost </span><span style="color: #000000">=</span><span style="color: #000000"> c, collect </span><span style="color: #000000">=</span><span style="color: #000000"> d;<br /></span><span style="color: #008080">19</span> <span style="color: #000000">    }<br /></span><span style="color: #008080">20</span> <span style="color: #000000">    </span><span style="color: #0000ff">bool</span><span style="color: #000000"> </span><span style="color: #0000ff">operator</span><span style="color: #000000"><</span><span style="color: #000000"> (</span><span style="color: #0000ff">const</span><span style="color: #000000"> pos </span><span style="color: #000000">&</span><span style="color: #000000">a) </span><span style="color: #0000ff">const</span><span style="color: #000000"> {<br /></span><span style="color: #008080">21</span> <span style="color: #000000">        </span><span style="color: #0000ff">return</span><span style="color: #000000"> cost </span><span style="color: #000000">></span><span style="color: #000000"> a.cost;<br /></span><span style="color: #008080">22</span> <span style="color: #000000">    }<br /></span><span style="color: #008080">23</span> <span style="color: #000000">};<br /></span><span style="color: #008080">24</span> <span style="color: #000000"><br /></span><span style="color: #008080">25</span> <span style="color: #000000"></span><span style="color: #0000ff">bool</span><span style="color: #000000"> check(</span><span style="color: #0000ff">int</span><span style="color: #000000"> x, </span><span style="color: #0000ff">int</span><span style="color: #000000"> y) {<br /></span><span style="color: #008080">26</span> <span style="color: #000000">    </span><span style="color: #0000ff">return</span><span style="color: #000000"> x </span><span style="color: #000000">>=</span><span style="color: #000000"> </span><span style="color: #000000">0</span><span style="color: #000000"> </span><span style="color: #000000">&&</span><span style="color: #000000"> x </span><span style="color: #000000"><</span><span style="color: #000000"> r </span><span style="color: #000000">&&</span><span style="color: #000000"> y </span><span style="color: #000000">>=</span><span style="color: #000000"> </span><span style="color: #000000">0</span><span style="color: #000000"> </span><span style="color: #000000">&&</span><span style="color: #000000"> y </span><span style="color: #000000"><</span><span style="color: #000000"> c;<br /></span><span style="color: #008080">27</span> <span style="color: #000000">}<br /></span><span style="color: #008080">28</span> <span style="color: #000000"><br /></span><span style="color: #008080">29</span> <span style="color: #000000"></span><span style="color: #0000ff">int</span><span style="color: #000000"> bfs() {<br /></span><span style="color: #008080">30</span> <span style="color: #000000">    priority_queue</span><span style="color: #000000"><</span><span style="color: #000000">pos</span><span style="color: #000000">></span><span style="color: #000000"> que;<br /></span><span style="color: #008080">31</span> <span style="color: #000000">    memset(visit, </span><span style="color: #0000ff">false</span><span style="color: #000000">, </span><span style="color: #0000ff">sizeof</span><span style="color: #000000">(visit));<br /></span><span style="color: #008080">32</span> <span style="color: #000000">    que.push(pos(sx, sy, </span><span style="color: #000000">0</span><span style="color: #000000">, </span><span style="color: #000000">0</span><span style="color: #000000">));<br /></span><span style="color: #008080">33</span> <span style="color: #000000">    visit[sx][sy][</span><span style="color: #000000">0</span><span style="color: #000000">] </span><span style="color: #000000">=</span><span style="color: #000000"> </span><span style="color: #0000ff">true</span><span style="color: #000000">;<br /></span><span style="color: #008080">34</span> <span style="color: #000000">    </span><span style="color: #0000ff">while</span><span style="color: #000000">(</span><span style="color: #000000">!</span><span style="color: #000000">que.empty()) {<br /></span><span style="color: #008080">35</span> <span style="color: #000000">        pos now </span><span style="color: #000000">=</span><span style="color: #000000"> que.top();<br /></span><span style="color: #008080">36</span> <span style="color: #000000">        que.pop();<br /></span><span style="color: #008080">37</span> <span style="color: #000000">        </span><span style="color: #0000ff">int</span><span style="color: #000000"> x </span><span style="color: #000000">=</span><span style="color: #000000"> now.x, y </span><span style="color: #000000">=</span><span style="color: #000000"> now.y, collect </span><span style="color: #000000">=</span><span style="color: #000000"> now.collect, ncost </span><span style="color: #000000">=</span><span style="color: #000000"> now.cost </span><span style="color: #000000">+</span><span style="color: #000000"> cost[collect] </span><span style="color: #000000">+</span><span style="color: #000000"> </span><span style="color: #000000">1</span><span style="color: #000000">;<br /></span><span style="color: #008080">38</span> <span style="color: #000000">        </span><span style="color: #0000ff">if</span><span style="color: #000000">(ncost </span><span style="color: #000000">></span><span style="color: #000000"> p) </span><span style="color: #0000ff">continue</span><span style="color: #000000">;<br /></span><span style="color: #008080">39</span> <span style="color: #000000">        </span><span style="color: #0000ff">for</span><span style="color: #000000">(</span><span style="color: #0000ff">int</span><span style="color: #000000"> i </span><span style="color: #000000">=</span><span style="color: #000000"> </span><span style="color: #000000">0</span><span style="color: #000000">; i </span><span style="color: #000000"><</span><span style="color: #000000"> </span><span style="color: #000000">4</span><span style="color: #000000">; i</span><span style="color: #000000">++</span><span style="color: #000000">) {<br /></span><span style="color: #008080">40</span> <span style="color: #000000">            </span><span style="color: #0000ff">int</span><span style="color: #000000"> nx </span><span style="color: #000000">=</span><span style="color: #000000"> x </span><span style="color: #000000">+</span><span style="color: #000000"> dx[i], ny </span><span style="color: #000000">=</span><span style="color: #000000"> y </span><span style="color: #000000">+</span><span style="color: #000000"> dy[i];<br /></span><span style="color: #008080">41</span> <span style="color: #000000">            </span><span style="color: #0000ff">if</span><span style="color: #000000">(</span><span style="color: #000000">!</span><span style="color: #000000">check(nx, ny) </span><span style="color: #000000">||</span><span style="color: #000000"> maz[nx][ny] </span><span style="color: #000000">==</span><span style="color: #000000"> </span><span style="color: #000000">'</span><span style="color: #000000">#</span><span style="color: #000000">'</span><span style="color: #000000"> </span><span style="color: #000000">||</span><span style="color: #000000"> visit[nx][ny][collect]) </span><span style="color: #0000ff">continue</span><span style="color: #000000">;<br /></span><span style="color: #008080">42</span> <span style="color: #000000">            </span><span style="color: #0000ff">if</span><span style="color: #000000">(nx </span><span style="color: #000000">==</span><span style="color: #000000"> sx </span><span style="color: #000000">&&</span><span style="color: #000000"> ny </span><span style="color: #000000">==</span><span style="color: #000000"> sy) {<br /></span><span style="color: #008080">43</span> <span style="color: #000000">                </span><span style="color: #0000ff">if</span><span style="color: #000000">(collect </span><span style="color: #000000">==</span><span style="color: #000000"> (</span><span style="color: #000000">1</span><span style="color: #000000"> </span><span style="color: #000000"><<</span><span style="color: #000000"> k) </span><span style="color: #000000">-</span><span style="color: #000000"> </span><span style="color: #000000">1</span><span style="color: #000000">) </span><span style="color: #0000ff">return</span><span style="color: #000000"> ncost;<br /></span><span style="color: #008080">44</span> <span style="color: #000000">                </span><span style="color: #0000ff">continue</span><span style="color: #000000">;<br /></span><span style="color: #008080">45</span> <span style="color: #000000">            }<br /></span><span style="color: #008080">46</span> <span style="color: #000000">            </span><span style="color: #0000ff">if</span><span style="color: #000000">(maz[nx][ny] </span><span style="color: #000000"><=</span><span style="color: #000000"> </span><span style="color: #000000">'</span><span style="color: #000000">Z</span><span style="color: #000000">'</span><span style="color: #000000"> </span><span style="color: #000000">&&</span><span style="color: #000000"> maz[nx][ny] </span><span style="color: #000000">>=</span><span style="color: #000000"> </span><span style="color: #000000">'</span><span style="color: #000000">A</span><span style="color: #000000">'</span><span style="color: #000000">) {<br /></span><span style="color: #008080">47</span> <span style="color: #000000">                </span><span style="color: #0000ff">int</span><span style="color: #000000"> t </span><span style="color: #000000">=</span><span style="color: #000000"> maz[nx][ny] </span><span style="color: #000000">-</span><span style="color: #000000"> </span><span style="color: #000000">'</span><span style="color: #000000">A</span><span style="color: #000000">'</span><span style="color: #000000">;<br /></span><span style="color: #008080">48</span> <span style="color: #000000">                </span><span style="color: #0000ff">if</span><span style="color: #000000">((collect </span><span style="color: #000000">&</span><span style="color: #000000"> (</span><span style="color: #000000">1</span><span style="color: #000000"> </span><span style="color: #000000"><<</span><span style="color: #000000"> t)) </span><span style="color: #000000">==</span><span style="color: #000000"> </span><span style="color: #000000">0</span><span style="color: #000000">) {<br /></span><span style="color: #008080">49</span> <span style="color: #000000">                    que.push(pos(nx, ny, ncost </span><span style="color: #000000">+</span><span style="color: #000000"> a[t], collect </span><span style="color: #000000">|</span><span style="color: #000000"> (</span><span style="color: #000000">1</span><span style="color: #000000"><<</span><span style="color: #000000">t)));<br /></span><span style="color: #008080">50</span> <span style="color: #000000">                    visit[nx][ny][collect</span><span style="color: #000000">|</span><span style="color: #000000">(</span><span style="color: #000000">1</span><span style="color: #000000"><<</span><span style="color: #000000">t)] </span><span style="color: #000000">=</span><span style="color: #000000"> </span><span style="color: #0000ff">true</span><span style="color: #000000">;<br /></span><span style="color: #008080">51</span> <span style="color: #000000">                }<br /></span><span style="color: #008080">52</span> <span style="color: #000000">            }<br /></span><span style="color: #008080">53</span> <span style="color: #000000">            que.push(pos(nx, ny, ncost, collect));<br /></span><span style="color: #008080">54</span> <span style="color: #000000">            visit[nx][ny][collect] </span><span style="color: #000000">=</span><span style="color: #000000"> </span><span style="color: #0000ff">true</span><span style="color: #000000">;<br /></span><span style="color: #008080">55</span> <span style="color: #000000">        }<br /></span><span style="color: #008080">56</span> <span style="color: #000000">    }<br /></span><span style="color: #008080">57</span> <span style="color: #000000">    </span><span style="color: #0000ff">return</span><span style="color: #000000"> </span><span style="color: #000000">-</span><span style="color: #000000">1</span><span style="color: #000000">;<br /></span><span style="color: #008080">58</span> <span style="color: #000000">}<br /></span><span style="color: #008080">59</span> <span style="color: #000000"><br /></span><span style="color: #008080">60</span> <span style="color: #000000"></span><span style="color: #0000ff">int</span><span style="color: #000000"> main() {<br /></span><span style="color: #008080">61</span> <span style="color: #000000">    </span><span style="color: #0000ff">int</span><span style="color: #000000"> T;<br /></span><span style="color: #008080">62</span> <span style="color: #000000">    scanf(</span><span style="color: #000000">"</span><span style="color: #000000">%d</span><span style="color: #000000">"</span><span style="color: #000000">, </span><span style="color: #000000">&</span><span style="color: #000000">T);<br /></span><span style="color: #008080">63</span> <span style="color: #000000">    </span><span style="color: #0000ff">while</span><span style="color: #000000">(T</span><span style="color: #000000">--</span><span style="color: #000000">) {<br /></span><span style="color: #008080">64</span> <span style="color: #000000">        scanf(</span><span style="color: #000000">"</span><span style="color: #000000">%d%d%d%d\n</span><span style="color: #000000">"</span><span style="color: #000000">, </span><span style="color: #000000">&</span><span style="color: #000000">r, </span><span style="color: #000000">&</span><span style="color: #000000">c, </span><span style="color: #000000">&</span><span style="color: #000000">k, </span><span style="color: #000000">&</span><span style="color: #000000">p);<br /></span><span style="color: #008080">65</span> <span style="color: #000000">        </span><span style="color: #0000ff">for</span><span style="color: #000000">(</span><span style="color: #0000ff">int</span><span style="color: #000000"> i </span><span style="color: #000000">=</span><span style="color: #000000"> </span><span style="color: #000000">0</span><span style="color: #000000">; i </span><span style="color: #000000"><</span><span style="color: #000000"> r; i</span><span style="color: #000000">++</span><span style="color: #000000">) {<br /></span><span style="color: #008080">66</span> <span style="color: #000000">            </span><span style="color: #0000ff">for</span><span style="color: #000000">(</span><span style="color: #0000ff">int</span><span style="color: #000000"> j </span><span style="color: #000000">=</span><span style="color: #000000"> </span><span style="color: #000000">0</span><span style="color: #000000">; j </span><span style="color: #000000"><=</span><span style="color: #000000"> c; j</span><span style="color: #000000">++</span><span style="color: #000000">) {<br /></span><span style="color: #008080">67</span> <span style="color: #000000">                scanf(</span><span style="color: #000000">"</span><span style="color: #000000">%c</span><span style="color: #000000">"</span><span style="color: #000000">, </span><span style="color: #000000">&</span><span style="color: #000000">maz[i][j]);<br /></span><span style="color: #008080">68</span> <span style="color: #000000">                </span><span style="color: #0000ff">if</span><span style="color: #000000">(maz[i][j] </span><span style="color: #000000">==</span><span style="color: #000000"> </span><span style="color: #000000">'</span><span style="color: #000000">*</span><span style="color: #000000">'</span><span style="color: #000000">) sx </span><span style="color: #000000">=</span><span style="color: #000000"> i, sy </span><span style="color: #000000">=</span><span style="color: #000000"> j;<br /></span><span style="color: #008080">69</span> <span style="color: #000000">            }<br /></span><span style="color: #008080">70</span> <span style="color: #000000">        }<br /></span><span style="color: #008080">71</span> <span style="color: #000000">        </span><span style="color: #0000ff">for</span><span style="color: #000000">(</span><span style="color: #0000ff">int</span><span style="color: #000000"> i </span><span style="color: #000000">=</span><span style="color: #000000"> </span><span style="color: #000000">0</span><span style="color: #000000">; i </span><span style="color: #000000"><</span><span style="color: #000000"> k; i</span><span style="color: #000000">++</span><span style="color: #000000">) scanf(</span><span style="color: #000000">"</span><span style="color: #000000">%d%d</span><span style="color: #000000">"</span><span style="color: #000000">, </span><span style="color: #000000">&</span><span style="color: #000000">a[i], </span><span style="color: #000000">&</span><span style="color: #000000">b[i]);<br /></span><span style="color: #008080">72</span> <span style="color: #000000">        memset(cost, </span><span style="color: #000000">0</span><span style="color: #000000">, </span><span style="color: #0000ff">sizeof</span><span style="color: #000000">(cost));<br /></span><span style="color: #008080">73</span> <span style="color: #000000">        </span><span style="color: #0000ff">for</span><span style="color: #000000">(</span><span style="color: #0000ff">int</span><span style="color: #000000"> t </span><span style="color: #000000">=</span><span style="color: #000000"> </span><span style="color: #000000">0</span><span style="color: #000000">; t </span><span style="color: #000000"><</span><span style="color: #000000"> (</span><span style="color: #000000">1</span><span style="color: #000000"> </span><span style="color: #000000"><<</span><span style="color: #000000"> k); t</span><span style="color: #000000">++</span><span style="color: #000000">)<br /></span><span style="color: #008080">74</span> <span style="color: #000000">            </span><span style="color: #0000ff">for</span><span style="color: #000000">(</span><span style="color: #0000ff">int</span><span style="color: #000000"> j </span><span style="color: #000000">=</span><span style="color: #000000"> </span><span style="color: #000000">0</span><span style="color: #000000">; j </span><span style="color: #000000"><</span><span style="color: #000000"> k; j</span><span style="color: #000000">++</span><span style="color: #000000">)<br /></span><span style="color: #008080">75</span> <span style="color: #000000">                </span><span style="color: #0000ff">if</span><span style="color: #000000">(t </span><span style="color: #000000">&</span><span style="color: #000000"> (</span><span style="color: #000000">1</span><span style="color: #000000"> </span><span style="color: #000000"><<</span><span style="color: #000000"> j)) cost[t] </span><span style="color: #000000">+=</span><span style="color: #000000"> b[j];<br /></span><span style="color: #008080">76</span> <span style="color: #000000">        </span><span style="color: #0000ff">int</span><span style="color: #000000"> ans </span><span style="color: #000000">=</span><span style="color: #000000"> bfs();<br /></span><span style="color: #008080">77</span> <span style="color: #000000">        </span><span style="color: #0000ff">if</span><span style="color: #000000">(ans </span><span style="color: #000000">==</span><span style="color: #000000"> </span><span style="color: #000000">-</span><span style="color: #000000">1</span><span style="color: #000000">) printf(</span><span style="color: #000000">"</span><span style="color: #000000">Impossible\n</span><span style="color: #000000">"</span><span style="color: #000000">);<br /></span><span style="color: #008080">78</span> <span style="color: #000000">        </span><span style="color: #0000ff">else</span><span style="color: #000000"> printf(</span><span style="color: #000000">"</span><span style="color: #000000">%d\n</span><span style="color: #000000">"</span><span style="color: #000000">, ans);<br /></span><span style="color: #008080">79</span> <span style="color: #000000">    }<br /></span><span style="color: #008080">80</span> <span style="color: #000000">    </span><span style="color: #0000ff">return</span><span style="color: #000000"> </span><span style="color: #000000">0</span><span style="color: #000000">;<br /></span><span style="color: #008080">81</span> <span style="color: #000000">}</span></div><br /> <img src ="http://www.shnenglu.com/y346491470/aggbug/154450.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.shnenglu.com/y346491470/" target="_blank">y @ The Angry Teletubbies</a> 2011-08-26 20:13 <a href="http://www.shnenglu.com/y346491470/articles/154450.html#Feedback" target="_blank" style="text-decoration:none;">鍙戣〃璇勮</a></div>]]></description></item><item><title>zoj 3446 Doraemon's Battle - bfshttp://www.shnenglu.com/y346491470/articles/153663.htmly @ The Angry Teletubbiesy @ The Angry TeletubbiesWed, 17 Aug 2011 07:01:00 GMThttp://www.shnenglu.com/y346491470/articles/153663.htmlhttp://www.shnenglu.com/y346491470/comments/153663.htmlhttp://www.shnenglu.com/y346491470/articles/153663.html#Feedback0http://www.shnenglu.com/y346491470/comments/commentRss/153663.htmlhttp://www.shnenglu.com/y346491470/services/trackbacks/153663.html銆愰鎰忋戯細Doraemon姝e湪涓巑涓晫浜烘垬鏂楋紝Doraemon鏈変袱縐嶅睘鎬P鍜孲P錛堢粡甯告墦鏈虹殑鏈嬪弸閮界煡閬撴槸浠涔堝枃錛孒P鏄閲忥紝SP鏄瓟娉曢噺錛夈傚鏋淒oraemon鐨凥P灝忎簬鎴栫瓑浜?鐨勮瘽錛岄偅涔堝氨鎰忓懗鐫鎴樻枟澶辮觸銆侶P鍜孲P鍒嗗埆鏈変竴涓笂闄怢H銆丩S錛屾瘡涓洖鍚堜腑Doraemon鍜屾晫浜鴻疆嫻佽鍔紙Doraemon鎬繪槸鍏堣鍔級錛屽湪姣忎釜鍥炲悎涓紝Doraemon鑳介夋嫨涓嬪垪4縐嶆搷浣滀腑鐨勫叾涓竴縐嶏細
      1.鏉姝諱竴涓晫浜哄茍浣縎P鍥炲1鐐?nbsp;
      2.鎭㈠floor(0.2*LH錛夌殑琛閲?nbsp;
      3.濡傛灉褰撳墠SP澶т簬0錛屽彲浠ュ彂鍔ㄦ妧鑳芥潃姝籇[SP]涓晫浜猴紝騫朵笖SP娓呴浂 
      4.寰呮満錛屽叾瀹炲氨鏄粈涔堥兘涓嶅仛
姣忔鍦―oraemon琛屽姩瀹屽悗錛屾瘡涓晫浜哄彲浠ユ秷鑰桪oraemon涓婊磋銆傚湪姣忎釜鍥炲悎鐨勬渶鍚庯紝Doraemon鑳藉鎭㈠[褰撳墠鏁屼漢鏁?3]鐨凷P銆備竴寮濮婦oraemon澶勪簬婊¤錛岄浂SP鐨勭姸鎬侊紝闂瓺oraemon鏉姝繪墍鏈夋晫浜虹殑鏈灝忓洖鍚堟暟鏄灝戯紵濡傛灉Doraemon涓嶅彲鑳芥垬鑳滐紝璇瘋緭鍑?#8220;HELP!”銆?br />
銆愰瑙c戯細鍦ㄦ瘮璧涗腑鐪嬪埌榪欓亾棰樿寰楁槸鍗氬紙錛岀劧鍚嶻Y浜嗕竴杞箣鍚庢病鎵懼埌浠諱綍瑙勫緥銆傚悗鏉ュ彂鐜拌繖鏄竴閬撴悳绱㈤錛屼竴涓崟綰殑bfs鍙В錛岃屼笖bfs鑳芥弧瓚蟲渶灝忕殑鍥炲悎鏁般傝繖閬撻緇嗚妭鐨勫湴鏂規瘮杈冨錛?br />      1.褰撳墠鐨凥P鍜孲P鍦ㄤ換浣曟椂鍊欓兘涓嶈兘瓚呰繃涓婇檺LH銆丩S
      2.姣忓洖鍚堢粨鏉熷墠閮借鎭㈠[褰撳墠鏁屼漢鏁?3]鐨凷P
      3.姣忓洖鍚堟繪槸Doraemon鍏堣鍔紝鐒跺悗杞埌鏁屼漢琛屽姩
      4.鍦ㄤ嬌鐢ㄥ畬鎶鑳藉悗SP瑕佸彉鎴?錛屽茍涓?[褰撳墠鏁屼漢鏁?- D[SP]] 鍙兘鍑虹幇璐熸暟
      5.[灝忎紭鍖朷絎洓縐嶆搷浣滃緟鏈烘樉鐒舵槸娌$敤鐨勶紝濡傛灉閫夋嫨寰呮満鐨勮瘽榪樹笉濡傞夋嫨鍔犺錛岃繖閲屽彲浠ュ壀鎺夈?br />
銆愪唬鐮併戯細
 1 #include "iostream"
 2 #include "cstdio"
 3 #include "cstring"
 4 #include "queue"
 5 #include "cmath"
 6 using namespace std;
 7 #define MAX 105
 8 int lh, ls, n;
 9 int d[MAX];
10 int visit[255][105][45];
11 int add;
12 
13 struct st {
14     int hp, sp, cnt;
15     void init(int a, int b, int c) {
16         if(a > lh) a = lh;
17         if(b > ls) b = ls;
18         hp = a, sp = b, cnt = c;
19     }
20 } start;
21 
22 int bfs() {
23     queue<st> que;
24     que.push(start);
25     visit[start.hp][start.sp][start.cnt] = 1;
26     while (!que.empty()) {
27         st now = que.front(), next;
28         que.pop();
29         //絎竴縐嶆搷浣?/span>
30         next.init(now.hp - (now.cnt - 1), now.sp + 1 + (now.cnt - 1% 3, (now.cnt - 1));
31         if (next.cnt <= 0return visit[now.hp][now.sp][now.cnt];
32         if (next.hp > 0 && !visit[next.hp][next.sp][next.cnt]) {
33             visit[next.hp][next.sp][next.cnt] = visit[now.hp][now.sp][now.cnt] + 1;
34             que.push(next);
35         }
36         //絎簩縐嶆搷浣?/span>
37         next.init(now.hp + add - now.cnt, now.sp + now.cnt % 3, now.cnt);
38         if (next.hp > 0 && !visit[next.hp][next.sp][next.cnt]) {
39             visit[next.hp][next.sp][next.cnt] = visit[now.hp][now.sp][now.cnt] + 1;
40             que.push(next);
41         }
42         //絎笁縐嶆搷浣?/span>
43         if (now.sp > 0) {
44             if(now.cnt - d[now.sp] <= 0return visit[now.hp][now.sp][now.cnt];
45             next.init(now.hp - (now.cnt - d[now.sp]), (now.cnt - d[now.sp]) % 3, (now.cnt - d[now.sp]));
46             if (next.hp > 0 && !visit[next.hp][next.sp][next.cnt]) {
47                 visit[next.hp][next.sp][next.cnt] = visit[now.hp][now.sp][now.cnt] + 1;
48                 que.push(next);
49             }
50         }
51     }
52     return -1;
53 }
54 
55 void init() {
56     for (int i = 0; i < 255; i++)
57         for (int j = 0; j < 105; j++)
58             for (int k = 0; k < 45; k++)
59                 visit[i][j][k] = 0;
60     add = (int) floor(0.2 * lh);
61 }
62 
63 int main() {
64     while (~scanf("%d%d%d"&lh, &ls, &n)) {
65         for (int i = 1; i <= ls; i++) scanf("%d"&d[i]);
66         start.init(lh, 0, n);
67         init();
68         int ans = bfs();
69         if (ans == -1) printf("HELP!\n");
70         else printf("%d\n", ans);
71     }
72     return 0;
73 }



y @ The Angry Teletubbies 2011-08-17 15:01 鍙戣〃璇勮
]]>
zoj 3291 Never End - bfshttp://www.shnenglu.com/y346491470/articles/153474.htmly @ The Angry Teletubbiesy @ The Angry TeletubbiesMon, 15 Aug 2011 14:37:00 GMThttp://www.shnenglu.com/y346491470/articles/153474.htmlhttp://www.shnenglu.com/y346491470/comments/153474.htmlhttp://www.shnenglu.com/y346491470/articles/153474.html#Feedback0http://www.shnenglu.com/y346491470/comments/commentRss/153474.htmlhttp://www.shnenglu.com/y346491470/services/trackbacks/153474.html銆愰鎰忋戯細鍦ㄤ竴涓糠瀹噷錛屾湁涓涓漢錛岃繕鏈変竴涓嚭鍙c傝繖閲屾湁鍥涚鎿嶄綔錛?.浜哄悜宸﹁蛋 2.浜哄悜鍙寵蛋 3.鎶婃暣涓糠瀹悜宸﹁漿 4.鎶婃暣涓糠瀹悜鍙寵漿銆傛瘡鎿嶄綔涓嬈¢兘浼氳姳璐逛竴涓椂闂村崟浣嶃傞棶浜烘渶鍚庤兘鍚﹁蛋鍑鴻糠瀹紵濡傛灉鑳斤紝杈撳嚭鏈鐭殑鏃墮棿銆?br />
銆愰瑙c戯細鐪嬪埌鏈鐭椂闂達紝鎴戜滑寰堝鏄撳氨鎯沖埌bfs銆傝繖棰樹笌騫蟲椂鐨勮糠瀹棶棰樻湁涓涓笉鍚岀殑鍦版柟灝辨槸錛屽鉤鏃墮鐩墍緇欑殑榪峰鏄刊瑙嗗浘錛岃岃繖閲屾槸涓昏鍥撅紝鑰冭檻鍒拌繖涓鐐癸紝鎴戜滑灝辮鎶婇噸鍔涘姞鍏ユ垜浠殑鎬濊冭寖鍥淬傜畝鍗曠偣鏉ヨ灝辨槸錛屽鏋滃綋鍓嶈繖涓漢鍚戝乏璧頒簡涓姝ワ紝鑰岄偅涓綅緗槸娌℃湁绔欏潡鐨勶紝閭d箞浜哄氨浼氬悜涓嬭穼錛岀洿鍒版湁涓滆タ欏朵綇涓烘銆傚疄闄呮搷浣滅殑鏃跺欏彲浠ュ浐瀹氳糠瀹笉鍔紝鑰屾槸閫氳繃浜烘潵鏃嬭漿錛岃繖鏄劇劧鏄彲浠ョ殑銆備竴寮濮嬫垜浠彲浠ラ澶勭悊涓涓嬪浜庢瘡涓牸杞姩涔嬪悗浜轟細璺屽湪鍝噷錛岃繖鏍鋒垜浠互鍚庡氨鍙互鐢∣錛?錛夌殑鏃墮棿鏉ユ壘鍒頒笅涓涓姸鎬併傚叾浣欑殑鎿嶄綔璺熸櫘閫歜fs涓鏍楓?br />
銆愪唬鐮併戯細
 1 #include "iostream"
 2 #include "queue"
 3 #include "cstdio"
 4 using namespace std;
 5 #define MAX 505
 6 struct node {
 7     int x, y, dir;
 8     void init(int a, int b, int c) {
 9         x = a, y = b, dir = c;
10     }
11 }rot[MAX][MAX][4], s;
12 int n, m;
13 int ti[MAX][MAX][4];
14 char maz[MAX][MAX];
15 const int dx[4= {10-10};
16 const int dy[4= {010-1};
17 
18 bool check(int i, int j) {
19     if(i >= 1 && i <= n && j >= 1 && j <= m) return true;
20     return false;
21 }
22 
23 int bfs() {
24     queue<node> que;
25     que.push(s);
26     ti[s.x][s.y][s.dir] = 1;
27     while(!que.empty()) {
28         node now = que.front(), next;
29         que.pop();
30         if(maz[now.x][now.y] == 'E'return ti[now.x][now.y][now.dir];
31         for(int i = 0; i < 4; i++) {
32             if((now.dir % 2 == 0 && i % 2 != 0|| (now.dir % 2 != 0 && i % 2 == 0)) {
33                 if(maz[now.x+dx[i]][now.y+dy[i]] != '#') {
34                     next = rot[now.x+dx[i]][now.y+dy[i]][now.dir];
35                     if(!ti[next.x][next.y][next.dir]) {
36                         ti[next.x][next.y][next.dir] = ti[now.x][now.y][now.dir] + 1;
37                         que.push(next);
38                     }
39                 }
40             }
41             if(i % 2) {
42                 next = rot[now.x][now.y][(now.dir + i) % 4];
43                 if(!ti[next.x][next.y][next.dir]) {
44                     ti[next.x][next.y][next.dir] = ti[now.x][now.y][now.dir] + 1;
45                     que.push(next);
46                 }
47             }
48         }
49     }
50     return 0;
51 }
52 
53 void init() {
54     for(int i = n; i; i--)
55         for(int j = m; j; j--)
56             if(maz[i][j] != '#') {
57                 if(check(i + 1, j) && maz[i+1][j] != '#') rot[i][j][0= rot[i+1][j][0];
58                 else rot[i][j][0].init(i, j, 0);
59                 if(check(i, j + 1&& maz[i][j+1!= '#') rot[i][j][3= rot[i][j+1][3];
60                 else rot[i][j][3].init(i, j, 3);
61             }
62     for(int i = 1; i <= n; i++)
63         for(int j = 1; j <= m; j++)
64             if(maz[i][j] != '#') {
65                 if(check(i - 1, j) && maz[i-1][j] != '#') rot[i][j][2= rot[i-1][j][2];
66                 else rot[i][j][2].init(i, j, 2);
67                 if(check(i, j - 1&& maz[i][j-1!= '#') rot[i][j][1= rot[i][j-1][1];
68                 else rot[i][j][1].init(i, j, 1);
69             }
70 }
71 
72 int main() {
73     while(~scanf("%d%d"&n, &m)) {
74         getchar();
75         for(int i = 1; i <= n; i++)
76             for(int j = 1; j <= m + 1; j++) {
77                 for(int k = 0; k < 4; k++) ti[i][j][k] = 0;
78                 scanf("%c"&maz[i][j]);
79                 if(maz[i][j] == '|') s.init(i, j, 0);
80             }
81         init();
82         int ans = bfs();
83         if(!ans) printf("Can not escape!\n");
84         else printf("%d\n", ans - 1);
85     }
86     return 0;
87 }



y @ The Angry Teletubbies 2011-08-15 22:37 鍙戣〃璇勮
]]>
hdoj 3912 Turn Right - dfshttp://www.shnenglu.com/y346491470/articles/153466.htmly @ The Angry Teletubbiesy @ The Angry TeletubbiesMon, 15 Aug 2011 12:07:00 GMThttp://www.shnenglu.com/y346491470/articles/153466.htmlhttp://www.shnenglu.com/y346491470/comments/153466.htmlhttp://www.shnenglu.com/y346491470/articles/153466.html#Feedback0http://www.shnenglu.com/y346491470/comments/commentRss/153466.htmlhttp://www.shnenglu.com/y346491470/services/trackbacks/153466.html
銆愰瑙c戯細棰樼洰鏄庢樉灝辨槸璁╀綘鎼滅儲鐨勶紝瀵逛簬姣忎釜鏍煎瓙i,j鍔犲涓緇磋褰曟柟鍚戝氨濂戒簡銆傜敱浜庢暟鎹渶澶ф槸500*500錛屾墍浠ョ敤緋葷粺鏍堜細鐖嗘帀錛屾敼鎴愰潪閫掑綊鍗沖彲銆傚鏋滀綘娣變竴灞傜悊瑙i鐩殑璇濓紝浼氬彂鐜版槸涓嶉渶瑕佺敤鏍堢殑錛屽彧闇瑕佷竴涓驚鐜氨鍙互浜嗭紝鐩存帴涓婁唬鐮佷簡錛屽ぇ瀹朵笉鎳傚啀闂惂銆?br />
銆愪唬鐮併戯細
 1 #include <iostream>
 2 #include <cstdio>
 3 using namespace std;
 4 #define MAX 505
 5 
 6 struct node {
 7     int x, y, dir;
 8     void init(int a, int b, int c) {
 9         x = a, y = b, dir = c;
10     }
11 } s, t, goal;
12 int r, c, n, m;
13 int maz[MAX][MAX][4];
14 bool visit[MAX][MAX];
15 const int dx[] = {010-1};
16 const int dy[] = {10-10};
17 
18 void dfs(node now, node goal) {
19     node next;
20     while (1) {
21         visit[now.x][now.y] = true;
22         if (now.x == goal.x && now.y == goal.y && now.dir == goal.dir) break;
23         for (int i = now.dir; i < 4 + now.dir; i++) {
24             int index = (now.dir - (i - now.dir) + 4% 4;
25             if (maz[now.x][now.y][(now.dir - (i - now.dir) + 5% 4== 0) {
26                 next.init(now.x + dx[index], now.y + dy[index], (now.dir - (i - now.dir) + 5% 4);
27                 now = next;
28                 break;
29             }
30         }
31     }
32 }
33 
34 void init() {
35     for (int i = 1; i <= r; i++)
36         for (int j = 1; j <= c; j++) {
37             visit[i][j] = false;
38             for (int k = 0; k < 4; k++)
39                 maz[i][j][k] = 1;
40         }
41         maz[t.x][t.y][s.dir] = maz[s.x][s.y][t.dir] = 0;
42 }
43 
44 int main() {
45     int T;
46     scanf("%d"&T);
47     while (T--) {
48         scanf("%d%d"&r, &c);
49         scanf("%d%d"&s.y, &t.y);
50         s.init(1, s.y + 12), t.init(r, t.y + 10);
51         init();
52         for (int i = 1; i < 2 * r; i++) {
53             if (i % 2 != 0) {
54                 for (int j = 1; j < c; j++) {
55                     scanf("%d"&maz[(i + 1/ 2][j][1]);
56                     maz[(i + 1/ 2][j + 1][3= maz[(i + 1/ 2][j][1];
57                 }
58             } else {
59                 for (int j = 1; j <= c; j++) {
60                     scanf("%d"&maz[i / 2][j][2]);
61                     maz[i / 2 + 1][j][0= maz[i / 2][j][2];
62                 }
63             }
64         }
65         goal.init(t.x + 1, t.y, s.dir);
66         dfs(s, goal);
67         goal.init(s.x - 1, s.y, t.dir);
68         dfs(t, goal);
69         bool flag = false;
70         for (int i = 1; i <= r; i++)
71             for (int j = 1; j <= c; j++)
72                 if (!visit[i][j])
73                     flag = true;
74         if (!flag) printf("YES\n");
75         else printf("NO\n");
76     }
77     return 0;
78 }




y @ The Angry Teletubbies 2011-08-15 20:07 鍙戣〃璇勮
]]>
国产精品视频久久| 亚洲欧美成人综合久久久| 99久久99久久精品国产片果冻| 久久99精品久久久久子伦| 2021国产精品久久精品| 久久久WWW成人| 久久久久久毛片免费看| 青草久久久国产线免观| 亚洲国产天堂久久综合| 东方aⅴ免费观看久久av| 欧美久久综合九色综合| 香蕉久久永久视频| 囯产精品久久久久久久久蜜桃| 九九精品99久久久香蕉| 久久精品这里热有精品| 精品国产一区二区三区久久蜜臀| 欧美激情精品久久久久久| 精品久久久久久中文字幕大豆网 | 久久影视综合亚洲| 精品伊人久久久| 91精品国产91久久久久福利| 久久久WWW成人免费毛片| 亚洲va久久久噜噜噜久久| 91久久香蕉国产熟女线看| 国内精品久久久久影院老司| 久久99精品国产麻豆宅宅| 麻豆精品久久久久久久99蜜桃| 好久久免费视频高清| 亚洲国产精品狼友中文久久久 | 伊人色综合久久天天| 日本五月天婷久久网站| 麻豆精品久久久一区二区| 久久久久久久久久久| 久久国产高清一区二区三区| 久久夜色精品国产欧美乱| 久久露脸国产精品| 青青草国产成人久久91网| 奇米综合四色77777久久| 99久久综合国产精品免费| 久久久久亚洲AV成人网人人网站| 久久久国产精品网站|