Quick test-Basic Search (Hangzhou Telecom 5)

Source: Internet
Author: User
HDU 1241 oil deposits

Water question, BFs, determine the number of blocks in the region.

Code List:

#include<queue>#include<cstdio>#include<cstring>#include<iostream>#include<algorithm>using namespace std;typedef pair<int,int>P;int m,n;char s[105][105];int xy[8][2]={{0,-1},{-1,-1},{-1,0},{-1,1},{0,1},{1,1},{1,0},{1,-1}};void bfs(int x,int y){    queue<P>q;    s[x][y]='*';    q.push(P(x,y));    while(q.size()){        P p=q.front(); q.pop();        for(int i=0;i<8;i++){            int xx=p.first+xy[i][0];            int yy=p.second+xy[i][1];            if(xx>=0&&xx<m&&yy>=0&&yy<n&&s[xx][yy]!='*'){                s[xx][yy]='*';                q.push(P(xx,yy));            }        }    }}int main(){    while(scanf("%d%d",&m,&n)!=EOF){        if(m==0&&n==0) break;        //memset(s,'\0',sizeof(s));        for(int i=0;i<m;i++)            scanf("%s",s[i]);        int ans=0;        for(int i=0;i<m;i++){            for(int j=0;j<n;j++){                if(s[i][j]=='@'){                    ans++;                    bfs(i,j);                }            }        }        printf("%d\n",ans);    }return 0;}

Hdu1312 red and black questions, BFs (DFS), sum of accessible points.

#include<queue>#include<cstdio>#include<cstring>#include<iostream>#include<algorithm>using namespace std;typedef pair<int,int>P;int m,n;int sx,sy;int vis[25][25];char s[25][25];int xy[4][2]={{0,-1},{-1,0},{0,1},{1,0}};int bfs(){    int sum=0;    queue<P>q;    while(q.size()) q.pop();    q.push(P(sx,sy));    vis[sx][sy]=1;    while(q.size()){        P p=q.front();        q.pop();        sum++;        for(int i=0;i<4;i++){            int xx=p.first+xy[i][0];            int yy=p.second+xy[i][1];            if(xx>=0&&xx<n&&yy>=0&&yy<m&&s[xx][yy]!='#'&&!vis[xx][yy]){                vis[xx][yy]=1;  //cout<<xx<<" "<<yy<<endl;                q.push(P(xx,yy));            }        }    }return sum;}int main(){    while(scanf("%d%d",&m,&n)!=EOF){        if(m==0&&n==0) break;        for(int i=0;i<n;i++){            scanf("%s",s[i]);            for(int j=0;j<m;j++){                if(s[i][j]=='@'){                    sx=i;                    sy=j;                }            }        }        memset(vis,0,sizeof(vis));        printf("%d\n",bfs());    }return 0;}

HDU 1010 tempter of the bonedfs, which has learned parity pruning.

Q: there is only one door in the Maze and the door is opened only in the K-second. Can you walk out of the maze.

# Include <queue> # include <cstdio> # include <cstring> # include <iostream> # include <algorithm> using namespace STD; int first; int n, m, T; int Sx, Sy; int ex, ey; char s [8] [8]; int X [4] = {1,-1, 0, 0 }; int y [4] = {0, 0,-1, 1}; void DFS (int x, int y, int t) {If (t = T) {If (x = ex & Y = ey) First = 1; return;} If (first) return; int judge = ABS (X-Ex) + ABS (Y-ey)-ABS (t-t); // parity pruning: judge <= 0 and an even number can continue if (Judge> 0 | judge % 2) return; For (INT I = 0; I <4; I ++) {int xx = x + X [I]; int YY = Y + Y [I]; if (XX> = 0 & XX <n & YY> = 0 & YY <M & S [XX] [YY]! = 'X') {s [XX] [YY] = 'X'; DFS (XX, YY, t + 1); s [XX] [YY] = '. ';}} int main () {While (scanf ("% d", & N, & M, & T )! = EOF) {If (n = 0 & M = 0 & t = 0) break; int Pos = 0; For (INT I = 0; I <n; I ++) {scanf ("% s", s [I]); For (Int J = 0; j <m; j ++) {If (s [I] [J] = 's') {SX = I; Sy = J ;} else if (s [I] [J] = 'D') {EX = I; ey = J ;} else if (s [I] [J] = 'X') {pos ++ ;}}} if (N * m-pos <= T) printf ("NO \ n"); // The number of walk points must be greater than T else {First = 0; s [SX] [sy] = 'X'; DFS (sx, sy, 0); If (first) printf ("Yes \ n"); else printf ("NO \ n") ;}} return 0 ;}

