题目1460:Oil Deposit(递归遍历图)
题目链接:http://ac.jobdu.com/problem.php?pid=1460
详解链接:https://github.com/zpfbuaa/JobduInCPlusPlus
参考代码:
// // 1460 Oil Deposit.cpp // Jobdu // // Created by PengFei_Zheng on 23/04/2017. // Copyright © 2017 PengFei_Zheng. All rights reserved. // #include <stdio.h> #include <iostream> #include <algorithm> #include <string.h> #include <cmath> #define MAX_SIZE 101 using namespace std; char plot[MAX_SIZE][MAX_SIZE]; bool visited[MAX_SIZE][MAX_SIZE]; int change[2][8]={{-1,-1,-1,0,0,1,1,1},{-1,0,1,-1,1,-1,0,1}}; int n, m; void DFS(int x, int y){ for(int i = 0 ; i < 8 ; i++){ int nx = x + change[0][i]; int ny = y + change[1][i]; if(nx<0 || nx>=n || ny<0 || ny>=m) continue; if(plot[nx][ny]=='*') continue; if(visited[nx][ny]==true) continue; visited[nx][ny]=true; DFS(nx,ny); } return; } int main(){ // for(int i = 0 ; i < 8 ; i ++){ // cout<<"change[0]["<<i<<"] = "<<change[0][i]<<" "; // cout<<"change[1]["<<i<<"] = "<<change[1][i]<<endl; // } while(scanf("%d%d",&n,&m)!=EOF){ if(0==n && 0==m) break; for(int i = 0 ; i < n ; i++){ scanf("%s",plot[i]); } for(int i = 0; i < n ; i ++){ for(int j = 0 ; j < m ; j++){ visited[i][j]=false; } } int ans = 0; for(int i = 0 ; i < n ; i++){ for(int j = 0 ; j < m ; j++){ if(visited[i][j]==true) continue; if(plot[i][j]=='*') continue; DFS(i,j); ans++; } } printf("%d\n",ans); } return 0; } /************************************************************** Problem: 1460 User: zpfbuaa Language: C++ Result: Accepted Time:10 ms Memory:1540 kb ****************************************************************/
作者: 伊甸一点
出处: http://www.cnblogs.com/zpfbuaa/
本文版权归作者伊甸一点所有,欢迎转载和商用(须保留此段声明),且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利.
原文链接 如有问题, 可邮件(zpflyfe@163.com)咨询.