Go hdu 4158 hoj 簡單搜尋

來源:互聯網
上載者:User
這是一個建立於 的文章,其中的資訊可能已經有所發展或是發生改變。

/*搜尋中狀態排入佇列時就要立即更新標誌,

不能出隊的時候更新,否則中間有部分狀態會重複搜尋造成錯誤。*/

#include <stdio.h>#include <cstring>#include <queue>#include <iostream>using namespace std;int map[21][21];bool vis[21][21];int dx[4]= {1,-1,0,0};int dy[4]= {0,0,-1,1};int cb,cw,n;struct point{    int x,y;}p,t;void bfs(int x,int y){    p.x=x,p.y=y;    queue <point> q;    q.push(p);    int num=0;    vis[p.x][p.y]=true;    int r2=-1,r3=-1;    while(!q.empty())    {        t=q.front();        num++;        q.pop();        for(int i=0; i<4; i++)        {            p.x=t.x+dx[i];            p.y=t.y+dy[i];            if(vis[p.x][p.y]) continue;            if(p.x<1||p.x>n||p.y<1||p.y>n) continue;            if(map[p.x][p.y]==1) r2=1;            else if(map[p.x][p.y]==2) r3=1;            else q.push(p),vis[p.x][p.y]=true;        }    }    if(r2==1&&r3==1) return ;    if(r3==-1) cb+=num;    else if(r2==-1) cw+=num;    return ;}int main(){    int b,w,x,y;    while(scanf("%d",&n)==1&&n)    {        memset(map,0,sizeof(map));        memset(vis,false,sizeof(vis));        cb=cw=0;        scanf("%d%d",&b,&w);        for(int i=0; i<b; i++)            scanf("%d%d",&x,&y),map[x][y]=1;        for(int i=0; i<w; i++)            scanf("%d%d",&x,&y),map[x][y]=2;        for(int i=1; i<=n; i++)            for(int j=1; j<=n; j++)                if(!map[i][j]&&!vis[i][j]) bfs(i,j);        if(cb==cw) printf("Draw\n");        else if(cb>cw) printf("Black wins by %d\n",cb-cw);        else printf("White wins by %d\n",cw-cb);    }    return 0;}


相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.