HDU 1242 rescue priority queue + BFs. Note that there are multiple rescue sites, so you only need to go to the nearest rescue site from the origin point.

#include<queue>#include<cstdio>#include<cstring>#include<iostream>#include<algorithm>using namespace std;struct edge{    int x,y;    int t;    friend bool operator<(edge a,edge b){        return a.t>b.t;    }};int N,M;int ex,ey,ok;int vis[205][205];char s[205][205];int xy[4][2]={{0,-1},{-1,0},{0,1},{1,0}};int bfs(){    priority_queue<edge>q;    while(q.size()) q.pop();    edge p;    p.x=ex; p.y=ey; p.t=0;    vis[ex][ey]=1;    q.push(p);    while(q.size()){        p=q.top();        q.pop();        if(s[p.x][p.y]=='r'){            return p.t;        }        for(int i=0;i<4;i++){            edge w;            w.x=p.x+xy[i][0];            w.y=p.y+xy[i][1];            if(w.x>=0&&w.x<N&&w.y>=0&&w.y<M&&s[w.x][w.y]!='#'&&!vis[w.x][w.y]){                w.t=p.t+1;                if(s[w.x][w.y]=='x') w.t++;                vis[w.x][w.y]=1;                q.push(w);            }        }    }    return -1;}int main(){    while(scanf("%d%d",&N,&M)!=EOF){        for(int i=0;i<N;i++){            scanf("%s",s[i]);            for(int j=0;j<M;j++){                if(s[i][j]=='a'){                    ex=i;                    ey=j;                }            }        }        memset(vis,0,sizeof(vis));        int ans=bfs();        if(ans!=-1) printf("%d\n",ans);        else        printf("Poor ANGEL has to stay in the prison all his life.\n");    }return 0;}

HDU 1026 Ignatius and the princess I

Priority queue + BFS + path restoration.

# Include <queue> # include <cstdio> # include <cstring> # include <iostream> # include <algorithm> using namespace STD; struct edge {int X, Y; int t, r; friend bool operator <(edge C, edge d) {return C. t> D. t ;}} A [10000], B [105] [105]; // path of the B array record: int n, m; int vis [105] [105]; char s [105] [105]; int XY [4] [2] = {0,-1 }}; void BFS () {priority_queue <edge> q; while (Q. size () Q. pop (); edge P, W; p. X = 0; p. y = 0; p. T = 0, P. R = 0; vis [0] [0] = 1; q. push (p); int first = 0; while (Q. size () {P = Q. top (); q. pop (); If (P. X = N-1 & P. y = M-1) {First = 1; break;} For (INT I = 0; I <4; I ++) {W. X = P. X + XY [I] [0]; W. y = P. Y + XY [I] [1]; If (W. x> = 0 & W. x <n & W. y> = 0 & W. Y <M & S [W. x] [W. y]! = 'X '&&! Vis [W. x] [W. y]) {B [W. x] [W. y]. X = P. x; B [W. x] [W. y]. y = P. y; W. T = P. t + 1; W. R = P. R + 1; vis [W. x] [W. y] = 1; if (s [W. x] [W. y]! = '. ') W. T = W. T + s [W. x] [W. y]-'0'; q. push (w) ;}}if (first) {int K = 1; int I = P. r-1, xx = P. x, YY = P. y; A [p. r]. X = P. x; A [p. r]. y = P. y; for (I = P. r-1; I> = 0; I --) {A [I]. X = B [XX] [YY]. x; A [I]. y = B [XX] [YY]. y; xx = A [I]. x; YY = A [I]. y;} printf ("it takes % d seconds to reach the target position, let me show you the way. \ n ", p. t); For (INT I = 0; I <p. r; I ++) {printf ("% DS :( % d, % d)-> (% d, % d) \ n", K ++, A [I]. x, a [I]. y, a [I + 1]. x, a [I + 1]. y); If (S [A [I + 1]. x] [A [I + 1]. Y]! = '.' & S [A [I + 1]. x] [A [I + 1]. Y]! = 'X') {int TT = s [A [I + 1]. x] [A [I + 1]. y]-'0'; while (TT --) printf ("% DS: fight at (% d, % d) \ n", K ++, A [I + 1]. x, a [I + 1]. y) ;}} else printf ("God please help our poor hero. \ n "); printf (" Finish \ n ");} int main () {While (scanf (" % d ", & N, & M )! = EOF) {for (INT I = 0; I <n; I ++) scanf ("% s", s [I]); memset (VIS, 0, sizeof (VIS); BFS ();} return 0 ;}


Quick test-Basic Search (Hangzhou Telecom 5)

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.