[Topcoder] srm233

Source: Internet
Author: User

Divi lev2

 

# Include <iostream> <br/> # include <list> <br/> # include <vector> <br/> # include <cstring> <br/> # include <queue> <br/> # include <iterator> <br/> # include <algorithm> <br/> # include <sstream> <br/> using namespace STD; </P> <p> # define node_num (26*26*26*26) </P> <p> typedef struct node {<br/> long distance; <br/>} node; </P> <p> class smartwordtoy {<br/> PRIVATE: <br/> node * vertex; <br/> short * visited; <br/> Vector <string> pattern; </P> <p> Public: </P> <p> long str2id (string Str) {<br/> long sum = 0; <br/> for (INT I = 0; I <4; I ++) {<br/> sum = sum * 26 + STR [I]-'A '; <br/>}< br/> return sum; <br/>}</P> <p> string id2str (long ID) {<br/> string STR (4, 'A'); <br/> for (INT I = 3; I> = 0; I --) {<br/> STR [I] = ('A' + id % 26); <br/> ID/= 26; <br/>}< br/> return STR; <br/>}</P> <p> Public: <br/> smartwordtoy () {<br/> Vertex = new node [node_num]; <br/> memset (vertex,-1, sizeof (node) * node_num ); </P> <p> visited = new short [node_num]; <br/> memset (visited, 0, sizeof (short) * node_num ); </P> <p >}< br/> ~ Smartwordtoy () {<br/> Delete [] vertex; <br/>}</P> <p> int minpresses (string start, string finish, vector <string> forbid) {<br/> // construct pattern <br/> for (INT I = 0; I <forbid. size (); I ++) {<br/> istringstream ISS (forbid [I]); <br/> string sub; <br/> while (ISS> sub) {<br/> pattern. push_back (sub); <br/>}</P> <p> // find the shortest path <br/> queue <long> q; <br/> long start_id = str2id (start); <Br/> vertex [start_id]. distance = 0; <br/> visited [start_id] = 1; <br/> q. push (start_id); <br/> while (! Q. empty () {<br/> long cur_id = Q. front (); <br/> q. pop (); <br/> string cur_str = id2str (cur_id); <br/> If (cur_str = finish) <br/> return vertex [cur_id]. distance; </P> <p> for (INT I = 0; I <4; I ++) <br/> for (Int J =-1; j <= 1; j + = 2) {</P> <p> string tmp_str = cur_str; <br/> tmp_str [I] + = J; <br/> tmp_str [I] = (tmp_str [I]-'A' + 26) % 26 + 'a '; </P> <p> long next_id = str2id (tmp_str); </P> <p> If (visited [next _ Id] = 1) <br/> continue; </P> <p> short forbid_flg = 0; <br/> for (int K = 0; k <forbid. size (); k ++) {<br/> int match = 0; <br/> for (int l = 0; L <4; l ++) {<br/> size_t found = pattern [K * 4 + L]. find (tmp_str [l]); <br/> If (found! = String: NPOs) <br/> match ++; <br/> else <br/> break; <br/>}< br/> If (match = 4) {<br/> forbid_flg = 1; <br/> break; <br/>}</P> <p> If (forbid_flg = 0) {<br/> vertex [next_id]. distance = vertex [cur_id]. distance + 1; <br/> visited [next_id] = 1; <br/> q. push (next_id); <br/>}< br/> return-1; <br/>}< br/> }; </P> <p> int main () <br/> {<br/> smartwordtoy SWT; <br/> // cout <SWT. str2id ("ABCD") <Endl; <br/> // cout <SWT. id2str( 731) <Endl; </P> <p> string start ("aaaa"); <br/> string finish ("ZZZZ "); </P> <p> vector <string> forbid; <br/> forbid. push_back ("bz a"); <br/> forbid. push_back ("a bz a"); <br/> forbid. push_back ("A a BZ a"); <br/> forbid. push_back ("A a BZ"); </P> <p> cout <SWT. minpresses (START, finish, forbid) <Endl; <br/> return 0; <br/>}< br/>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.