[Topcoder] srm197

Source: Internet
Author: User

Divii lev2

 

First, define the offset in eight directions. Take the first coordinate of the array pieces and obtain the eight offset coordinates. The result must be a subset of these coordinates, if the coordinates threaten other coordinates of the array, the result array is added. The order of the question must be arranged, as long as the offset is defined in an ordered order, which saves explicit sorting.

# Include <iostream> <br/> # include <string> <br/> # include <vector> <br/> # include <cmath> <br/> # include <sstream> <br/> # include <iterator> <br/> using namespace STD; </P> <p> # define ABS (x) (x <0? -X: X) </P> <p> const int xoffset [8] = {-2,-2,-1,-1, 1, 1, 2, 2 }; <br/> const int yoffset [8] = {-1, 1,-2, 2,-2, 2,-1, 1 }; </P> <p> class generalchess {<br/> PRIVATE: </P> <p> int canthreaten (const int curx, const int Cury, <br/> const int posx, const int posy) {<br/> If (ABS (curx-posx) = 1 & ABS (Cury-Posy) = 2 <br/> | ABS (curx-posx) = 2 & ABS (Cury-Posy) = 1) <br/> return 1; <br/> else <br/> Return 0; <br/>}</P> <p> Public: <br/> vector <string> attackpositions (vector <string> pieces) {<br/> vector <string> res; <br/> int X, Y; <br/> char t; </P> <p> istringstream SS (pieces. at (0); <br/> SS> x> T> Y; </P> <p> for (INT I = 0; I <8; I ++) {<br/> int curx = x + xoffset [I]; <br/> int Cury = Y + yoffset [I]; <br/> Int J = 1; <br/> for (j = 1; j <pieces. size (); j ++) {<br/> istringstream OSS (pieces. at (J); <br/> int posx, Posy; <br/> OSS> posx> T> posy; <br/> If (! Canthreaten (curx, Cury, posx, Posy) {<br/> break; <br/>}</P> <p >}< br/> If (j = pieces. size () {<br/> ostringstream RESS; <br/> RESS <curx <',' <Cury; <br/> res. push_back (RESS. STR (); <br/>}< br/> return res; <br/>}< br/> }; </P> <p> int main () <br/> {<br/> generalchess GC; <br/> vector <string> VEC; <br/> Vec. push_back ("0, 0"); </P> <p> vector <string> res = GC. attackpositions (VEC); </P> <p> copy (res. begin (), Res. end (), <br/> ostream_iterator <string> (cout, ""); <br/> cout <Endl; </P> <p> Vec. clear (); <br/> Vec. push_back ("2, 1"); <br/> Vec. push_back ("-1,-2"); <br/> res = GC. attackpositions (VEC); <br/> copy (res. begin (), Res. end (), <br/> ostream_iterator <string> (cout, ""); <br/> cout <Endl; </P> <p> return 0; <br/>}

Of course, if you want to explicitly sort, you can also:

Bool compare (string a, string B) {<br/> int ax, ay; <br/> int BX, by; <br/> char T; </P> <p> istringstream SS (a); <br/> SS> ax> T> ay; </P> <p> SS. clear (); <br/> SS. STR (B); <br/> SS> BX> T>; </P> <p> If (ax <BX <br/> | Ax = Bx & ay <by) <br/> return true; <br/> else <br/> return false; <br/>}</P> <p> int main () <br/>{< br/> vector <string> VEC; <br/> Vec. push_back ("0, 0"); <br/> Vec. push_back ("2, 1"); <br/> Vec. push_back ("1, 2"); </P> <p> sort (VEC. begin (), VEC. end (), compare); </P> <p> copy (VEC. begin (), VEC. end (), <br/> ostream_iterator <string> (cout, ""); <br/> cout <Endl; </P> <p> 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.