poj 1979 Red and Black
2012-03-25 21:03
555 查看
// 题意: 给定一矩形棋盘,上面仅有黑棋和红棋,要求只能在黑棋上移动, // 规定移动方向是上下左右. 给定一起点,求从起点可到达的所有黑棋的个数(包括起点) #include<iostream> #include <string.h> using namespace std; const int MAXN=25; int g[MAXN][MAXN],vis[MAXN][MAXN]; int mv[4][2]={{0,1},{0,-1},{1,0},{-1,0}}; int cnt; void dfs(int x,int y) { if(!g[x][y]||vis[x][y]) return; vis[x][y]=1; cnt++; for(int i=0;i<4;++i) dfs(x+mv[i][0],y+mv[i][1]); } int main() { int n,m; //表示矩形的长和宽 char s[MAXN]; while (scanf("%d%d",&m,&n)&&n) { memset(g,0,sizeof(g)); memset(vis,0,sizeof(vis)); int x,y; for(int i=0;i<n;++i) { scanf("%s",s); for(int j=0;j<m;++j) { if(s[j]!='#') g[i+1][j+1]=1; //把图像往中间移动一点,相当于在矩形外面加上一圈虚拟的红格子,就不用担心越界了 if(s[j]=='@') x=i+1,y=j+1; } } cnt=0; dfs(x,y); printf("%d\n",cnt); } return 0; }
相关文章推荐
- poj 1979 Red and Black ---DFS
- hdu 1312 Red and Black && POJ 1979 Red and Black
- poj 1979 zoj 2165 Red and Black 深搜做法
- Red and Black(poj 1979 bfs)
- POJ 1979 Red and Black
- POJ——1979Red and Black
- [ACM]POJ1979 Red and Black
- Red and Black POJ - 1979-入门dfs
- POJ 1979 Red and Black
- Red and Black (POJ 1979,深度优先搜索)
- POJ:1979 Red and Black
- poj 1979 Red and Black(dfs)
- POJ 1979 Red and Black (dfs)
- 【DFS】poj 1979 Red and Black
- POJ 1979 Red and Black
- POJ 1979 Red and Black
- POJ 1979 Red and Black
- poj-1979-Red and Black
- poj_1979 Red and Black
- POJ 1979:Red and Black(dfs)