[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 ;}