POJ 1979 Red and Black
2017-03-13 23:12
357 查看
原题链接
思路:经典深搜
AC代码:
思路:经典深搜
AC代码:
#include <iostream> #include <cstdio> #include <algorithm> #include <cstdlib> #include <cstring> #include <queue> using namespace std; //与POJ_2386水洼题相似 //dfs深度优先搜索 int ans,dis[21][21],w,h; char room[21][21]; int dx[4]={1,0,0,-1}; int dy[4]={0,1,-1,0}; void dfs(int x,int y) { int i; for(i=0;i<4;i++){ int x0=x+dx[i],y0=y+dy[i]; if(room[x0][y0]=='.' && dis[x0][y0]==0 && x0>=0 && y0>=0 && x0<h && y0<w){ dis[x0][y0]=1; ans++; dfs(x0,y0); } } return; } int main() { int i,j,x0,y0; cin>>w>>h; getchar(); while(w||h){ ans=0; memset(dis,0,sizeof(dis)); for(i=0;i<h;i++) gets(room[i]); for(i=0;i<h;i++){ for(j=0;j<w;j++){ if(room[i][j]=='@'){ dis[i][j]=1; ans++; dfs(i,j); } } } printf("%d\n",ans); cin>>w>>h; getchar(); } return 0; }
相关文章推荐
- zoj 2165||poj 1979 red and black (dfs)
- POJ 1979 Red and Black
- poj 1979 , zoj 2165 Red and Black【dfs,入门题】
- poj 1979 red and black
- POJ-1979-Red and Black
- poj 1979 Red and Black
- poj 1979 zoj 2165 Red and Black 深搜做法
- zoj 2165 Red and Black (DFs)poj 1979
- POJ 1979 Red and Black(C语言堆栈实现)
- 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
- POJ1979 Red and Black dfs基础题
- POJ 1979 Red and Black(BFS广度优先搜索)
- poj1979——Red and Black
- poj 1979 Red and Black
- poj 1979 Red and Black
- poj 1979 zoj 2165 Red and Black