Queue ----- chain storage structure and implementation of its operation algorithm ----- HOJ2581

Source: Internet
Author: User

[Cpp] # ifndef _ QUEUE_LINK_H_HUMING_INCLUDE _ # define _ QUEUE_LINK_H_HUMING_INCLUDE _ # include <cstdio> template <class T> class node {public: T data; node <T> * next; node (): next (NULL) {}; // No-arg constructor node (T p) // Constructor {this-> data = p; // "this" means "class node" next = NULL ;};}; // node defination template <class T> class queue {public: queue ();~ Queue (); // to avoid memory leak, a destructor is needed. bool empty (); void pop (); void push (T x); T front (); private: node <T> * head, * tail ;}; template <class T> void queue <T>: push (T x) {node <T> * q = new node <T>; q-> data = x; q-> next = NULL; tail-> next = q; tail = q;} template <class T> queue <T>: queue () {head = new node <T>; head-> next = NULL; tail = head;} template <class T> bool queue <T >:: empty () {return (Tail = head); // This method is simple!} Template <class T> void queue <T>: pop () {if (! Empty () {node <T> * x; x = head-> next; head-> next = x-> next; if (x-> next = NULL) tail = head; delete x ;}}template <class T> T queue <T >:: front () {return (head-> next-> data );} // you do not need to judge whether it is null. before calling the function, write a template <class T> queue <T> ::~ Queue () // delete all nodes including "head ". {node <T> * x; while (head) {x = head; head = head-> next; delete x ;}} # endif [cpp] view plaincopy # include <iostream> # include "queue_link.h" # include <cstring> # define maxlen 22 # include <cstdio> int mat [maxlen] [maxlen], black, white; int dir [4] [2] = {-}, {}, {0,-1}, {}; using namespace std; struct ND {int x, y ;}; void BFS (ND s, int n) {int f1 = 0, f2 = 0, coun T = 0; queue <ND> q; ND ol, ne; while (! Q. empty () {q. pop ();} q. push (s); mat [s. x] [s. y] = 3; while (! Q. empty () {ol = q. front (); q. pop (); count ++; // count is updated after the team leaves, and for (int I = 0; I <4; I ++) {ne. x = ol. x + dir [I] [0]; ne. y = ol. y + dir [I] [1]; if (ne. x> = 1 & ne. x <= n & ne. y> = 1 & ne. y <= n) {if (mat [ne. x] [ne. y] = 0) {mat [ne. x] [ne. y] = 3; q. push (ne);} else if (mat [ne. x] [ne. y] = 1) f1 = 1; else if (mat [ne. x] [ne. y] = 2) f2 = 1 ;}} if (f1 + f2! = 2) // as long as this happens to both, it indicates that this situation is surrounded by black and white games {if (f2) black + = count; if (f1) white + = count ;}} int main () {int n, B, w, I, j; ND s; while (cin >>> n & n) {black = 0, white = 0; memset (mat, 0, sizeof (mat); cin> B> w; while (B --) {cin> I> j; mat [I] [j] = 2; // B} while (w --) {cin> I> j; mat [I] [j] = 1; // w} for (I = 1; I <= n; I ++) for (j = 1; j <= n; j ++) {if (mat [I] [j] = 0) {s. x = I; s. y = j; BFS (s, n) ;}} if (black> white) printf ("Black wins by % d \ n", black-white ); else if (white> black) printf ("White wins by % d \ n", white-black); else printf ("Draw \ n");} return 0 ;}

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.