Figure BFS-HDU 1241/HDU 2612/poj 3984

Source: Internet
Author: User
Tags cmath

This is completely a review of previous knowledge... some familiar methods gradually emerged when I was writing ........--

In fact, the questions are very watery... but for the sake of my wa, I posted it ....

HDU 1241

# Include <cstdio> # include <cstring> # include <iostream> # include <cmath> # include <string> # include <vector> # include <map> # include <algorithm> using namespace STD; inline int RINT () {int X; scanf ("% d", & X); Return X;} inline int max (int x, int y) {return (x> Y )? X: Y;} inline int min (int x, int y) {return (x <Y )? X: Y ;}# define for (I, a, B) for (INT I = (a); I <= (B); I ++) # define Ford (I, a, B) for (INT I = (a); I >= (B); I --) # define rep (X) for (INT I = 0; I <(x); I ++) typedef long int64; # define Inf (1 <30) # define bug (s) cout <# S <"=" <S <"" # define maxn 102int G [maxn * maxn]; // (x, y) => x + y * mint DX [] = {-1, 0, 1, 0, 1, 1,-1,-1}; int dy [] = {0, 1, 0,-1, 1,-1, 1,-1}; int vis [maxn * maxn]; int Q [maxn * maxn]; int front, tail; Int n, m; void BFS (INT st) {// memset (Q, 0, sizeof (q); front = tail = 0; Q [tail ++] = sT; while (front <tail) {int cur = Q [Front ++]; int x = cur % maxn; int y = cur/maxn; rep (8) {int Tx = x + dx [I]; int ty = Y + dy [I]; int now = Tx + ty * maxn; if (G [now] =-1 | G [now] = 1 | Vis [now]) continue; vis [now] = 1; Q [tail ++] = now ;}} int main () {While (M = RINT () {int ans = 0; n = RINT (); if (M = 0) break; memset (G,-1, sizeof (g); me Mset (VIS, 0, sizeof (VIS); for (I, 1, m) // rows {char Buf [maxn]; scanf ("% s", Buf ); for (J, 1, n) {G [J + maxn * I] = Buf [J-1] = '*'? 1: 0; int Va = J + maxn * I; // bug (VA); // bug (G [VA]) <Endl ;}} for (I, 1, m) // rowsfor (J, 1, n) {int cur = J + I * maxn; // bug (cur); // bug (G [cur]) <Endl; If (! G [cur] &! Vis [cur]) {vis [cur] = 1; BFS (cur); ans ++ ;}} printf ("% d \ n", ANS );}}

HDU 2612

I can't brush too many questions at night, so my mind is dizzy... this question is wa to death. wa to death ....

First, the array of the result (KFC) is reduced, so that only maxn is available. In fact, KFC can open the full graph .... so it should be maxn * maxn .... I have watched this Re for a long time ....

Then the result array should be initialized to INF... Or wa...

In the end, we found that 'y' M 'can also be used. In fact, we can't do it ..... actually, it's right... when I write it, I am so dumb that I can reverse it ..... shoot ......

Code:

