POJ 1979 Red and Black(BFS)
来源:互联网 发布:python 计算技术指标 编辑:程序博客网 时间:2024/05/15 16:24
题意:有一个H*W的矩阵,'.'表示可以走的地方,'#'表示不能走的地方,你最初在'@'的地方,输出你最多可以到达的地方数。
思路:直接BFS即可。
注意:当没有可以走的地方时,输出1。
#include<stdio.h>#include<string.h>int w,h,ans,me_x,me_y;char word[100][100];int number[100][100];int front1,rear;int dx[4]={-1,1,0,0};//上下左右int dy[4]={0,0,-1,1};struct node{ int x,y;}q[1000];void bfs(int xx,int yy){ int i,j; q[front1].x=xx; q[front1].y=yy; while(front1<rear) { for(i=0;i<4;i++)//搜索可能有的路径 { int a=dx[i]+q[front1].x; int b=dy[i]+q[front1].y; if(a<=0||b<=0||a>h||b>w||!number[a][b])//已到边界 continue; else { number[a][b]=0; q[rear].x=a; q[rear].y=b; rear++;//入队 ans++; } } front1++;//出队 }}int main(){ //freopen("in.txt","r",stdin); int i,j; while(scanf("%d%d",&w,&h)) { front1=0,rear=1; getchar(); ans=0; memset(number,0,sizeof(number));//当为0时表示无法走 if(w==0&&h==0) break; for(i=1;i<=h;i++) { for(j=1;j<=w;j++) { scanf("%c",&word[i][j]); if(word[i][j]=='.') number[i][j]=1; else if(word[i][j]=='@') { number[i][j]=1; me_x=i; me_y=j; } } getchar(); } bfs(me_x,me_y); if(ans==0)//如果除了该点之外都无法到达要特判 ans=1; printf("%d\n",ans); } return 0;}
- poj 1979Red and Black(BFS DFS)
- POJ 1979 Red and Black(BFS)
- poj 1979 Red and Black - bfs
- POJ 1979 Red and Black (裸BFS)
- poj 1979 Red and Black(BFS)
- POJ 1979 Problem:Red and Black(BFS)
- POJ 1979 Red and Black(BFS)
- 【BFS】poj 1979 Red and Black
- POJ 1979 Red and Black(BFS广度优先搜索)
- zoj 2165 || poj 1979 Red and Black(BFS水水)
- hdu 1312 poj 1979 Red and Black BFS
- poj 1979 Red and Black(DFS||BFS)
- POJ 1979 Red and Black(BFS or DFS)
- poj 1979 &&hdu 1312Red and Black (bfs)
- poj 1979 Red and Black (简单BFS)
- (水)POJ-1979 Red and Black(dfs,bfs)
- (BFS/DFS)POJ 1979 Red and Black
- POJ 1979 Red and Black
- Android 中的 Service 全面总结
- c语言-连接!
- RCC_AHBPeriph_DMA DMA_Channel1 在那里定义的?
- 类的加载,初始化顺序
- LESS详解之函数(二)
- POJ 1979 Red and Black(BFS)
- nodejs入门级基础(数据类型,最基本的语法详解)
- 自定义导航栏
- POJ——多项式加法(WA)
- 小数如何转化为二进制
- fork函数
- PHP基础知识~~~待续~~~
- orcale数据库之连接查询
- MyISAM 和 InnoDB 讲解