1011寻找@
来源:互联网 发布:萨沙-考恩生涯数据 编辑:程序博客网 时间:2024/05/21 06:17
题意:经典的深搜,寻找@块,给出行和列,组成个地图。在其中搜索。@块对于横竖左右斜都能组成一个@块,问有几个@块、
代码:
#include<iostream>
#include<cstring>
using namespace std;
int m,n;
const int max=600;
int dir[8][2]={(-1,0),(1,0),(0,1),(0,-1),(-1,-1),(-1,1),(1,-1),(1,1)};
char chess[600][600];
bool v[max][max];
bool isbound(int a,int b)
{
if(a<0||b<0||a>m||b>n)
return 1;
else return 0;
}
void dfs(int x,int y)
{
for(int i=0;i<8;i++){
if(chess[x+dir[i][0]][y+dir[i][1]]=='*') continue;
if(isbound(x+dir[i][0],y+dir[i][1])!=0) continue;
if(v[x+dir[i][0]][y+dir[i][1]]!=0) continue;
v[x+dir[i][0]][y+dir[i][1]]=1;
dfs(x+dir[i][0],y+dir[i][1]);
}
}
int main()
{
while(cin>>m>>n)
{
memset(v,0,sizeof(v));
if(m==0||n==0) break;
for(int i=0;i<m;i++)
for(int j=0;j<n;j++)
cin>>chess[i][j];
int sum=0;
for(int a=0;a<m;a++)
{
for(int j=0;j<=n;j++)
if(chess[a][j]=='@'&&!v[a][j])
{
v[a][j]=1;
dfs(1,1);
sum++;
}
}
cout<<sum<<endl;
}
#include<cstring>
using namespace std;
int m,n;
const int max=600;
int dir[8][2]={(-1,0),(1,0),(0,1),(0,-1),(-1,-1),(-1,1),(1,-1),(1,1)};
char chess[600][600];
bool v[max][max];
bool isbound(int a,int b)
{
if(a<0||b<0||a>m||b>n)
return 1;
else return 0;
}
void dfs(int x,int y)
{
for(int i=0;i<8;i++){
if(chess[x+dir[i][0]][y+dir[i][1]]=='*') continue;
if(isbound(x+dir[i][0],y+dir[i][1])!=0) continue;
if(v[x+dir[i][0]][y+dir[i][1]]!=0) continue;
v[x+dir[i][0]][y+dir[i][1]]=1;
dfs(x+dir[i][0],y+dir[i][1]);
}
}
int main()
{
while(cin>>m>>n)
{
memset(v,0,sizeof(v));
if(m==0||n==0) break;
for(int i=0;i<m;i++)
for(int j=0;j<n;j++)
cin>>chess[i][j];
int sum=0;
for(int a=0;a<m;a++)
{
for(int j=0;j<=n;j++)
if(chess[a][j]=='@'&&!v[a][j])
{
v[a][j]=1;
dfs(1,1);
sum++;
}
}
cout<<sum<<endl;
}
}
0 0
- 1011寻找@
- 寻找
- 寻找
- 寻找
- 寻找...
- 寻找
- 寻找
- 寻找。。。。。。
- 寻找
- 寻找
- 寻找......
- 寻找
- 寻找
- 寻找
- 寻找?
- 寻找
- 寻找Kylix
- 寻找资源
- iOS开发总结之自定义等高cell02-storyboard(封装)
- LeetCode 35 Search Insert Position
- 【解决方法】eclipse debug Unable to install breakpoint in xxx
- 对WIN32中窗口的理解
- Java compiler level does not match the version of the installed Java project
- 1011寻找@
- DES、3DES
- Android SDK Manager无法更新的解决方案
- [网络编程]——UDP
- Volley的使用
- android学习——Handler引起的内存泄露及解决方法
- 二分查找-lintcode
- iOS开发总结之项目开发中使用UITableView自定义多级表格&自定义cell
- You have an error in your SQL syntax;