[Topcoder] SRM207

來源:互聯網
上載者:User

Div2 Lev3

 

#include <iostream><br />#include <cstring><br />#include <vector><br />#include <queue><br />#include <deque><br />using namespace std;</p><p>typedef struct node {<br />string pos;<br />int hasKillRook;<br />int hasKillQueen;<br />int step;<br />} node;</p><p>const int coffset[8] = {-2, -2, 2, 2, -1, 1, -1, 1};<br />const int roffset[8] = {-1, 1, -1, 1, -2, -2, 2, 2};</p><p>class CaptureThemAll {<br />private:</p><p>public:<br />int fastKnight(string knight, string rook, string queen) {<br />int visited[4][64] = {0};<br />deque<node> q;<br />// push the first pos of knight<br />node n;<br />n.pos = knight;<br />n.hasKillRook = 0;<br />n.hasKillQueen = 0;<br />n.step = 0;<br />visited[0][64] = 1;<br />q.push_back(n);</p><p>while (!q.empty()) {<br />n = q.front();<br />q.pop_front();<br />cerr << n.pos << endl;</p><p>if (n.hasKillQueen && n.hasKillRook)<br />return n.step;<br />else if (n.hasKillQueen)<br />visited[2][(n.pos[0] - 'a') * 8 + n.pos[1] - '1']++;<br />else if (n.hasKillRook)<br />visited[1][(n.pos[0] - 'a') * 8 + n.pos[1] - '1']++;<br />else<br />visited[0][(n.pos[0] - 'a') * 8 + n.pos[1] - '1']++;</p><p>for (int i = 0; i < 8; i++) {<br />string curp(2, '/0');<br />curp[0] = n.pos[0] + coffset[i];<br />curp[1] = n.pos[1] + roffset[i];</p><p>// out of bound<br />if (curp[0] < 'a' || curp[0] > 'h' ||<br />curp[1] < '1' || curp[1] > '8')<br /> continue;</p><p>node newn = n;<br />newn.pos = curp;<br />if (curp == queen)<br />newn.hasKillQueen = 1;<br />if (curp == rook)<br />newn.hasKillRook = 1;<br />newn.step++;</p><p>int flg = 0;<br />if (newn.hasKillQueen && newn.hasKillRook)<br />return newn.step;<br />else if (newn.hasKillQueen)<br />flg = 2;<br />else if (newn.hasKillRook)<br />flg = 1;<br />else<br />flg = 0;</p><p>if (visited[flg][(newn.pos[0] - 'a') * 8 + newn.pos[1] - '1'])<br />continue;</p><p>q.push_back(newn);<br />}<br />}</p><p>return -1;<br />}<br />};</p><p>int main()<br />{<br />CaptureThemAll cta;<br />cout << "example 0: " << cta.fastKnight("a1", "b3", "c5") << endl;<br />cout << "example 1: " << cta.fastKnight("b1", "c3", "a3") << endl;<br />cout << "example 2: " << cta.fastKnight("a1", "a2", "b2") << endl;<br />cout << "example 3: " << cta.fastKnight("a5", "b7", "e4") << endl;<br />cout << "example 4: " << cta.fastKnight("h8", "e2", "d2") << endl;<br />return 0;<br />}<br />

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.