HDU 1401 zoj1505 PKU 1198 bidirectional wide search

Source: Internet
Author: User
Tags bitset

/* <Br/> I am afraid that I will never write it... <Br/> it is always strange to store the status, but the status in bfs dfs is very important. <br/> I wanted to write this question before... <Br/> the AC is finished .. <Br/> */<br/> # include <iostream> <br/> # include <cstdio> <br/> # include <bitset> <br/> # include <cstdio> <br/> # include <cstring> <br/> # include <algorithm> <br/> # include <queue> <br/> using namespace STD; <br/> const int n = (1 <24) + 10); <br/> bitset <n> hash1; // This can save memory <br/> bitset <n> hash2; <br/> int dir1 [4] [2] ={{-1, 0 }, {1, 0}, {0, 1}, {0,-1 }}; <br/> int dir2 [4] [2] = {-2, 0 },{ 2, 0 },{ 0, 2}, {0,-2 }}; <br/> char map1 [9] [9], MAP2 [9] [9]; <br/> struct dir <br/>{< br/> int X, Y; <br/> }; <br/> struct node <br/> {<br/> dir [4]; <br/> int step; <br/> char map [9] [9]; <br/> node () <br/> {<br/> for (INT I = 0; I <9; I ++) <br/> for (Int J = 0; j <9; j ++) <br/> map [I] [J] = 'a '; <br/> step = 0; <br/>}< br/>}; <br/> node start and end; <br/> inline bool CMP (const dir & A, const dir & B) <br/> {// sort coordinates to avoid unnecessary incoming queues <br /> If (. X = B. x) <br/> return. Y <B. y; <br/> else <br/> return. x <B. x; <br/>}< br/> inline int Hash (const node & node) <br/> {// convert coordinates to one-dimensional <br/> int num; <br/> int sum = 0; <br/> for (INT I = 0; I <4; I ++) <br/>{< br/> num = (node. dir [I]. x-1) * 8 + node. dir [I]. y; <br/> sum = 64 * sum + num; <br/>}< br/> return sum; <br/>}< br/> void BFS () <br/>{< br/> queue <node> q1, Q2; <br/> q1.push (start); <br/> q2.pus H (end); <br/> node now, next; <br/> hash1.reset (); <br/> hash2.reset (); <br/> hash1 [Hash (start)] = true; <br/> hash2 [Hash (end)] = true; <br/> int X, Y; <br/> for (INT step = 0; Step <4; Step ++) <br/>{< br/> while (! Q1.empty () <br/>{< br/> next = now = q1.front (); <br/> q1.pop (); <br/> If (next. step> 4) <br/> break; <br/> for (INT I = 0; I <4; I ++) // four dots D indicate the pawns, a Indicates blank <br/>{< br/> for (Int J = 0; j <4; j ++) <br/> {// first search for the direction <br/> next = now; <br/> X = now. dir [I]. X + dir1 [J] [0]; <br/> Y = now. dir [I]. Y + dir1 [J] [1]; <br/> If (x> = 1 & x <= 8 & Y> = 1 & Y <= 8 & now. map [x] [Y]! = 'D') <br/>{< br/> next. dir [I]. X = x; <br/> next. dir [I]. y = y; <br/> next. step = now. step + 1; <br/> next. map [x] [Y] = 'D'; <br/> next. map [now. dir [I]. x] [now. dir [I]. y] = 'a'; <br/> sort (next. dir, next. dir + 4, CMP); <br/> If (hash1 [Hash (next)]) <br/> continue; <br/> If (hash2 [Hash (next)]) <br/> {// if this state has been hashed by end, it indicates that printf ("Yes/N") exists. <br/> return; <br/>}< br/> hash1 [Hash (next)] = true; <br/> Q1. Push (next); <br/>}< br/> for (Int J = 0; j <4; j ++) <br/>{< br/> next = now; <br/> X = now. dir [I]. X + dir2 [J] [0]; <br/> Y = now. dir [I]. Y + dir2 [J] [1]; <br/> If (x> = 1 & x <= 8 & Y> = 1 & Y <= 8 & <br/> now. map [now. dir [I]. X + dir1 [I] [0] [now. dir [I]. Y + dir1 [I] [1] = 'D' <br/> & now. map [x] [Y]! = 'D') <br/>{< br/> next. dir [I]. X = x; <br/> next. dir [I]. y = y; <br/> next. step = now. step + 1; <br/> next. map [x] [Y] = 'D'; <br/> next. map [now. dir [I]. x] [now. dir [I]. y] = 'a'; <br/> sort (next. dir, next. dir + 4, CMP); <br/> If (hash1 [Hash (next)]) <br/> continue; <br/> If (hash2 [Hash (next)]) <br/>{< br/> printf ("Yes/N"); <br/> return; <br/>}< br/> hash1 [Hash (next)] = true; <br/> q1.push (next); <br/>}< BR/>}< br/>}</P> <p> while (! Q2.empty () <br/>{< br/> next = now = q2.front (); <br/> q2.pop (); <br/> If (next. step = Step + 1) <br/> break; <br/> for (INT I = 0; I <4; I ++) // four vertices D indicates the pawn, a Indicates blank <br/>{< br/> for (Int J = 0; j <4; j ++) <br/>{< br/> next = now; <br/> X = now. dir [I]. X + dir1 [J] [0]; <br/> Y = now. dir [I]. Y + dir1 [J] [1]; <br/> If (x> = 1 & x <= 8 & Y> = 1 & Y <= 8 & now. map [x] [Y]! = 'D') <br/>{< br/> next. dir [I]. X = x; <br/> next. dir [I]. y = y; <br/> next. step = now. step + 1; <br/> next. map [x] [Y] = 'D'; <br/> next. map [now. dir [I]. x] [now. dir [I]. y] = 'a'; <br/> sort (next. dir, next. dir + 4, CMP); <br/> If (hash2 [Hash (next)]) <br/> continue; <br/> If (hash1 [Hash (next)]) <br/>{< br/> printf ("Yes/N"); <br/> return; <br/>}< br/> hash2 [Hash (next)] = true; <br/> q2.push (next); <br/>}< B R/> for (Int J = 0; j <4; j ++) <br/>{< br/> next = now; <br/> X = now. dir [I]. X + dir2 [J] [0]; <br/> Y = now. dir [I]. Y + dir2 [J] [1]; <br/> If (x> = 1 & x <= 8 & Y> = 1 & Y <= 8 & <br/> now. map [now. dir [I]. X + dir1 [I] [0] [now. dir [I]. Y + dir1 [I] [1] = 'D' <br/> & now. map [x] [Y]! = 'D') <br/>{< br/> next. dir [I]. X = x; <br/> next. dir [I]. y = y; <br/> next. step = now. step + 1; <br/> next. map [x] [Y] = 'D'; <br/> next. map [now. dir [I]. x] [now. dir [I]. y] = 'a'; <br/> sort (next. dir, next. dir + 4, CMP); <br/> If (hash2 [Hash (next)]) <br/> continue; <br/> If (hash1 [Hash (next)]) <br/>{< br/> printf ("Yes/N"); <br/> return; <br/>}< br/> hash2 [Hash (next)] = true; <br/> q2.push (next); <br/>}< br/>} <Br/>}< br/> printf ("No/N"); <br/> return; <br/>}< br/> int main () <br/>{< br/> int X, Y; <br/> while (scanf ("% d", & X, & Y )! = EOF) <br/>{< br/> node SS, ee; <br/> SS. map [x] [Y] = 'D'; <br/> SS. dir [0]. X = x; <br/> SS. dir [0]. y = y; <br/> for (INT I = 1; I <4; I ++) <br/>{< br/> scanf ("% d", & X, & Y); <br/> SS. map [x] [Y] = 'D'; <br/> SS. dir [I]. X = x; <br/> SS. dir [I]. y = y; <br/>}< br/> for (INT I = 0; I <4; I ++) <br/>{< br/> scanf ("% d", & X, & Y); <br/> ee. map [x] [Y] = 'D'; <br/> ee. dir [I]. X = x; <br/> ee. dir [I]. y = y; <br/>}< br/> Start = SS; <br/> end = EE; <br/> sort (start. dir, start. dir + 4, CMP); <br/> sort (end. dir, end. dir + 4, CMP); <br/> BFS (); <br/>}< br/> return 0; <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.