国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

bfs練習題

2019-11-10 23:02:29
字體:
來源:轉載
供稿:網友

藍橋杯練習記錄

1、bfs(挑戰程序設計競賽)P32

代碼如下:

#include<cstdio>#include<queue>#define maxn 105using namespace std;char field[maxn][maxn];typedef pair<int,int>P;int N,M;int dis[10][3]= { {0,1},{0,-1},{-1,0},{1,0},{-1,-1},{-1,1},{1,-1},{1,1} };void bfs(int x,int y){    queue<P>que;    que.push(P(x,y));    field[x][y]='.';    while(que.size())    {        P p=que.front();        que.pop();        for(int i=0; i<8; i++)        {                int nx=p.first+dis[i][0];                int ny=p.second+dis[i][1];                if(nx>0&&nx<=N&&ny>0&ny<=M&&field[nx][ny]=='W')                {                    que.push(P(nx,ny));                    field[nx][ny]='.';                }            }    }    return ;}int main(){    int counter;    while(~scanf("%d %d",&N,&M))    {        getchar();  ///很重要        counter=0;///注意初始化的位置        for(int i=1; i<=N; i++)        {            for(int j=1; j<=M; j++)            {                scanf("%c",&field[i][j]);            }  ///注意大括號            getchar();///很重要        }        for(int i=1; i<=N; i++)            for(int j=1; j<=M; j++)                if(field[i][j]=='W')                {                    bfs(i,j);                    counter++;                }        PRintf("%d/n",counter);    }    return 0;}


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 普陀区| 通河县| 滦南县| 五华县| 枞阳县| 闽清县| 商都县| 梅州市| 定陶县| 城口县| 博野县| 永安市| 松滋市| 合水县| 温泉县| 平顺县| 巴彦县| 云南省| 青阳县| 临汾市| 肥乡县| 石景山区| 巍山| 株洲市| 浦东新区| 满城县| 即墨市| 巴林右旗| 乌拉特后旗| 军事| 句容市| 张家港市| 博野县| 新津县| 古蔺县| 余江县| 梓潼县| 息烽县| 容城县| 和田县| 承德县|