# Include <cstdio> # include <cstring> # include <iostream> # include <cmath> # include <string> # include <vector> # include <map> # include <algorithm> using namespace STD; inline int RINT () {int X; scanf ("% d", & X); Return X;} inline int max (int x, int y) {return (x> Y )? X: Y;} inline int min (int x, int y) {return (x <Y )? X: Y ;}# define for (I, a, B) for (INT I = (a); I <= (B); I ++) # define Ford (I, a, B) for (INT I = (a); I >= (B); I --) # define rep (X) for (INT I = 0; I <(x); I ++) typedef long int64; # define Inf (0 xffffff) # define bug (s) cout <# S <"=" <S <"" # define maxn 210int G [maxn * maxn]; // (x, y) => x + y * mint DX [] = {-1, 0, 1, 0}; int dy [] = {0, 1, 0,-1 }; int vis [maxn * maxn]; int Fa [maxn * maxn]; int Q [maxn * maxn]; int front, tail; int N, M; int s [2]; // int ret [2] [maxn + 3 + 1000000]; int ret [2] [maxn * maxn]; int idx; // kfcint CAL (INT cur) {If (Fa [cur]! = Cur) return CAL (Fa [cur]) + 1; return 0;} void BFS (INT St, int ID) {Fa [st] = sT; front = tail = 0; Q [tail ++] = sT; while (front <tail) {int cur = Q [Front ++]; int x = cur % maxn; int y = cur/maxn; rep (4) {int Tx = x + dx [I]; int ty = Y + dy [I]; int now = Tx + ty * maxn; If (G [now] =-1 | Vis [now]) continue; If (G [now]> 0) // KFC {RET [ID] [G [now] = CAL (cur) + 1; // can KFC pass? // Continue;} vis [now] = 1; Fa [now] = cur; Q [tail ++] = now ;}} int main () {While (scanf ("% d", & N, & M) = 2) {idx = 0; // kfcint Si = 0; memset (G, -1, sizeof (g); for (I, 1, n) // rows {char Buf [maxn]; scanf ("% s", Buf ); for (J, 1, m) {char T = Buf [J-1]; int val = J + maxn * I; if (t = 'y' | T = 'M') s [Si ++] = val; If (t = '@') //> 1, KFC {G [Val] =++ idx;} else // G [Val] = T = '. '? 0:-1; // only # is not accessible .... 'y' M 'can also be taken .... wa to death ...... ② G [Val] = T = '#'? -1: 0 ;}/// not necessarily every KFC can let two people reach .... so RET needs to initialize... cause wa ..... to death .... ① for (J, 1, idx) {rep (2) {RET [I] [J] = inf; // You can also remove this part from the actual test .... weak data ?}} Rep (2) {memset (VIS, 0, sizeof (VIS); vis [s [I] = 1; BFS (s [I], I );} int Minx = inf; for (I, 1, idx) {// bug (I); // bug (Ret [0] [I]) <Endl; minx = min (Ret [0] [I] + RET [1] [I], Minx);} printf ("% d \ n", 11 * Minx );}}

poj 3984

# Include <cstdio> # include <cstring> # include <iostream> # include <cmath> # include <string> # include <vector> # include <map> # include <algorithm> using namespace STD; inline int RINT () {int X; scanf ("% d", & X); Return X;} inline int max (int x, int y) {return (x> Y )? X: Y;} inline int min (int x, int y) {return (x <Y )? X: Y ;}# define for (I, a, B) for (INT I = (a); I <= (B); I ++) # define Ford (I, a, B) for (INT I = (a); I >= (B); I --) # define rep (X) for (INT I = 0; I <(x); I ++) typedef long int64; # define Inf (1 <30) # define bug (s) cout <# S <"=" <S <"" # define maxn 8int G [maxn * maxn]; // (x, y) => x + y * mint DX [] = {-1, 0, 1, 0}; int dy [] = {0, 1, 0,-1 }; // int vis [maxn] [maxn]; int Fa [maxn * maxn]; int Q [maxn * maxn]; int front, tail; int M = Maxn; void print (int x, int y) {If (X! = 1 | y! = 1) {int cur = x + y * m; int now = Fa [cur]; int Tx = now % m; int ty = now/m; print (TX, ty);} printf ("(% d, % d) \ n", x-1, Y-1);} void BFS (INT st) {memset (Q, 0, sizeof (q); memset (FA, 0, sizeof (FA); front = tail = 0; Q [tail ++] = sT; while (1) {int cur = Q [Front ++]; int x = cur % m; int y = cur/m; If (x = 5 & Y = 5) {print (x, y); return;} rep (4) {int Tx = x + dx [I]; int ty = Y + dy [I]; int now = Tx + ty * m; If (G [now] =-1 | G [now] = 1 | Fa [now]) continue; fa [now] = cur; Q [tail ++] = now ;}} int main () {memset (G,-1, sizeof (g )); for (I, 1, 5) for (J, 1, 5) {G [I + M * j] = RINT ();} BFS (1 + 1 * m );}

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.