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

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

bfs練習題

2019-11-10 21:55:31
字體:
來源:轉載
供稿:網友

藍橋杯練習記錄

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;}


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 陕西省| 镇赉县| 桃园市| 县级市| 东安县| 呼伦贝尔市| 姚安县| 休宁县| 盱眙县| 玉山县| 茶陵县| 西充县| 历史| 唐山市| 马鞍山市| 方山县| 尼玛县| 上林县| 调兵山市| 苏州市| 新竹市| 中宁县| 临澧县| 佳木斯市| 习水县| 环江| 建湖县| 南乐县| 芦溪县| 兰考县| 雷州市| 天门市| 古田县| 商丘市| 开封市| 军事| 永寿县| 朔州市| 塘沽区| 龙胜| 鹤岗市|