Red and Black HDU-1312
来源:互联网 发布:陈奕迅 爱情转移 知乎 编辑:程序博客网 时间:2024/05/27 10:42
一个简单的深搜问题,所有访问过的点都不要再访问,不用再搜索,所以也不用回溯。代码写的有点长,也有点麻烦,不过好理解。
#include<iostream>#include<cstdio>#include<cstring>#include<cstdlib>#include<algorithm>#include<cmath>#include<queue>using namespace std;char maze[25][25];int m,n;int vis[25][25];int dx[4]={1,-1,0,0};int dy[4]={0,0,1,-1};int sum=1;void DFS(int x,int y){ int temp=0; for(int i=0;i<4;i++) { int x3,y3; x3=x+dx[i]; y3=y+dy[i]; if(maze[x3][y3]=='#'||vis[x3][y3]==1||x3<0||x3>=n||y3<0||y3>=m) temp++; } if(temp==4) { return ; } int x1,y1; for(int i=0;i<4;i++) { x1=x+dx[i]; y1=y+dy[i]; if(maze[x1][y1]=='.'&&vis[x1][y1]==0&&x1>=0&&x1<n&&y1>=0&&y1<m) { vis[x1][y1]=1; sum++; DFS(x1,y1); } }}int main(){ while(cin>>m>>n) { if(m==0&&n==0) break; for(int i=0;i<n;i++) for(int j=0;j<m;j++) vis[i][j]=0; int x2,y2; for(int i=0;i<n;i++) { for(int j=0;j<m;j++) { cin>>maze[i][j]; if(maze[i][j]=='@') { x2=i; y2=j; } } } DFS(x2,y2); cout<<sum<<endl; sum=1; } return 0;}
阅读全文
0 0
- Red and Black hdu 1312
- HDU 1312 Red and Black
- HDU 1312 Red and Black
- hdu 1312 Red and Black
- Hdu 1312 - Red and Black
- hdu-1312-Red and Black
- hdu 1312Red and Black
- hdu 1312 Red and Black
- hdu - 1312 - Red and Black
- hdu 1312 Red and Black
- hdu 1312 Red and Black
- HDU-1312(red and black)
- HDU 1312 Red and Black
- hdu 1312 Red and Black
- HDU 1312 Red and Black
- hdu 1312 Red and Black
- hdu 1312 Red and Black
- hdu 1312 Red and Black
- KMP算法 数据结构
- html基础
- Android 内存相关
- 关于建模的初步学习笔记(1)
- 看《欢乐颂》五大美女独自在家_用什么智能用具
- Red and Black HDU-1312
- 开发环境搭建--Ubunut下ADT 安装
- log4j.xml根据日志级别输入日志配置
- android Appium 常用命令
- Android PullUpToRefresh刷新框架之PullToRefreshExpandableListView
- matlab统计相关函数(不定时更新)
- ADO比较基础的总结
- 查找n个元素中的第二小元素
- 读取excel