POJ-1979
来源:互联网 发布:家园2 网络不可用 编辑:程序博客网 时间:2024/06/06 05:27
代码:
#include<iostream>#include<cstdio>#include<set>#include<queue>#include<utility>#include<vector>#include<map>#include<cmath>#include<cstring>#include<string>using std::pair;#define INF 0x3f3f3f3ftypedef pair<int,int> p;using namespace std;char maze[55][55];int n,m;int sx,sy;int gx,gy;int d[55][55];int dx[4]={1,0,-1,0},dy[4]={0,1,0,-1};int bfs(){ int sum=0; p p1; queue<p> que; memset(d,INF,sizeof(d)); que.push(p(sx,sy)); d[sx][sy]=0; while(que.size()) { //cout<<sx<<' '<<sy<<endl; p1=que.front(); que.pop(); for(int i=0;i<4;i++) { int nx=p1.first+dx[i]; int ny=p1.second+dy[i]; if(nx>=0&&nx<m&&ny>=0&&ny<n&&maze[nx][ny]!='#'&&d[nx][ny]==INF) { que.push(p(nx,ny)); d[nx][ny]=d[p1.first][p1.second]+1; } } } for(int i=0;i<m;i++) { for(int j=0;j<n;j++) { if(d[i][j]!=INF) sum++; } } //cout<<d[3][3]; return sum;}int main(){ int i,j,he; while(cin>>n>>m&&n!=0&&m!=0) { for(i=0;i<m;i++)///n是列,m是行 { for(j=0;j<n;j++) { cin>>maze[i][j]; if(maze[i][j]=='@') { sx=i; sy=j; } } } he=bfs(); cout<<he<<endl; } return 0;}
思路源于挑战程序设计竞赛一书,题目的输入是列在先行在后,所以出了点问题;多花费了一些时间!
阅读全文
0 0
- POJ 1979
- POJ 1979
- POJ 1979
- poj 1979
- POJ 1979
- poj 1979
- poj 1979
- poj 1979
- poj 1979
- poj 1979
- poj 1979
- poj 1979
- poj-1979
- poj 1979
- POJ 1979
- Poj-1979
- poj 1979
- POJ 1979
- ICPC乌鲁木齐赛后感悟
- 数组1
- 12.03学习计划
- 、尊重对象所有权
- i love u 加 爱心 //脑子有病 爱心抄的
- POJ-1979
- dtd入门和引入方式
- [Android]自己动手做个拼图游戏
- 抽象类和接口的区别
- STM32学习记录之环境配置篇
- 每日一练-14
- java面试大全
- SSH与SSM学习之SSH实现CRM练习16——登录校验拦截器
- Spring+MVC+Mybatis整合